110. Balanced Binary Tree

原题链接:https://leetcode.com/problems/balanced-binary-tree/description/
判断一棵树是否是二叉树,本身不难,但是想要写出高效简洁的代码还是有点难度的:

/**
 * Created by clearbug on 2018/2/26.
 */
public class Solution {

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        Solution s = new Solution();

        // test1
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(s.height(root));
        System.out.println(s.isBalanced(root));
        System.out.println(s.height2(root));
        System.out.println(s.isBalanced2(root));

        // test2
        TreeNode root2 = new TreeNode(1);
        root2.left = new TreeNode(2);
        root2.right = new TreeNode(2);
        root2.left.left = new TreeNode(3);
        root2.left.right = new TreeNode(3);
        root2.left.left.left = new TreeNode(4);
        root2.left.left.right = new TreeNode(4);
        System.out.println(s.height(root2));
        System.out.println(s.isBalanced(root2));
        System.out.println(s.height2(root2));
        System.out.println(s.isBalanced2(root2));

        // test3
        TreeNode root3 = new TreeNode(1);
        root3.left = new TreeNode(2);
        root3.right = new TreeNode(2);
        root3.left.left = new TreeNode(3);
        root3.right.right = new TreeNode(3);
        root3.left.left.left = new TreeNode(4);
        root3.right.right.right = new TreeNode(4);
        System.out.println(s.height(root3));
        System.out.println(s.isBalanced(root3));
        System.out.println(s.height2(root3));
        System.out.println(s.isBalanced2(root3));
    }

    /**
     * 方法一:最普通的方法,就是对比左右子树的高度看是否符合平衡二叉树
     *
     * @param root
     * @return
     */
    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);
        return Math.abs(leftHeight - rightHeight) < 2 && isBalanced(root.left) && isBalanced(root.right);
    }

    private int height(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = height(root.left);
        int rightHeight = height(root.right);

        return Math.max(leftHeight, rightHeight) + 1;
    }

    /**
     * 方法二:在方法一的基础上进行优化,从底部向上依次遍历是否符合平衡二叉树
     *
     * @param root
     * @return
     */
    public boolean isBalanced2(TreeNode root) {
        return height2(root) != -1;
    }

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

}
posted @ 2018-03-16 19:20  optor  阅读(100)  评论(0编辑  收藏  举报