File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Main104 {
2
+ public static void main(String[] args){
3
+ Main104 main = new Main104();
4
+ main.test();
5
+ }
6
+
7
+ public void test(){
8
+ TreeNode root = new TreeNode(3);
9
+ root.left = new TreeNode(9);
10
+ root.right = new TreeNode(20);
11
+ root.right.left = new TreeNode(15);
12
+ root.right.right = new TreeNode(7);
13
+ System.out.println(maxDepth(root));
14
+ }
15
+
16
+ public int maxDepth(TreeNode root) {
17
+ int leftDep,rightDep,max;
18
+ if (root!=null){
19
+ leftDep = maxDepth(root.left);
20
+ rightDep = maxDepth(root.right);
21
+ max = leftDep>rightDep?leftDep:rightDep;
22
+ return max+1;
23
+ }
24
+ return 0;
25
+ }
26
+
27
+ private class TreeNode{
28
+ int val;
29
+ TreeNode left;
30
+ TreeNode right;
31
+ TreeNode(int x){
32
+ val = x;
33
+ }
34
+ }
35
+
36
+ }
You can’t perform that action at this time.
0 commit comments