{"id":3244,"date":"2018-07-21T22:56:43","date_gmt":"2018-07-22T05:56:43","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3244"},"modified":"2018-07-21T23:01:34","modified_gmt":"2018-07-22T06:01:34","slug":"leetcode-872-leaf-similar-trees","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-872-leaf-similar-trees\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 872. Leaf-Similar Trees"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Consider all the leaves of a binary tree.\u00a0 From\u00a0left to right order, the values of those\u00a0leaves form a\u00a0<em>leaf value sequence.<\/em><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/s3-lc-upload.s3.amazonaws.com\/uploads\/2018\/07\/16\/tree.png\" alt=\"\" \/><\/p>\n<p>For example, in the given tree above, the leaf value sequence is\u00a0<code>(6, 7, 4, 9, 8)<\/code>.<\/p>\n<p>Two binary trees are considered\u00a0<em>leaf-similar<\/em>\u00a0if their leaf value sequence is the same.<\/p>\n<p>Return\u00a0<code>true<\/code>\u00a0if and only if the two given trees with head nodes\u00a0<code>root1<\/code>\u00a0and\u00a0<code>root2<\/code>\u00a0are leaf-similar.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>Both of the given trees will have between\u00a0<code>1<\/code>\u00a0and\u00a0<code>100<\/code>\u00a0nodes.<\/li>\n<\/ul>\n<h1><strong>Solution: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n1 + n2)<\/p>\n<p>Space complexity: O(n1 + n2)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  bool leafSimilar(TreeNode* root1, TreeNode* root2) {\r\n    vector&lt;int&gt; leafs1;\r\n    vector&lt;int&gt; leafs2;\r\n    getLeafs(root1, leafs1);\r\n    getLeafs(root2, leafs2);\r\n    return leafs1 == leafs2;\r\n  }\r\nprivate:\r\n  void getLeafs(TreeNode* root, vector&lt;int&gt;&amp; leafs) {\r\n    if (root == nullptr) return;\r\n    if (root-&gt;left == nullptr &amp;&amp; root-&gt;right == nullptr)\r\n      leafs.push_back(root-&gt;val);\r\n    getLeafs(root-&gt;left, leafs);\r\n    getLeafs(root-&gt;right, leafs);\r\n  }\r\n};<\/pre>\n<p>Python<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 24 ms\r\n\"\"\"\r\nclass Solution(object):\r\n  def leafSimilar(self, root1, root2):\r\n    def getLeafs(root):\r\n      if not root: return []\r\n      if not root.left and not root.right: return [root.val]\r\n      return getLeafs(root.left) + getLeafs(root.right)\r\n    return getLeafs(root1) == getLeafs(root2)<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Consider all the leaves of a binary tree.\u00a0 From\u00a0left to right order, the values of those\u00a0leaves form a\u00a0leaf value sequence. For example, in the&#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,28],"class_list":["post-3244","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3244","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=3244"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3244\/revisions"}],"predecessor-version":[{"id":3250,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3244\/revisions\/3250"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}