Same Tree

class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(!p&&!q) return true;
        if(!p&&q||p&&!q||p->val!=q->val) return false;
        
        bool left=isSameTree(p->left,q->left);
        bool right=isSameTree(p->right,q->right);
        
        return left&&right;
    }
};

你可能感兴趣的:(Same Tree)