鍍金池/ 教程/ Java/ 二叉樹的深度
從尾到頭打印鏈表
滑動(dòng)窗口的最大值
對(duì)稱的二叉樹
數(shù)組中只出現(xiàn)一次的數(shù)字
反轉(zhuǎn)鏈表
序列化二叉樹
把二叉樹打印出多行
丑數(shù)
最小的 k 個(gè)數(shù)
數(shù)據(jù)流中的中位數(shù)
從上往下打印二叉樹
表示數(shù)值的字符串
數(shù)值的整數(shù)次方
樹中兩個(gè)結(jié)點(diǎn)的最低公共祖先
數(shù)組中的逆序?qū)?/span>
兩個(gè)鏈表的第一個(gè)公共結(jié)點(diǎn)
二叉搜索樹與雙向鏈表
二叉樹的鏡像
鏈表中倒數(shù)第 k 個(gè)結(jié)點(diǎn)
二叉樹中和為某一值的路徑
實(shí)現(xiàn) Singleton 模式——七種實(shí)現(xiàn)方式
樹的子結(jié)構(gòu)
字符流中第一個(gè)不重復(fù)的字符
復(fù)雜鏈表的復(fù)制
二叉搜索樹的后序遍歷序列
二維數(shù)組中的查找
調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)前面
合并兩個(gè)排序的鏈表
構(gòu)建乘積數(shù)組
求從 1 到 n 的整數(shù)中 1 出現(xiàn)的次數(shù)
鏈表中環(huán)的入口結(jié)點(diǎn)
數(shù)組中出現(xiàn)次數(shù)超過一半的數(shù)字
旋轉(zhuǎn)數(shù)組的最小數(shù)字
和為 s 的兩個(gè)數(shù)字 vs 和為 s 的連續(xù)正數(shù)序列
把數(shù)組排成最小的數(shù)
二叉樹的下一個(gè)結(jié)點(diǎn)
不用加減乘除做加法
第一個(gè)只出現(xiàn)一次的字符
二叉樹的深度
二叉搜索樹的第 k 個(gè)結(jié)點(diǎn)
翻轉(zhuǎn)單詞順序 vs 左旋轉(zhuǎn)字符串
用兩個(gè)棧實(shí)現(xiàn)隊(duì)列
按之字形順序打印二叉樹
矩陣中的路徑
刪除鏈表中重復(fù)的結(jié)點(diǎn)
圓圈中最后剩下的數(shù)字(約瑟夫環(huán)問題)
順時(shí)針打印矩陣
撲克牌的順子
二進(jìn)制中 1 的個(gè)數(shù)
n 個(gè)鍛子的點(diǎn)數(shù)
數(shù)字在排序數(shù)組中出現(xiàn)的次數(shù)
正則表達(dá)式匹配
機(jī)器人的運(yùn)動(dòng)范圍
重建二叉樹
替換空格
數(shù)組中重復(fù)的數(shù)字
打印 1 到最大的 n 位數(shù)
字符串的排列
斐波那契數(shù)列
連續(xù)子數(shù)組的最大和
在 O(1)時(shí)間刪除鏈表結(jié)點(diǎn)
棧的壓入、彈出序列
把字符串轉(zhuǎn)換成整數(shù)
包含 min 函數(shù)的錢

二叉樹的深度

題目一:輸入一棵二叉樹的根結(jié)點(diǎn),求該樹的深度。從根結(jié)點(diǎn)到葉子點(diǎn)依次經(jīng)過的結(jié)點(diǎn)(含根、葉結(jié)點(diǎn))形成樹的一條路徑,最長(zhǎng)路徑的長(zhǎng)度為樹的深度。

二叉樹的結(jié)點(diǎn)定義

private static class BinaryTreeNode {
    int val;
    BinaryTreeNode left;
    BinaryTreeNode right;
    public BinaryTreeNode() {
    }
    public BinaryTreeNode(int val) {
        this.val = val;
    }
}

解題思路

如果一棵樹只有一個(gè)結(jié)點(diǎn),它的深度為。 如果根結(jié)點(diǎn)只有左子樹而沒有右子樹, 那么樹的深度應(yīng)該是其左子樹的深度加 1,同樣如果根結(jié)點(diǎn)只有右子樹而沒有左子樹,那么樹的深度應(yīng)該是其右子樹的深度加 1。如果既有右子樹又有左子樹, 那該樹的深度就是其左、右子樹深度的較大值再加 1。 比如在圖 6.1 的二叉樹中,根結(jié)點(diǎn)為 1 的樹有左右兩個(gè)子樹,其左右子樹的根結(jié)點(diǎn)分別為結(jié)點(diǎn) 2 和 3。根結(jié)點(diǎn)為 2 的左子樹的深度為 3, 而根結(jié)點(diǎn)為 3 的右子樹的深度為 2,因此根結(jié)點(diǎn)為 1 的樹的深度就是 4 。

