劍指Offer學習-面試題55:二叉樹的深度

	/**
     * 二叉樹的深度
     *
     * @param root
     * @return
     */
    public int treeDepth(TreeNode root) {
        if (null == root) return 0;
        int left = treeDepth(root.left);
        int right = treeDepth(root.right);
        return Math.max(left, right) + 1;
    }


    /**
     * 判斷一顆樹是不是平衡二叉樹(暴力)
     *
     * @param root
     * @return
     */
    public boolean isBalanced(TreeNode root) {
        if (null == root) return true;
        if (Math.abs(treeDepth(root.left) - treeDepth(root.right)) >= 2) return false;
        return isBalanced(root.left) && isBalanced(root.right);
    }


    /**
     * 判斷一顆樹是不是平衡二叉樹(遍歷一次)
     *
     * @param root
     * @return
     */
    public boolean isBalanced1(TreeNode root) {
        return process(root).b;
    }

    private Result process(TreeNode root) {
        if (null == root) return new Result(0, true);
        Result left = process(root.left);
        Result right = process(root.right);
        if (left.b && right.b) {
            int abs = Math.abs(left.depth - right.depth);
            if (abs >= 2) return new Result(0, false);
            return new Result(Math.max(left.depth, right.depth) + 1, true);
        }
        return new Result(0, false);
    }

    class Result {
        int depth;
        boolean b;

        public Result(int depth, boolean b) {
            this.depth = depth;
            this.b = b;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章