import java.lang.Math; //使用深度优先遍历判断左右子树高度差是否大于1,如果某个节点的 //左右子树高度差大于1,则整棵树不是平衡二叉树,否则返回真 public class Solution { boolean isBalanced = true; public boolean IsBalanced_Solution(TreeNode root) { treeDepth(root); return isBalanced; } private int treeDepth(TreeNode root){ if(root == null){ return 0; } int left = treeDepth(root.left); int right = treeDepth(root.right); if(Math.abs(left-right) > 1){ isBalanced = false; } return Math.max(left,right) + 1; }

}