Nav: << previous: 115.不同的子序列 | next: 117.填充每个节点的下一个右侧节点指针 II >>
Description
tab: English
<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 class="example">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 class="example">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>
---
[submissions](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/submissions/) | [solutions](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/solutions/)
tab: 中文
<p>给定一个 <strong>完美二叉树 </strong>,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}</pre>
<p>填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 <code>NULL</code>。</p>
<p>初始状态下,所有 next 指针都被设置为 <code>NULL</code>。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="height: 171px; width: 500px;" /></p>
<pre>
<b>输入:</b>root = [1,2,3,4,5,6,7]
<b>输出:</b>[1,#,2,3,#,4,5,6,7,#]
<b>解释:</b>给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。
</pre>
<p><meta charset="UTF-8" /></p>
<p><strong>示例 2:</strong></p>
<pre>
<b>输入:</b>root = []
<b>输出:</b>[]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点的数量在<meta charset="UTF-8" /> <code>[0, 2<sup>12</sup> - 1]</code> 范围内</li>
<li><code>-1000 <= node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>你只能使用常量级额外空间。</li>
<li>使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。</li>
</ul>
---
[提交记录](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/submissions/) | [题解](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122