Skip to content

Commit a46215e

Browse files
committed
剑指
1 parent eced7ef commit a46215e

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Solution55.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ public class Solution55 {
1818
public int TreeDepth(TreeNode root){
1919
if (root == null)
2020
return 0;
21+
2122
Queue<TreeNode> queue = new LinkedList<>();
2223
queue.offer(root);
2324
int max_depth = 0;
25+
2426
while (!queue.isEmpty()){
2527
int size = queue.size();
2628
for (int i = 0;i < queue.size();i++){
@@ -34,6 +36,7 @@ public int TreeDepth(TreeNode root){
3436
}
3537
++max_depth;
3638
}
39+
3740
return max_depth;
3841
//return Math.max(TreeDepth(root.left),TreeDepth(root.right))+1;//递归
3942
}

Solution55_2.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package com.usher.algorithm.offer;
22

3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
36
/**
47
* @Author: Usher
58
* @Description:
69
* 平衡二叉树
10+
* 后序遍历,在遍历左右子树节点的时候记录他的深度
711
*/
812
public class Solution55_2 {
13+
14+
private boolean flag = true;
915
public boolean IsBalanced_Solution(TreeNode root) {
16+
TreeDepth(root);
17+
return flag;
18+
}
1019

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;//递归
1130
}
31+
1232
}

Solution58_1.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.usher.algorithm.offer;
22

3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
36
/**
47
* @Author: Usher
58
* @Description:

0 commit comments

Comments
 (0)