[leetcode] 190. Reverse Bits 解题报告

递归,注意结果的三重判断

public boolean isBalanced(TreeNode root) {
        if (root==null) return true;
        return Math.abs(getHeight(root.left)-getHeight(root.right)) <=1 && isBalanced(root.left) && isBalanced(root.right);
    }

    public int getHeight(TreeNode node){
        if (node==null){
            return 0;
        }
        if (node.right == null && node.left ==null){
            return 1;
        }
        return Math.max(getHeight(node.left),getHeight(node.right))+1;
    }

 

posted @ 2017-10-31 14:46  pulusite  阅读(101)  评论(0编辑  收藏  举报