{"id":2239,"date":"2018-03-19T21:34:25","date_gmt":"2018-03-20T04:34:25","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2239"},"modified":"2018-03-21T09:20:23","modified_gmt":"2018-03-21T16:20:23","slug":"leetcode-572-subtree-of-another-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-572-subtree-of-another-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 572. Subtree of Another Tree"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u68f5\u6811\u662f\u4e0d\u662f\u53e6\u5916\u4e00\u68f5\u6811\u7684\u5b50\u6811\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/subtree-of-another-tree\/description\/\">https:\/\/leetcode.com\/problems\/subtree-of-another-tree\/description\/<\/a><\/p>\n<p>Given two non-empty binary trees\u00a0<b>s<\/b>\u00a0and\u00a0<b>t<\/b>, check whether tree\u00a0<b>t<\/b>\u00a0has exactly the same structure and node values with a subtree of\u00a0<b>s<\/b>. A subtree of\u00a0<b>s<\/b>\u00a0is a tree consists of a node in\u00a0<b>s<\/b>\u00a0and all of this node&#8217;s descendants. The tree\u00a0<b>s<\/b>\u00a0could also be considered as a subtree of itself.<\/p>\n<p><b>Example 1:<\/b><br \/>\nGiven tree s:<\/p>\n<pre>     3\r\n    \/ \\\r\n   4   5\r\n  \/ \\\r\n 1   2\r\n<\/pre>\n<p>Given tree t:<\/p>\n<pre>   4 \r\n  \/ \\\r\n 1   2\r\n<\/pre>\n<p>Return\u00a0<b>true<\/b>, because t has the same structure and node values with a subtree of s.<\/p>\n<p><b>Example 2:<\/b><br \/>\nGiven tree s:<\/p>\n<pre>     3\r\n    \/ \\\r\n   4   5\r\n  \/ \\\r\n 1   2\r\n    \/\r\n   0\r\n<\/pre>\n<p>Given tree t:<\/p>\n<pre>   4\r\n  \/ \\\r\n 1   2\r\n<\/pre>\n<p>Return\u00a0<b>false<\/b>.<\/p>\n<h1>Solution: Recursion<\/h1>\n<p>Time complexity: O(max(n, m))<\/p>\n<p>Space complexity: O(max(n, m))<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 29 ms\r\nclass Solution {\r\npublic:\r\n  bool isSubtree(TreeNode* s, TreeNode* t) {\r\n    if (t == nullptr) return true;\r\n    if (s == nullptr) return false;\r\n    if (isSameTree(s, t)) return true;\r\n    return isSubtree(s-&gt;left, t) || isSubtree(s-&gt;right, t);\r\n  }\r\nprivate:\r\n  bool isSameTree(TreeNode* s, TreeNode* t) {\r\n    if (s == nullptr &amp;&amp; t == nullptr) return true;\r\n    if (s == nullptr || t == nullptr) return false;\r\n    return (s-&gt;val == t-&gt;val) \r\n           &amp;&amp; isSameTree(s-&gt;left, t-&gt;left) \r\n           &amp;&amp; isSameTree(s-&gt;right, t-&gt;right);\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-100-same-tree\/\">[\u89e3\u9898\u62a5\u544a] Leetcode 100. Same Tree<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-508-most-frequent-subtree-sum\/\">\u82b1\u82b1\u9171 LeetCode 508. Most Frequent Subtree Sum<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-563-binary-tree-tilt\/\">\u82b1\u82b1\u9171 LeetCode 563. Binary Tree Tilt<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u68f5\u6811\u662f\u4e0d\u662f\u53e6\u5916\u4e00\u68f5\u6811\u7684\u5b50\u6811\u3002 https:\/\/leetcode.com\/problems\/subtree-of-another-tree\/description\/ Given two non-empty binary trees\u00a0s\u00a0and\u00a0t, check whether tree\u00a0t\u00a0has exactly the same structure and node values with a subtree of\u00a0s. A subtree of\u00a0s\u00a0is&#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,225],"class_list":["post-2239","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-recursion","tag-same-tree","tag-subtree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2239","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=2239"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2239\/revisions"}],"predecessor-version":[{"id":2273,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2239\/revisions\/2273"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}