{"id":3140,"date":"2018-07-14T12:22:28","date_gmt":"2018-07-14T19:22:28","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3140"},"modified":"2018-07-14T12:22:50","modified_gmt":"2018-07-14T19:22:50","slug":"leetcode-429-n-ary-tree-level-order-traversal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-429-n-ary-tree-level-order-traversal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 429. N-ary Tree Level Order Traversal"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given an n-ary tree, return the level order traversal of its nodes&#8217; values. (ie, from left to right, level by level).<\/p>\n<p>For example, given a\u00a0<code>3-ary<\/code>\u00a0tree:<\/p>\n<p>&nbsp;<\/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>We should return its level order traversal:<\/p>\n<pre class=\"crayon:false\">[\r\n     [1],\r\n     [3,2,4],\r\n     [5,6]\r\n]\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The depth of the tree is at most\u00a0<code>1000<\/code>.<\/li>\n<li>The total number of nodes is at most\u00a0<code>5000<\/code>.<\/li>\n<\/ol>\n<h1><strong>Solution1: Recursion<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 44 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; levelOrder(Node* root) {\r\n    vector&lt;vector&lt;int&gt;&gt; ans;\r\n    preorder(root, 0, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void preorder(Node* root, int d, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {\r\n    if (root == nullptr) return;\r\n    while (ans.size() &lt;= d) ans.push_back({});\r\n    ans[d].push_back(root-&gt;val);\r\n    for (auto child : root-&gt;children)\r\n      preorder(child, d + 1, ans);\r\n  }\r\n};<\/pre>\n<h1><strong>Solution2: Iterative<\/strong><\/h1>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 44 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; levelOrder(Node* root) {\r\n    if (!root) return {};\r\n    vector&lt;vector&lt;int&gt;&gt; ans;    \r\n    queue&lt;Node*&gt; q;\r\n    q.push(root);\r\n    int depth = 0;\r\n    while (!q.empty()) {\r\n      int size = q.size();\r\n      ans.push_back({});\r\n      while (size--) {\r\n        Node* n = q.front(); q.pop();\r\n        ans[depth].push_back(n-&gt;val);\r\n        for (auto child : n-&gt;children)\r\n          q.push(child);\r\n      }\r\n      ++depth;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an n-ary tree, return the level order traversal of its nodes&#8217; values. (ie, from left to right, level by level). For example, given&#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":[329,325,328,28],"class_list":["post-3140","post","type-post","status-publish","format-standard","hentry","category-tree","tag-level","tag-n-ary","tag-perorder","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3140","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=3140"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3140\/revisions"}],"predecessor-version":[{"id":3142,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3140\/revisions\/3142"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}