My 3 lines C++ solution


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

  • 0
    R

    I useif (p == q) return true; instead of if(p == NULL && q == NULL)return true;
    That may be more effective when those trees have completely same node.


Log in to reply
 

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