mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	国外版
This commit is contained in:
		
							
								
								
									
										46
									
								
								算法题(国外版)/populating-next-right-pointers-in-each-node.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								算法题(国外版)/populating-next-right-pointers-in-each-node.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
<p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
 | 
			
		||||
 | 
			
		||||
<pre>
 | 
			
		||||
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 <code>NULL</code>.</p>
 | 
			
		||||
 | 
			
		||||
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
 | 
			
		||||
 | 
			
		||||
<p> </p>
 | 
			
		||||
<p><strong>Example 1:</strong></p>
 | 
			
		||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
 | 
			
		||||
<pre>
 | 
			
		||||
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
 | 
			
		||||
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
 | 
			
		||||
<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. 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>
 | 
			
		||||
<strong>Input:</strong> root = []
 | 
			
		||||
<strong>Output:</strong> []
 | 
			
		||||
</pre>
 | 
			
		||||
 | 
			
		||||
<p> </p>
 | 
			
		||||
<p><strong>Constraints:</strong></p>
 | 
			
		||||
 | 
			
		||||
<ul>
 | 
			
		||||
	<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
 | 
			
		||||
	<li><code>-1000 <= Node.val <= 1000</code></li>
 | 
			
		||||
</ul>
 | 
			
		||||
 | 
			
		||||
<p> </p>
 | 
			
		||||
<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>
 | 
			
		||||
		Reference in New Issue
	
	Block a user