-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode_Number_of_nodes_greater_than x.java
More file actions
52 lines (40 loc) · 1.03 KB
/
Code_Number_of_nodes_greater_than x.java
File metadata and controls
52 lines (40 loc) · 1.03 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
/*
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
Sample Input 2 :
10 10 3 20 30 40 2 40 50 0 0 0 0
Sample Output 2:
5
*/
public class Solution {
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){
if(root == null)
return 0;
int count = 0;
if(root.data > x)
{
count++;
}
for(TreeNode<Integer> node : root.children)
count += numNodeGreater(node, x);
return count;
}
}
/**
* @author Pradumn Patel */