C++ what 's wrong with my code??


  • 0
    J

    class Solution {
    public:

    bool isBalanced(TreeNode* root) {
      
        if(root==NULL)
        {
            return true;
        }
        if((root->left == NULL)&&(root->right ==NULL))
        {
            return true;
        }
     
        else
        {   
            return height_Max(root)-height_Min(root)<2;
           
        }
      
    }
    int height_Max(TreeNode* rt)
    {
        if(rt==NULL)
        return 0;
        
        else 
        return  1+max(height_Max(rt->left),height_Max(rt->right));
       
    }
     int height_Min(TreeNode* rt1)
    {
        
        if(rt1==NULL)
        {
            return 0;
        }
        else
        {
            return 1+min(height_Min(rt1->left),height_Min(rt1->right));
        }
       
    }
    

    };


  • 0
    B
    (the depth of the two subtrees of every node never differ by more than 1) != (max_height - min_height ==1)

Log in to reply
 

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