My c++ code (recursion)


  • 0
    D
    class Solution {
    public:
        bool isSameTree(TreeNode *p, TreeNode *q) {
            if ((!p) || (!q))
            { // at least one of them is NULL
                return (p==q);
            }
            else
            { // both are non-NULL
                if(p->val != q->val)
                { // the root node values are different
                    return false;
                }
                else
                {
                    if(!(isSameTree(p->left, q->left))) // check left subtree
                        return false;
                    else
                        return (isSameTree(p->right, q->right)); // check right subtree
                      
                }
            }
        }
    };

Log in to reply
 

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