My C++ solution


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

  • 1
    H

    I think there is no need for
    || (p->left == NULL && q->left != NULL) || (p->left != NULL && q->left == NULL)

    NULL can be detect by 1st 2nd if clauses


Log in to reply
 

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