O(N) with one extra flag.


  • 0
    S

    O(N) with one extra flag.

         bool isBalanced(TreeNode* root){
            bool flag=true;
            helper(root,flag);
            return flag;
          }
    
          int helper(TreeNode* root, bool& flag){
            if(!root) return 0;
            int lf = helper(root->left, flag)+1;
            int rg = helper(root->right, flag)+1;
            if(abs(lf-rg)>1) flag = false;
            return max(lf, rg);
          }
    
    

Log in to reply
 

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