LeetCode 110. Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.


Just follow the definition.....

    static int getDepth(TreeNode* root) {
        if(!root) return 0;
        return 1 + max(getDepth(root->left), getDepth(root->right));
    }
    bool isBalanced(TreeNode* root) {
        if(!root) return true;
        int leftDepth = getDepth(root->left);
        int rightDepth = getDepth(root->right);
        return abs(leftDepth - rightDepth) <= 1 && isBalanced(root->right) && isBalanced(root->left);
    }

你可能感兴趣的:(LeetCode 110. Balanced Binary Tree)