Straight forward 12 ms C++


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

Log in to reply
 

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