本题链接:Same Tree
本题难度:
![](https://img.haomeiwen.com/i7019336/0f4ccbcee8d5a885.png)
![](https://img.haomeiwen.com/i7019336/6cb451b3c0f0b0a1.png)
方案1:
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if((p == nullptr && q != nullptr) || (p != nullptr && q == nullptr))
return false;
if(p == nullptr && q == nullptr)
return true;
if(p->val != q->val)
return false;
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
};
时间复杂度:
空间复杂度:
网友评论