Very clean solution yet 12ms as the best in C++


  • 2
    class Solution {
    private:
        int height(TreeNode* root)
        {
            if(!root) return 0;
            int l = height(root->left), r = height(root->right);
            if(l==-1 || r==-1 || abs(l-r)>1) return -1;
            return max(l, r)+1;
        }
    public:
        bool isBalanced(TreeNode* root) 
        {
            return height(root) != -1;
        }
    };
    

Log in to reply
 

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