<?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>linkedlist Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/linkedlist/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/linkedlist/</link>
	<description></description>
	<lastBuildDate>Sun, 28 Nov 2021 04:36: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>linkedlist Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/linkedlist/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 116. Populating Next Right Pointers in Each Node</title>
		<link>https://zxi.mytechroad.com/blog/tree/leetcode-116-populating-next-right-pointers-in-each-node/</link>
					<comments>https://zxi.mytechroad.com/blog/tree/leetcode-116-populating-next-right-pointers-in-each-node/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 22 Aug 2019 15:58:13 +0000</pubDate>
				<category><![CDATA[Tree]]></category>
		<category><![CDATA[linkedlist]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[tree]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5478</guid>

					<description><![CDATA[<p>You are given a&#160;perfect binary tree&#160;where&#160;all leaves are on the same level, and every parent has two children. The binary tree has the following definition:&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-116-populating-next-right-pointers-in-each-node/">花花酱 LeetCode 116. Populating Next Right Pointers in Each Node</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe width="500" height="375" src="https://www.youtube.com/embed/YNu143ZN4qU?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given a&nbsp;<strong>perfect binary tree</strong>&nbsp;where&nbsp;all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</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:</strong></p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input: </strong>{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":{"$id":"6","left":null,"next":null,"right":null,"val":6},"next":null,"right":{"$id":"7","left":null,"next":null,"right":null,"val":7},"val":3},"val":1}

<strong>Output: </strong>{"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":{"$id":"4","left":null,"next":{"$id":"5","left":null,"next":{"$id":"6","left":null,"next":null,"right":null,"val":7},"right":null,"val":6},"right":null,"val":5},"right":null,"val":4},"next":{"$id":"7","left":{"$ref":"5"},"next":null,"right":{"$ref":"6"},"val":3},"right":{"$ref":"4"},"val":2},"next":null,"right":{"$ref":"7"},"val":1}

<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B.
</pre>



<p><strong>Note:</strong></p>



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



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



<p>Do a preorder traversal:<br>1. return if self is empty or leaf<br>2. self.left-&gt;next = self.right<br>3. if self.next: self.right.next = self.next.left</p>



<p>Time complexity: O(n)<br>Space complexity: O(log(n)) since it&#8217;s a perfect tree</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 || !root-&gt;left) return root;
    root-&gt;left-&gt;next = root-&gt;right;
    if (root-&gt;next)
      root-&gt;right-&gt;next = root-&gt;next-&gt;left;
    connect(root-&gt;left);
    connect(root-&gt;right);
    return root;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/tree/leetcode-116-populating-next-right-pointers-in-each-node/">花花酱 LeetCode 116. Populating Next Right Pointers in Each Node</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-116-populating-next-right-pointers-in-each-node/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
