-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrees-Find Height
More file actions
29 lines (24 loc) · 869 Bytes
/
Trees-Find Height
File metadata and controls
29 lines (24 loc) · 869 Bytes
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
Given a generic tree, find and return the height of given tree.
Input Format:
The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space.
Output Format :
The first and only line of output prints the height of the given generic tree.
Constraints:
Time Limit: 1 sec
Sample Input 1:
10 3 20 30 40 2 40 50 0 0 0 0
Sample Output 1:
3
************************Solution*****************************
public static int getHeight(TreeNode<Integer> root){
if(root==null)
return 0;
int max=1;
for(TreeNode<Integer> child: root.children)
{
int childHeight=1+getHeight(child);
if(max<childHeight)
max=childHeight;
}
return max;
}