110. 平衡二叉树 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public boolean isBalanced(TreeNode root) {
return getDeep(root)>=0;
}
public int getDeep(TreeNode root){
if(root==null) return 0;
int left = getDeep(root.left),right = getDeep(root.right);
if(left>=0&&right>=0&&Math.abs(left-right)<=1){
return Math.max(left,right)+1;
}else {
return -1;
}
}
}