Anyone can point out why I did wrong? thanks


  • 0
    O
    class Solution {
    public:
        bool isSameTree(TreeNode* p, TreeNode* q) {
                if(p==q)
                return true;
                if(p==NULL||q==NULL)
                return false;
                if(p!=NULL&&q!=NULL){
                      if(q->val!=p->val){
                        return false;
                    isSameTree(p->left,q->left);
                     isSameTree(p->right,q->right);
                  
                    }
                   
                
            
                }   
                return true; 
        }
    };

  • 0
    C
    1. Don't need the NULL check if(p!=NULL&&q!=NULL) since you already do that earlier.
    2. You are not using the return values of the recursive calls to isSameTree() and returning true each time.

  • 0
    I

    The trees are the same if their values, left subtrees, and right subtrees are all equal


Log in to reply
 

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