平衡二叉树


输入一棵二叉树,判断该二叉树是否是平衡二叉树。在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树


解题思路

最直接的做法,遍历每个结点,借助一个获取树深度的递归函数,根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。

public class Solution {

    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null) {
            return true;
        }
        return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 
           && IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
    }

    private int maxDepth(TreeNode node) {
        if(node == null) {
            return 0;
        }
        return Math.max(1 + maxDepth(node.left), 1 + maxDepth(node.right));
    }
}

这种做法有很明显的问题,在判断上层结点的时候,会多次重复遍历下层结点,增加了不必要的开销。如果改为从下往上遍历,如果子树是平衡二叉树,则返回子树的高度;如果发现子树不是平衡二叉树,则直接停止遍历,这样至多只对每个结点访问一次。

public class Solution {
    
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root == null) {
            return true;
        }
        return getDepth(root) != -1;
    }
    
    private int getDepth(TreeNode node) {
        if(node == null) {
            return 0;
        }
        int left = getDepth(node.left);
        if(left == -1) {
            return -1;
        }
        int right = getDepth(node.right);
        if(right == -1) {
            return -1;
        }
        return Math.abs(left - right) <= 1 ? Math.max(left, right) + 1 : -1;
    } 
}

原文地址:https://www.cnblogs.com/Yee-Q/p/13854166.html