题目:输入一棵二叉树的根结点,判断该树是不是平衡二叉树。如果某二叉树中任意结点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。
int getHeight(Node node){ if(node==null){ return 0; } int heigth = 1; if(node.left!=null){ height = 1+getHeight(node.left); } if(node.rigth!=null){ int h = 1+getHeight(node.right); height = height>h?height:h; } return height;}boolean isBalance(Node node){ if(node==null){ return true; } int left = getHeight(node.left); int right = getHeight(node.right); int diff = left - right; if(diff > 1 || diff < -1) return false; return isBalance(node.left)&&isBalance(node.right);}