My c code,1ms.


  • 0
    8
    bool isSameTree(struct TreeNode *p, struct TreeNode *q) {
        if(!p && !q) return true;
        if(!p || !q) return false;
        if(isSameTree(p->left,q->left)&&isSameTree(p->right,q->right) && p->val==q->val){
           return true; 
        }
        return false;
    }

  • 0
    L
        bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
        if(p == NULL && q == NULL) return true;
        else
        {
            if(p != NULL && q != NULL) return((p -> val == q -> val) && isSameTree(p -> left, q -> left) && isSameTree(p -> right, q -> right));
            else{
                return false;
            }
        }
    }

  • 0
    L

    return (isSameTree(p->left,q->left)&&isSameTree(p->right,q->right) && p->val==q->val);
    instead of the last 4 lines


Log in to reply
 

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