{"id":3113,"date":"2018-07-12T23:24:56","date_gmt":"2018-07-13T06:24:56","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3113"},"modified":"2018-09-04T02:14:54","modified_gmt":"2018-09-04T09:14:54","slug":"leetcode-590-n-ary-tree-postorder-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-590-n-ary-tree-postorder-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 590. N-ary Tree Postorder Traversal"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an n-ary tree, return the\u00a0<i>postorder<\/i>\u00a0traversal of its nodes&#8217; values.<\/p>\n<p>&nbsp;<\/p>\n<p>For example, given a\u00a0<code>3-ary<\/code>\u00a0tree:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/leetcode.com\/static\/images\/problemset\/NaryTreeExample.png\" width=\"40%\" height=\"40%\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Return its postorder traversal as:\u00a0<code>[5,6,3,2,4,1]<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Note:<\/b>\u00a0Recursive solution is trivial, could you do it iteratively?<\/p>\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 1: Recursive<\/strong><\/h1>\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\n\/\/ Running time: 44 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; postorder(Node* root) {\r\n    vector&lt;int&gt; ans;\r\n    postorder(root, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void postorder(Node* root, vector&lt;int&gt;&amp; ans) {\r\n    if (!root) return;\r\n    for (auto child : root-&gt;children)\r\n      postorder(child, ans);\r\n    ans.push_back(root-&gt;val);\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Iterative<\/strong><\/h1>\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\n\/\/ Running time: 44 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; postorder(Node* root) {\r\n   if (!root) return {};\r\n    vector&lt;int&gt; ans;\r\n    stack&lt;Node*&gt; s;\r\n    s.push(root);\r\n    while (!s.empty()) {\r\n      const Node* node = s.top(); s.pop();      \r\n      ans.push_back(node-&gt;val);\r\n      for (auto child : node-&gt;children)\r\n        s.push(child);      \r\n    }\r\n    reverse(begin(ans), end(ans));\r\n    return ans;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-589-n-ary-tree-preorder-traversal\/\">\u82b1\u82b1\u9171 LeetCode 589. N-ary Tree Preorder Traversal<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-145-binary-tree-postorder-traversal\/\">\u82b1\u82b1\u9171 LeetCode 145. Binary Tree Postorder Traversal<\/a><\/li>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/leetcode\/leetcode-102-binary-tree-level-order-traversal\/\">\u82b1\u82b1\u9171 LeetCode 102. Binary Tree Level Order Traversal<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an n-ary tree, return the\u00a0postorder\u00a0traversal of its nodes&#8217; values. &nbsp; For example, given a\u00a03-ary\u00a0tree: &nbsp; Return its postorder traversal as:\u00a0[5,6,3,2,4,1]. &nbsp; Note:\u00a0Recursive solution&#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,325,56,32,28],"class_list":["post-3113","post","type-post","status-publish","format-standard","hentry","category-tree","tag-easy","tag-n-ary","tag-postorder","tag-traversal","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3113","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=3113"}],"version-history":[{"count":8,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3113\/revisions"}],"predecessor-version":[{"id":3857,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3113\/revisions\/3857"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}