Skip to content

Commit 3e0daa6

Browse files
Create MaximumDepthOfTree.java
1 parent e4fdd89 commit 3e0daa6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
17+
import java.util.*;
18+
class MaximumDepthOfTree {
19+
public int maxDepth(TreeNode root) {
20+
return postorder(root);
21+
}
22+
23+
public static int postorder(TreeNode root){
24+
if(root == null){
25+
return 0;
26+
}
27+
28+
int leftDepth = postorder(root.left);
29+
int rightDepth = postorder(root.right);
30+
return 1+Math.max(leftDepth, rightDepth);
31+
}
32+
}

0 commit comments

Comments
 (0)