题目:https://leetcode-cn.com/problems/balanced-binary-tree/
答案:
递归
public boolean isBalanced(TreeNode root) {
if(root==null) return true;
if(Math.abs(maxDepth(root.left)-maxDepth(root.right))>1){
return false;
}else{
return isBalanced(root.left) && isBalanced(root.right);
}
}
public int maxDepth(TreeNode root) {
if(root==null) {
return 0;
}else {
int heightLeft = 1+maxDepth(root.left);
int heightRight = 1+maxDepth(root.right);
return Math.max(heightLeft,heightRight);
}
}