递归,注意结果的三重判断
public boolean isBalanced(TreeNode root) { if (root==null) return true; return Math.abs(getHeight(root.left)-getHeight(root.right)) <=1 && isBalanced(root.left) && isBalanced(root.right); } public int getHeight(TreeNode node){ if (node==null){ return 0; } if (node.right == null && node.left ==null){ return 1; } return Math.max(getHeight(node.left),getHeight(node.right))+1; }