<?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>level order Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/level-order/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/level-order/</link>
	<description></description>
	<lastBuildDate>Mon, 29 Nov 2021 08:37:34 +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>level order Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/level-order/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 117. Populating Next Right Pointers in Each Node II</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-117-populating-next-right-pointers-in-each-node-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-117-populating-next-right-pointers-in-each-node-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 29 Nov 2021 08:14:24 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[level order]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[traversal]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8933</guid>

					<description><![CDATA[<p>Given a binary tree [crayon-663cb03a10e96922604237/] Populate each next pointer to point to its next right node. If there is no next right node, the next&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-117-populating-next-right-pointers-in-each-node-ii/">花花酱 LeetCode 117. Populating Next Right Pointers in Each Node II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a binary tree</p>



<pre class="crayon-plain-tag">struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}</pre>



<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to&nbsp;<code>NULL</code>.</p>



<p>Initially, all next pointers are set to&nbsp;<code>NULL</code>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>The number of nodes in the tree is in the range&nbsp;<code>[0, 6000]</code>.</li><li><code>-100 &lt;= Node.val &lt;= 100</code></li></ul>



<p><strong>Follow-up:</strong></p>



<ul><li>You may only use constant extra space.</li><li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li></ul>



<h2><strong>Solution -2: Group nodes into levels</strong></h2>



<p>Use pre-order traversal to group nodes by levels.<br>Connects nodes in each level.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  Node* connect(Node* root) {
    vector&lt;vector&lt;Node*&gt;&gt; levels;
    dfs(root, 0, levels);
    for (auto&amp; nodes : levels)
      for(int i = 0; i &lt; nodes.size() - 1; ++i)
        nodes[i]-&gt;next = nodes[i+1]; 
    return root;
  }
private:
  void dfs(Node *root, int d, vector&lt;vector&lt;Node*&gt;&gt;&amp; levels) {
    if (!root) return;
    if (levels.size() &lt; d+1) levels.push_back({});
    levels[d].push_back(root);
    dfs(root-&gt;left, d + 1, levels);
    dfs(root-&gt;right, d + 1, levels);
  }
};</pre>
</div></div>



<h2><strong>Solution -1: BFS level order traversal</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<p></p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  Node* connect(Node* root) {
    if (!root) return root;
    queue&lt;Node*&gt; q{{root}};
    while (!q.empty()) {
      int size = q.size();
      Node* p = nullptr;
      while (size--) {
        Node* t = q.front(); q.pop();
        if (p) 
          p-&gt;next = t, p = p-&gt;next;
        else
          p = t;
        if (t-&gt;left) q.push(t-&gt;left);
        if (t-&gt;right) q.push(t-&gt;right);
      }
    }
    return root;
  }
};</pre>
</div></div>



<h2><strong>Solution 1: BFS w/o extra space</strong></h2>



<p>Populating the next level while traversing current level.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  Node* connect(Node* root) {
    Node* p = root;        
    while (p) {
      Node dummy;
      Node* c = &amp;dummy;
      while (p) {
        if (p-&gt;left)
          c-&gt;next = p-&gt;left, c = c-&gt;next;
        if (p-&gt;right)
          c-&gt;next = p-&gt;right, c = c-&gt;next;
        p = p-&gt;next;
      }
      p = dummy.next;
    }
    return root;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-117-populating-next-right-pointers-in-each-node-ii/">花花酱 LeetCode 117. Populating Next Right Pointers in Each Node II</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-117-populating-next-right-pointers-in-each-node-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 107. Binary Tree Level Order Traversal II</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-107-binary-tree-level-order-traversal-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-107-binary-tree-level-order-traversal-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 30 Jan 2020 02:15:09 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[level order]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6168</guid>

					<description><![CDATA[<p>Given a binary tree, return the&#160;bottom-up level order&#160;traversal of its nodes&#8217; values. (ie, from left to right, level by level from leaf to root). For&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-107-binary-tree-level-order-traversal-ii/">花花酱 LeetCode 107. Binary Tree Level Order Traversal II</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a binary tree, return the&nbsp;<em>bottom-up level order</em>&nbsp;traversal of its nodes&#8217; values. (ie, from left to right, level by level from leaf to root).</p>



<p>For example:<br>Given binary tree&nbsp;<code>[3,9,20,null,null,15,7]</code>,<br></p>



<pre class="wp-block-preformatted;crayon:false">    3
   / \
  9  20
    /  \
   15   7
</pre>



<p>return its bottom-up level order traversal as:<br></p>



<pre class="wp-block-preformatted;crayon:false">[
  [15,7],
  [9,20],
  [3]
]</pre>



<h2><strong>Solution: Recursion</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
    vector&lt;vector&lt;int&gt;&gt; levelOrderBottom(TreeNode *root) {
      vector&lt;vector&lt;int&gt;&gt; ans;
      levelOrderBottom(root, 0, ans);
      reverse(ans.begin(), ans.end());
      return ans;
    }
private:
    void levelOrderBottom(TreeNode *root, int depth, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {
      if (!root) return;
      while (ans.size() &lt;= depth) ans.push_back({});
      ans[depth].push_back(root-&gt;val);
      levelOrderBottom(root-&gt;left, depth + 1, ans);
      levelOrderBottom(root-&gt;right, depth + 1, ans);
    }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/leetcode/leetcode-102-binary-tree-level-order-traversal/">https://zxi.mytechroad.com/blog/leetcode/leetcode-102-binary-tree-level-order-traversal/</a></li><li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-429-n-ary-tree-level-order-traversal/">https://zxi.mytechroad.com/blog/tree/leetcode-429-n-ary-tree-level-order-traversal/</a></li><li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-872-leaf-similar-trees/">https://zxi.mytechroad.com/blog/tree/leetcode-872-leaf-similar-trees/</a></li><li><a href="https://zxi.mytechroad.com/blog/tree/leetcode-987-vertical-order-traversal-of-a-binary-tree/">https://zxi.mytechroad.com/blog/tree/leetcode-987-vertical-order-traversal-of-a-binary-tree/</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-107-binary-tree-level-order-traversal-ii/">花花酱 LeetCode 107. Binary Tree Level Order Traversal II</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-107-binary-tree-level-order-traversal-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
