# 相同的树

<p>给你两棵二叉树的根节点 <code>p</code> 和 <code>q</code> ，编写一个函数来检验这两棵树是否相同。</p><p>如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex1.jpg" style="width: 622px; height: 182px;" /><pre><strong>输入：</strong>p = [1,2,3], q = [1,2,3]<strong><br />输出：</strong>true</pre><p><strong>示例 2：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex2.jpg" style="width: 382px; height: 182px;" /><pre><strong>输入：</strong>p = [1,2], q = [1,null,2]<strong><br />输出：</strong>false</pre><p><strong>示例 3：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0100.Same%20Tree/images/ex3.jpg" style="width: 622px; height: 182px;" /><pre><strong>输入：</strong>p = [1,2,1], q = [1,1,2]<strong><br />输出：</strong>false</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>两棵树上的节点数目都在范围 <code>[0, 100]</code> 内</li>	<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li></ul>

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
	bool isSameTree(TreeNode *p, TreeNode *q)
	{
		if (p == nullptr && q == nullptr)
		{
			return true;
		}
		if (p == nullptr || q == nullptr)
		{
			return false;
		}
		if (p->val != q->val)
		{
			return false;
		}
		return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
	}
};
```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```