Skip to content

Commit 8d34b59

Browse files
authored
Create MaximumDepthOfBinaryTree.java
1 parent f89a60e commit 8d34b59

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

MaximumDepthOfBinaryTree.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class MaximumDepthOfBinaryTree {
2+
public static void main(String[] args){
3+
System.out.println("Maximum depth of the tree is : " + getDepth(getTree()));
4+
}
5+
6+
public static class TreeNode {
7+
int val;
8+
TreeNode left;
9+
TreeNode right;
10+
TreeNode(int x) { val = x; }
11+
}
12+
13+
private static int getDepth(TreeNode root) {
14+
if(root == null) {
15+
return 0;
16+
}
17+
return Integer.max(getDepth(root.left), getDepth(root.right)) + 1;
18+
}
19+
20+
public static TreeNode getTree(){
21+
TreeNode root = new TreeNode(10);
22+
root.left = new TreeNode(5);
23+
root.right = new TreeNode(15);
24+
root.right.right = new TreeNode(20);
25+
root.right.right.right = new TreeNode(23);
26+
return root;
27+
}
28+
}

0 commit comments

Comments
 (0)