File tree Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -18,9 +18,11 @@ public class Solution55 {
18
18
public int TreeDepth (TreeNode root ){
19
19
if (root == null )
20
20
return 0 ;
21
+
21
22
Queue <TreeNode > queue = new LinkedList <>();
22
23
queue .offer (root );
23
24
int max_depth = 0 ;
25
+
24
26
while (!queue .isEmpty ()){
25
27
int size = queue .size ();
26
28
for (int i = 0 ;i < queue .size ();i ++){
@@ -34,6 +36,7 @@ public int TreeDepth(TreeNode root){
34
36
}
35
37
++max_depth ;
36
38
}
39
+
37
40
return max_depth ;
38
41
//return Math.max(TreeDepth(root.left),TreeDepth(root.right))+1;//递归
39
42
}
Original file line number Diff line number Diff line change 1
1
package com .usher .algorithm .offer ;
2
2
3
+ import java .util .LinkedList ;
4
+ import java .util .Queue ;
5
+
3
6
/**
4
7
* @Author: Usher
5
8
* @Description:
6
9
* 平衡二叉树
10
+ * 后序遍历,在遍历左右子树节点的时候记录他的深度
7
11
*/
8
12
public class Solution55_2 {
13
+
14
+ private boolean flag = true ;
9
15
public boolean IsBalanced_Solution (TreeNode root ) {
16
+ TreeDepth (root );
17
+ return flag ;
18
+ }
10
19
20
+ private int TreeDepth (TreeNode root ){
21
+ if (root == null )
22
+ return 0 ;
23
+
24
+ int left = TreeDepth (root .left );
25
+ int right = TreeDepth (root .right );
26
+
27
+ if (Math .abs (left - right ) > 1 )
28
+ flag = false ;
29
+ return Math .max (left ,right )+1 ;//递归
11
30
}
31
+
12
32
}
Original file line number Diff line number Diff line change 1
1
package com .usher .algorithm .offer ;
2
2
3
+ import java .util .LinkedList ;
4
+ import java .util .Queue ;
5
+
3
6
/**
4
7
* @Author: Usher
5
8
* @Description:
You can’t perform that action at this time.
0 commit comments