Cleaner 12ms C++ solution :)


  • 0
    S
    class Solution {
    public:
    int depth(TreeNode* root){
        return root?max(depth(root->left),depth(root->right))+1:0;
    }
    bool isBalanced(TreeNode* root){
        return root?abs(depth(root->left)-depth(root->right))<=1&&isBalanced(root->left)&&isBalanced(root->right):1;
    }
    

    };


  • 2
    J

    i think its better to store the depth. your time complexity is O(n2)


Log in to reply
 

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