Skip to content

Commit 867d5bb

Browse files
committed
height of tree
1 parent 0f585fe commit 867d5bb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tree/BinaryTree/HeightOfTree.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tree.BinaryTree;
2+
3+
public class HeightOfTree {
4+
public static int height(Node root) {
5+
6+
if (root == null) {
7+
return 0;
8+
}
9+
10+
int left = height(root.left);
11+
int right = height(root.right);
12+
13+
int totalHeight = Math.max(left, right) + 1;
14+
15+
return totalHeight;
16+
}
17+
18+
public static void main(String[] args) {
19+
int[] nodes = { 1, 2, 4, -1, -1, 5, -1, -1, 3, -1, 6, -1, -1 };
20+
21+
CreateBT tree = new CreateBT();
22+
Node root = tree.createTreeFromArr(nodes);
23+
24+
System.out.println(height(root));
25+
}
26+
}

0 commit comments

Comments
 (0)