题目描述
判断是否平衡二叉树 - >左右子树高度差不超过 1。
代码实现
public class Solution {
boolean isBalance = true;
public boolean IsBalanced_Solution(TreeNode root) {
high(root);
return isBalance;
}
private int high(TreeNode root) {
if (root == null || !isBalance) {
return 0;
}
int left = high(root.left);
int right = high(root.right);
if (Math.abs(left - right) > 1) {
isBalance = false;
}
return 1 + Math.max(left, right);
}
}
网友评论