C++ easy to understand solution.


  • 1
    C
    bool isBalanced(TreeNode* root) {
        if (root == nullptr)
            return true;
        return isBalanced(root->left) && isBalanced(root->right) && abs(height(root->left)-height(root->right))<=1;
    }
    
    int height(TreeNode *root) {
        if (root == NULL)
            return 0;
        return max(height(root->left), height(root->right)) +1;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.