## 判断二叉树是不是平衡的 原

datacube

``````public class IsBalanceTree {
public static void main(String[] args) {
IsBalanceTree bt = new IsBalanceTree();
TreeNode root = new TreeNode(1);
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(1);
TreeNode n3 = new TreeNode(1);
//        TreeNode n4 = new TreeNode(1);
root.left = n1;
root.right = n2;
n1.left = n3;
//        n3.left = n4;
//        System.out.println(bt.getHeight(root));
System.out.println(bt.isBalance(root));
}

int getHeight(TreeNode root){
if (root == null) return 0;
int left = getHeight(root.left);
int right = getHeight(root.right);
//return left > right?(left+1):(right +1);
return 1 + Math.max(left, right);
}

boolean isBalance(TreeNode root){
if (root == null) return true;
int left = getHeight(root.left);
int right = getHeight(root.right);
if (left - right > 1 || left - right < -1){
return false;
}
return isBalance(root.left) && isBalance(root.right);
}
}
``````

### datacube

09/01
0
0
110. Balanced Binary Tree - LeetCode

Question 110. Balanced Binary Tree Solution 题目大意：判断一个二叉树是不是平衡二叉树 思路：定义个boolean来记录每个子节点是否平衡 Java实现： Ref https://www.youtube.com/watch?v=...

yysue
2018/08/15
34
0
[算法总结] 20 道题搞定 BAT 面试——二叉树

2018/09/04
0
0
[剑指offer] 平衡二叉树

2018/08/08
0
0

kkae8643150
2017/12/01
0
0

Mybatis Plus删除

/** @author beth @data 2019-10-17 00:30 */ @RunWith(SpringRunner.class) @SpringBootTest public class DeleteTest { @Autowired private UserInfoMapper userInfoMapper; /** 根据id删除......

33分钟前
4
0

BobwithB

4
0
java内存模型

ls_cherish

4
0

5
0
js中实现页面跳转（返回前一页、后一页）

5
0