這個(gè)思路用遞歸的方法很容易實(shí)現(xiàn), 只儒對(duì)遍歷的代碼稍作修改即可。

http://wiki.jikexueyuan.com/project/for-offer/images/56.png" alt="" />

代碼實(shí)現(xiàn)

public static int treeDepth(BinaryTreeNode root) {
    if (root == null) {
        return 0;
    }
    int left = treeDepth(root.left);
    int right = treeDepth(root.right);
    return left > right ? (left + 1) : (right + 1);
}

題目二:輸入一棵二叉樹的根結(jié)點(diǎn),判斷該樹是不是平衡二叉樹。如果某二叉樹中任意結(jié)點(diǎn)的左右子樹的深度相差不超過 1 ,那么它就是一棵平衡二叉樹。

解題思路

解法一:需要重蟹遍歷結(jié)點(diǎn)多次的解法

在遍歷樹的每個(gè)結(jié)點(diǎn)的時(shí)候,調(diào)用函數(shù) treeDepth 得到它的左右子樹的深度。如果每個(gè)結(jié)點(diǎn)的左右子樹的深度相差都不超過 1 ,按照定義它就是一棵平衡的二叉樹。

public static boolean isBalanced(BinaryTreeNode root) {
    if (root == null) {
        return true;
    }
    int left = treeDepth(root.left);
    int right = treeDepth(root.right);
    int diff = left - right;
    if (diff > 1 || diff < -1) {
        return false;
    }
    return isBalanced(root.left) && isBalanced(root.right);
}

解法二:每個(gè)結(jié)點(diǎn)只遍歷一次的解法

用后序遍歷的方式遍歷二叉樹的每一個(gè)結(jié)點(diǎn),在遍歷到一個(gè)結(jié)點(diǎn)之前我們就已經(jīng)遍歷了它的左右子樹。只要在遍歷每個(gè)結(jié)點(diǎn)的時(shí)候記錄它的深度(某一結(jié)點(diǎn)的深度等于它到葉節(jié)點(diǎn)的路徑的長(zhǎng)度),我們就可以一邊遍歷一邊判斷每個(gè)結(jié)點(diǎn)是不是平衡的。

/**
 * 判斷是否是平衡二叉樹,第二種解法
 *
 * @param root
 * @return
 */
public static boolean isBalanced2(BinaryTreeNode root) {
    int[] depth = new int[1];
    return isBalancedHelper(root, depth);
}
public static boolean isBalancedHelper(BinaryTreeNode root, int[] depth) {
    if (root == null) {
        depth[0] = 0;
        return true;
    }
    int[] left = new int[1];
    int[] right = new int[1];
    if (isBalancedHelper(root.left, left) && isBalancedHelper(root.right, right)) {
        int diff = left[0] - right[0];
        if (diff >= -1 && diff <= 1) {
            depth[0] = 1 + (left[0] > right[0] ? left[0] : right[0]);
            return true;
        }
    }
    return false;
}

完整代碼

