-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ104.java
36 lines (26 loc) · 901 Bytes
/
Q104.java
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
package algorithms;
import algorithms.TreeNode;
public class Q104 {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
} else {
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
}
public static void main(String[] args) {
int[] nums = {1, 2, 2, 3, 4, 4, 3};
TreeNode treeNode = new TreeNode(nums[0]);
treeNode.left = new TreeNode(2);
treeNode.left.left = new TreeNode(3);
treeNode.left.right = new TreeNode(4);
treeNode.right = new TreeNode(2);
treeNode.right.left = new TreeNode(4);
treeNode.right.right = new TreeNode(3);
Q104 q104 = new Q104();
int ans = q104.maxDepth(treeNode);
System.out.println(ans);
}
}