{"id":150,"date":"2017-09-08T01:19:18","date_gmt":"2017-09-08T08:19:18","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=150"},"modified":"2018-07-10T18:35:22","modified_gmt":"2018-07-11T01:35:22","slug":"leetcode-145-binary-tree-postorder-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-145-binary-tree-postorder-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 145. Binary Tree Postorder Traversal"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 145. Binary Tree Postorder Traversal - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP40\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/A6iCX_5xiU4?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 a binary tree, return the\u00a0<i>postorder<\/i>\u00a0traversal of its nodes&#8217; values.<\/p>\n<p>For example:<br \/>\nGiven binary tree\u00a0<code>{1,#,2,3}<\/code>,<\/p>\n<pre>   1\r\n    \\\r\n     2\r\n    \/\r\n   3\r\n<\/pre>\n<p>return\u00a0<code>[3,2,1]<\/code>.<\/p>\n<p><b>Note:<\/b>\u00a0Recursive solution is trivial, could you do it iteratively?<\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-154\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-1-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-1-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-153\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-2.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-2-768x432.png 768w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/09\/145-ep40-2-624x351.png 624w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/p>\n<h1><strong>Solution 1:<\/strong><\/h1>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    vector&lt;int&gt; postorderTraversal(TreeNode* root) {\r\n        vector&lt;int&gt; ans;        \r\n        postorderTraversal(root, ans);\r\n        return ans;\r\n    }\r\n    \r\n    void postorderTraversal(TreeNode* root, vector&lt;int&gt;&amp; ans) {\r\n        if (!root) return;\r\n        postorderTraversal(root-&gt;left, ans);\r\n        postorderTraversal(root-&gt;right, ans);\r\n        ans.push_back(root-&gt;val);\r\n    }\r\n};<\/pre>\n<h1><strong>Solution 2:<\/strong><\/h1>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    vector&lt;int&gt; postorderTraversal(TreeNode* root) {\r\n        if (!root) return {};\r\n        vector&lt;int&gt; ans;\r\n        const vector&lt;int&gt; l = postorderTraversal(root-&gt;left);\r\n        const vector&lt;int&gt; r = postorderTraversal(root-&gt;right);\r\n        ans.insert(ans.end(), l.begin(), l.end());\r\n        ans.insert(ans.end(), r.begin(), r.end());\r\n        ans.push_back(root-&gt;val);\r\n        return ans;\r\n    }\r\n};<\/pre>\n<h1><strong>Solution 3:<\/strong><\/h1>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    vector&lt;int&gt; postorderTraversal(TreeNode* root) {\r\n        if (!root) return {};\r\n        deque&lt;int&gt; ans;\r\n        stack&lt;TreeNode*&gt; s;\r\n        s.push(root);\r\n        while (!s.empty()) {\r\n            TreeNode* n = s.top();\r\n            s.pop();\r\n            ans.push_front(n-&gt;val); \/\/ O(1)\r\n            if (n-&gt;left) s.push(n-&gt;left);\r\n            if (n-&gt;right) s.push(n-&gt;right);\r\n        }   \r\n        return vector&lt;int&gt;(ans.begin(), ans.end());\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Given a binary tree, return the\u00a0postorder\u00a0traversal of its nodes&#8217; values. For example: Given binary tree\u00a0{1,#,2,3}, 1 \\ 2 \/ 3 return\u00a0[3,2,1]. Note:\u00a0Recursive solution is&#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":[30,58,54,6,56,57,53,55,32],"class_list":["post-150","post","type-post","status-publish","format-standard","hentry","category-tree","tag-binary-tree","tag-inorder","tag-iterative","tag-leetcode","tag-postorder","tag-preorder","tag-recursive","tag-solutions","tag-traversal","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/150","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=150"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":3064,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/150\/revisions\/3064"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}