{"id":45,"date":"2017-09-02T16:42:37","date_gmt":"2017-09-02T23:42:37","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=45"},"modified":"2018-12-15T01:14:54","modified_gmt":"2018-12-15T09:14:54","slug":"leetcode-100-same-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-100-same-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 Leetcode 100. Same Tree"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 100. Same Tree - \u5237\u9898\u627e\u5de5\u4f5c EP 27\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/VJVQpqPRptM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem:<\/strong><\/h1>\n<p>Given two binary trees, write a function to check if they are the same or not.<\/p>\n<p>Two binary trees are considered the same if they are structurally identical and the nodes have the same value.<\/p>\n<h2><b>Example 1:<\/b><\/h2>\n<pre class=\"crayon:false\">Input:     1         1\r\n          \/ \\       \/ \\\r\n         2   3     2   3\r\n\r\n        [1,2,3],   [1,2,3]\r\n\r\nOutput: true\r\n<\/pre>\n<h2><b>Example 2:<\/b><\/h2>\n<pre class=\"crayon:false\">Input:     1         1\r\n          \/           \\\r\n         2             2\r\n\r\n        [1,2],     [1,null,2]\r\n\r\nOutput: false\r\n<\/pre>\n<h2><b>Example 3:<\/b><\/h2>\n<pre class=\"crayon:false\">Input:     1         1\r\n          \/ \\       \/ \\\r\n         2   1     1   2\r\n\r\n        [1,2,1],   [1,1,2]\r\n\r\nOutput: false\r\n<\/pre>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    bool isSameTree(TreeNode* p, TreeNode* q) {\r\n        \/\/ Both are emtry: same\r\n        if (!p &amp;&amp; !q) return true;\r\n        \/\/ One is emtry: not the Same\r\n        if (!p || !q) return false;\r\n        \/\/ Both are not emptry, compare the root value\r\n        if (p-&gt;val != q-&gt;val) return false;\r\n        \/\/ Compare left-subtree and right-subtree recursively.\r\n        return isSameTree(p-&gt;left, q-&gt;left) \r\n            &amp;&amp; isSameTree(p-&gt;right, q-&gt;right); \r\n    }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\n  public boolean isSameTree(TreeNode p, TreeNode q) {\r\n    if (p == null &amp;&amp; q == null) return true;\r\n    if (p == null || q == null) return false;\r\n    return p.val == q.val &amp;&amp; isSameTree(p.left, q.left) &amp;&amp; isSameTree(p.right, q.right);\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\n\"\"\"\r\nclass Solution:\r\n  def isSameTree(self, p, q):\r\n    if not p and not q: return True\r\n    if not p or not q: return False\r\n    return all((p.val == q.val, \r\n               self.isSameTree(p.left, q.left), \r\n               self.isSameTree(p.right, q.right)))<\/pre>\n<\/div><\/div>\n<h1><strong>Follow Up<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-572-subtree-of-another-tree\/\">\u82b1\u82b1\u9171 LeetCode 572. Subtree of Another Tree<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[222,17,264,28],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-recursion","tag-same-tree","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":4436,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/45\/revisions\/4436"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}