# 相同的树

<p>给你两棵二叉树的根节点 <code>p</code> 和 <code>q</code> ，编写一个函数来检验这两棵树是否相同。</p><p>如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/f57f91797d38f7989b2840df0af56f12.jpeg" 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://img-service.csdnimg.cn/img_convert/3cd39cd15154d94a0916631a1bb0af62.jpeg" 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://img-service.csdnimg.cn/img_convert/ec8eb306b8b154439c9322477d9b13bf.jpeg" 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

```python
class TreeNode(object):
	def __init__(self, x):
		self.val = x
		self.left = None
		self.right = None
class Solution(object):
	def isSameTree(self, p, q):
		"""
		:type p: TreeNode
		:type q: TreeNode
		:rtype: bool
		"""
		if p == q:
			return True
		try:
			left = right = True
			if p.val == q.val:
				left = self.isSameTree(p.left, q.left)
				right = self.isSameTree(p.right, q.right)
				return (left and right)
		except:
			return False
		return False
# %%
s = Solution()
print(s.isSameTree(p = [1,2,3], q = [1,2,3]))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```