C++, 12ms, DFS, 8 lines of code


  • 0
    int depth(TreeNode* root) {
        if (root==NULL) return 1;
        int left = depth(root->left);
        int right = depth(root->right);
        if (left==0 || right==0 || abs(left-right)>1) return 0;
        return 1+max(left,right);
    }
    bool isBalanced(TreeNode* root) {
        return depth(root);
    }

Log in to reply
 

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