判断一颗二叉树是不是平衡树

public class IsBalanceTree {

    private class Node {
        Node left;
        Node right;
    }

    public boolean isBalanceTree(Node node) {
        if(null == node ) return true;
        if(isBalanceTree(node.left)
            && isBalanceTree(node.right)
            && Math.abs(height(node.left)  - height(node.right)) <= 1) {
            return true;
        }
        return false;
    }

    private int height(Node node) {
        if(null == node ) return 0;
        return 1 + Math.abs(height(node.left) - height(node.right));
    }

} 

你可能感兴趣的:(判断一颗二叉树是不是平衡树)