# 相同的树

<p>给你两棵二叉树的根节点 <code>p</code> 和 <code>q</code> ，编写一个函数来检验这两棵树是否相同。</p><p>如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。</p><p><strong>示例 1：</strong></p><img alt="" src="https://img-blog.csdnimg.cn/img_convert/f57f91797d38f7989b2840df0af56f12.png#pic_center" /><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-blog.csdnimg.cn/img_convert/3cd39cd15154d94a0916631a1bb0af62.png#pic_center" /><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-blog.csdnimg.cn/img_convert/ec8eb306b8b154439c9322477d9b13bf.png#pic_center" /><pre><strong>输入：</strong>p = [1,2,1], q = [1,1,2]<strong><br />输出：</strong>false</pre><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>
<p>以下<span style="color:red">错误</span>的选项是？</p>

## aop

### before

```c
#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) {}
};

```

### after

```c

```

## 答案

```c
都是错的
```
## 选项


### A

```c
class Solution
{
public:
    bool isSameTree(TreeNode *p, TreeNode *q)
    {
        if (p == nullptr)
            return q == nullptr;
        return q != nullptr && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
};
```

### B

```c
class Solution
{
public:
    bool isSameTree(TreeNode *p, TreeNode *q)
    {
        TreeNode *temp, *temq;
        temp = p;
        temq = q;
        bool lb, rb;
        if (!p && !q)
            return true;
        if ((!p || !q) || p->val != q->val)
            return false;
        else
        {
            lb = isSameTree(p->left, q->left);
            rb = isSameTree(p->right, q->right);
        }
        if (lb && rb)
            return true;
        else
            return false;
    }
};
```

### C

```c
class Solution
{
public:
    bool isSameTree(TreeNode *p, TreeNode *q)
    {
        if (p == NULL && q == NULL)
            return true;
        if (p == NULL || q == NULL)
            return false;
        if (p->val != q->val)
            return false;
        return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
    }
};
```
