-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeNumberOfNodesGreaterThanX.java
More file actions
61 lines (53 loc) · 1.48 KB
/
CodeNumberOfNodesGreaterThanX.java
File metadata and controls
61 lines (53 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Code : Number of nodes greater than x
// Send Feedback
// Given a tree and an integer x, find and return number of Nodes which are
// greater than x.
// Input format :
// Single Line : First Integer denotes x and rest of the elements in level order
// form separated by space. Order is -
// Root_data, n (No_Of_Child_Of_Root), n children, and so on for every element
// Output Format :
// Count of nodes greater than x
// Sample Input 1 :
// 35 10 3 20 30 40 2 40 50 0 0 0 0
// Sample Output 1 :
// 3
// Explanation
// Since x=35, the elements which are greater than 35 are 40, 40, 50, so the
// output for this is 3.
// Sample Input 2 :
// 10 10 3 20 30 40 2 40 50 0 0 0 0
// Sample Output 2:
// 5
// Explanation
// Since x=10, the elements which are greater than 10 are 20, 30, 40, 40, 50, so
// the output for this is 5.
public class Solution {
/*
* TreeNode class
*
* class TreeNode<T> {
* T data;
* ArrayList<TreeNode<T>> children;
*
* TreeNode(T data){
* this.data = data;
* children = new ArrayList<TreeNode<T>>();
* }
* }
*/
public static int numNodeGreater(TreeNode<Integer> root, int x) {
// Write your code here
if (root == null) {
return 0;
}
int count = 0;
if (root.data > x) {
count++;
}
for (int i = 0; i < root.children.size(); i++) {
count += numNodeGreater(root.children.get(i), x);
}
return count;
}
}