{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "3e1078cb13894e2f8d6ea870bd43f907",
  "keywords": "树,深度优先搜索,广度优先搜索,二叉树",
  "title": "相同的树",
  "desc": [
    {
      "content": "\n<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>\t<li>两棵树上的节点数目都在范围 <code>[0, 100]</code> 内</li>\t<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "public class TreeNode {\n\tint val;\n\tTreeNode left;\n\tTreeNode right;\n\n\tTreeNode() {\n\t}\n\n\tTreeNode(int val) {\n\t\tthis.val = val;\n\t}\n\n\tTreeNode(int val, TreeNode left, TreeNode right) {\n\t\tthis.val = val;\n\t\tthis.left = left;\n\t\tthis.right = right;\n\t}\n}\n\nclass Solution {\n\tpublic boolean isSameTree(TreeNode p, TreeNode q) {\n\t\tif (p == null && q == null) {\n\t\t\treturn true;\n\t\t}\n\t\tif (p != null && q != null && p.val == q.val) {\n\t\t\treturn isSameTree(p.left, q.left) && isSameTree(p.right, q.right);\n\t\t} else {\n\t\t\treturn false;\n\t\t}\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-5fc81f3203724e6bbc28969923a64495",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600469927"
}