My c code , 13 lines, 6ms.


  • 0
    8
    int getDeep(struct TreeNode* p){
        int l = p->left?getDeep(p->left):0;
        int r = p->right?getDeep(p->right):0;
        if(l ==-1||r == -1||l-r<-1||l-r>1) return -1;
        return (l>r?l:r)+1;
    }
    bool isBalanced(struct TreeNode *root) {
        if(!root) return true;
        if(getDeep(root) == -1) return false;
        return true;
    }

Log in to reply
 

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