public class Test39 {
    private static class BinaryTreeNode {
        int val;
        BinaryTreeNode left;
        BinaryTreeNode right;
        public BinaryTreeNode() {
        }
        public BinaryTreeNode(int val) {
            this.val = val;
        }
    }
    public static int treeDepth(BinaryTreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = treeDepth(root.left);
        int right = treeDepth(root.right);
        return left > right ? (left + 1) : (right + 1);
    }
    /**
     * 判斷是否是平衡二叉樹,第一種解法
     *
     * @param root
     * @return
     */
    public static boolean isBalanced(BinaryTreeNode root) {
        if (root == null) {
            return true;
        }
        int left = treeDepth(root.left);
        int right = treeDepth(root.right);
        int diff = left - right;
        if (diff > 1 || diff < -1) {
            return false;
        }
        return isBalanced(root.left) && isBalanced(root.right);
    }
    /**
     * 判斷是否是平衡二叉樹,第二種解法
     *
     * @param root
     * @return
     */
    public static boolean isBalanced2(BinaryTreeNode root) {
        int[] depth = new int[1];
        return isBalancedHelper(root, depth);
    }
    public static boolean isBalancedHelper(BinaryTreeNode root, int[] depth) {
        if (root == null) {
            depth[0] = 0;
            return true;
        }
        int[] left = new int[1];
        int[] right = new int[1];
        if (isBalancedHelper(root.left, left) && isBalancedHelper(root.right, right)) {
            int diff = left[0] - right[0];
            if (diff >= -1 && diff <= 1) {
                depth[0] = 1 + (left[0] > right[0] ? left[0] : right[0]);
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
    }
    // 完全二叉樹
    //             1
    //         /      \
    //        2        3
    //       /\       / \
    //      4  5     6   7
    private static void test1() {
        BinaryTreeNode n1 = new BinaryTreeNode(1);
        BinaryTreeNode n2 = new BinaryTreeNode(1);
        BinaryTreeNode n3 = new BinaryTreeNode(1);
        BinaryTreeNode n4 = new BinaryTreeNode(1);
        BinaryTreeNode n5 = new BinaryTreeNode(1);
        BinaryTreeNode n6 = new BinaryTreeNode(1);
        BinaryTreeNode n7 = new BinaryTreeNode(1);
        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n2.right = n5;
        n3.left = n6;
        n3.right = n7;
        System.out.println(isBalanced(n1));
        System.out.println(isBalanced2(n1));
        System.out.println("----------------");
    }
    // 不是完全二叉樹,但是平衡二叉樹
    //             1
    //         /      \
    //        2        3
    //       /\         \
    //      4  5         6
    //        /
    //       7
    private static void test2() {
        BinaryTreeNode n1 = new BinaryTreeNode(1);
        BinaryTreeNode n2 = new BinaryTreeNode(1);
        BinaryTreeNode n3 = new BinaryTreeNode(1);
        BinaryTreeNode n4 = new BinaryTreeNode(1);
        BinaryTreeNode n5 = new BinaryTreeNode(1);
        BinaryTreeNode n6 = new BinaryTreeNode(1);
        BinaryTreeNode n7 = new BinaryTreeNode(1);
        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n2.right = n5;
        n5.left = n7;
        n3.right = n6;
        System.out.println(isBalanced(n1));
        System.out.println(isBalanced2(n1));
        System.out.println("----------------");
    }
    // 不是平衡二叉樹
    //             1
    //         /      \
    //        2        3
    //       /\
    //      4  5
    //        /
    //       7
    private static void test3() {
        BinaryTreeNode n1 = new BinaryTreeNode(1);
        BinaryTreeNode n2 = new BinaryTreeNode(1);
        BinaryTreeNode n3 = new BinaryTreeNode(1);
        BinaryTreeNode n4 = new BinaryTreeNode(1);
        BinaryTreeNode n5 = new BinaryTreeNode(1);
        BinaryTreeNode n6 = new BinaryTreeNode(1);
        BinaryTreeNode n7 = new BinaryTreeNode(1);
        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n2.right = n5;
        n5.left = n7;
        System.out.println(isBalanced(n1));
        System.out.println(isBalanced2(n1));
        System.out.println("----------------");
    }
    //               1
    //              /
    //             2
    //            /
    //           3
    //          /
    //         4
    //        /
    //       5
    private static void test4() {
        BinaryTreeNode n1 = new BinaryTreeNode(1);
        BinaryTreeNode n2 = new BinaryTreeNode(1);
        BinaryTreeNode n3 = new BinaryTreeNode(1);
        BinaryTreeNode n4 = new BinaryTreeNode(1);
        BinaryTreeNode n5 = new BinaryTreeNode(1);
        BinaryTreeNode n6 = new BinaryTreeNode(1);
        BinaryTreeNode n7 = new BinaryTreeNode(1);
        n1.left = n2;
        n2.left = n3;
        n3.left = n4;
        n4.left = n5;
        System.out.println(isBalanced(n1));
        System.out.println(isBalanced2(n1));
        System.out.println("----------------");
    }
    // 1
    //  \
    //   2
    //    \
    //     3
    //      \
    //       4
    //        \
    //         5
    private static void test5() {
        BinaryTreeNode n1 = new BinaryTreeNode(1);
        BinaryTreeNode n2 = new BinaryTreeNode(1);
        BinaryTreeNode n3 = new BinaryTreeNode(1);
        BinaryTreeNode n4 = new BinaryTreeNode(1);
        BinaryTreeNode n5 = new BinaryTreeNode(1);
        BinaryTreeNode n6 = new BinaryTreeNode(1);
        BinaryTreeNode n7 = new BinaryTreeNode(1);
        n1.right = n2;
        n2.right = n3;
        n3.right = n4;
        n4.right = n5;
        System.out.println(isBalanced(n1));
        System.out.println(isBalanced2(n1));
        System.out.println("----------------");
    }
}

運(yùn)行結(jié)果

http://wiki.jikexueyuan.com/project/for-offer/images/57.png" alt="" />