<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>perorder Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/perorder/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/perorder/</link>
	<description></description>
	<lastBuildDate>Sat, 14 Jul 2018 19:22:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>perorder Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/perorder/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 429. N-ary Tree Level Order Traversal</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-429-n-ary-tree-level-order-traversal/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-429-n-ary-tree-level-order-traversal/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 14 Jul 2018 19:22:28 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[level]]></category>
		<category><![CDATA[n-ary]]></category>
		<category><![CDATA[perorder]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3140</guid>

					<description><![CDATA[<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>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-429-n-ary-tree-level-order-traversal/">花花酱 LeetCode 429. N-ary Tree Level Order Traversal</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Problem</h1>
<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>
<p>For example, given a <code>3-ary</code> tree:</p>
<p>&nbsp;</p>
<p><img src="https://leetcode.com/static/images/problemset/NaryTreeExample.png" width="40%" height="40%" /></p>
<p>&nbsp;</p>
<p>We should return its level order traversal:</p>
<pre class="crayon:false">[
     [1],
     [3,2,4],
     [5,6]
]
</pre>
<p><b>Note:</b></p>
<ol>
<li>The depth of the tree is at most <code>1000</code>.</li>
<li>The total number of nodes is at most <code>5000</code>.</li>
</ol>
<h1><strong>Solution1: Recursion</strong></h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(n)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 44 ms
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; levelOrder(Node* root) {
    vector&lt;vector&lt;int&gt;&gt; ans;
    preorder(root, 0, ans);
    return ans;
  }
private:
  void preorder(Node* root, int d, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {
    if (root == nullptr) return;
    while (ans.size() &lt;= d) ans.push_back({});
    ans[d].push_back(root-&gt;val);
    for (auto child : root-&gt;children)
      preorder(child, d + 1, ans);
  }
};</pre><p></p>
<h1><strong>Solution2: Iterative</strong></h1>
<p></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 44 ms
class Solution {
public:
  vector&lt;vector&lt;int&gt;&gt; levelOrder(Node* root) {
    if (!root) return {};
    vector&lt;vector&lt;int&gt;&gt; ans;    
    queue&lt;Node*&gt; q;
    q.push(root);
    int depth = 0;
    while (!q.empty()) {
      int size = q.size();
      ans.push_back({});
      while (size--) {
        Node* n = q.front(); q.pop();
        ans[depth].push_back(n-&gt;val);
        for (auto child : n-&gt;children)
          q.push(child);
      }
      ++depth;
    }
    return ans;
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-429-n-ary-tree-level-order-traversal/">花花酱 LeetCode 429. N-ary Tree Level Order Traversal</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/tree/leetcode-429-n-ary-tree-level-order-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
