MY simple CPP answer


  • 0
    N
    int height(TreeNode* root){
        if(root == NULL){
            return 0;
        }
        int a = height(root->left);
        int b = height(root->right);
        return a  > b  ? 1 + a : 1+ b;
    }
    
    bool isBalanced(TreeNode* root) {
        
        if(root == NULL){
            return true;
        }
        
       int diff = height(root->left) -  height(root->right);
       
       diff = diff > 0 ? diff : -diff;
       
       if ( diff > 1){
           return false;
       }
        
        return isBalanced(root->left) && isBalanced(root->right);
        
    }

Log in to reply
 

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