1. 程式人生 > >劍指offer-39:平衡二叉樹

劍指offer-39:平衡二叉樹

題目描述

輸入一棵二叉樹,判斷該二叉樹是否是平衡二叉樹。

思路

首先,什麼是平衡二叉樹?如果二叉樹中任意結點的左右子樹深度相差不超過1,那麼它就是平衡二叉樹。
最直接的做法,遍歷每個結點,藉助一個獲取樹深度的遞迴函式,根據該結點的左右子樹高度差判斷是否平衡,然後遞迴地對左右子樹進行判斷。

public class Solution39 {
    

    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 root) { if(root == null) { return 0; } return
1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } public static void main(String[] args) { TreeNode tree = BeanUtil.createCompleteBinaryTree(new int[]{1, 3, 2, 5, 6, 34, 13, 54, 23, 31, 45, 23, 91, 22, 33, 44}); BeanUtil.print(new Solution39().IsBalanced_Solution
(tree)); } }

這種做法有很明顯的問題,在判斷上層結點的時候,會多次重複遍歷下層結點,增加了不必要的開銷。如果改為從下往上遍歷,如果子樹是平衡二叉樹,則返回子樹的高度;如果發現子樹不是平衡二叉樹,則直接停止遍歷,這樣至多隻對每個結點訪問一次。

public class Solution39 {


    public boolean IsBalanced_Solution(TreeNode root) {
        return getDepth(root) != -1;
    }

    private int getDepth(TreeNode root) {
        if (root == null) return 0;
        int left = getDepth(root.left);
        if (left == -1) return -1;
        int right = getDepth(root.right);
        if (right == -1) return -1;
        return Math.abs(left - right) > 1 ? -1 : 1 + Math.max(left, right);
    }

    public static void main(String[] args) {

        TreeNode tree = BeanUtil.createCompleteBinaryTree(new int[]{1, 3, 2, 5, 6, 34, 13, 54, 23, 31, 45, 23, 91, 22, 33, 44});
        BeanUtil.print(new Solution39().IsBalanced_Solution(tree));

    }

}