leetcode 0404 二叉树检查平衡性 DFS

public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int diff = getDepth(root.left) - getDepth(root.right);
        diff = diff > 0 ? diff : diff * -1;
        if((diff <= 1)){return false;}
        return isBalanced(root.right) && isBalanced(root.left);
    }

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

 

posted @ 2020-07-15 17:01  牛有肉  阅读(140)  评论(0编辑  收藏  举报