Balanced Binary Tree
LC 110 Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
method3: 这个方法最好,应该用了o(n)的时间复杂度 还是老套路用递归。这道题是求解树是否平衡,还是有一些小技巧的。要判断树是否平衡,根据题目的定义,深度是比需的信息,所以我们必须维护深度,另一方面我们又要返回是否为平衡树,那么对于左右子树深度差的判断也是必要的。这里我们用一个整数来做返回值,而0或者正数用来表示树的深度,而-1则用来比较此树已经不平衡了,如果已经不平衡,则递归一直返回-1即可,也没有继续比较的必要了,否则就利用返回的深度信息看看左右子树是不是违反平衡条件,如果违反返回-1,否则返回左右子树深度大的加一作为自己的深度即可。算法的时间是一次树的遍历O(n),空间是栈高度O(logn)。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return isBalancedHelper(root) != -1;
}
private int isBalancedHelper(TreeNode root) {
if (root == null) return 0;
int left = isBalancedHelper(root.left);
int right = isBalancedHelper(root.right);
if (left == -1 || right == -1 ||Math.abs(left - right) > 1) return -1;
return Math.max(left, right) + 1;
}
}