1. 程式人生 > >劍指offer-判斷是否是平衡二叉樹

劍指offer-判斷是否是平衡二叉樹

balanced pri oot treenode col solution offer private ==

private boolean isBalanced = true;
    public boolean IsBalanced_Solution(TreeNode root) {
        height(root);
        return isBalanced;
    }
    
    public int height(TreeNode root) {
        if(root == null || !isBalanced) return 0;
        int left = height(root.left);
        int
right = height(root.right); if(Math.abs(left-right)>1) isBalanced = false; return 1+Math.max(left, right); }

劍指offer-判斷是否是平衡二叉樹