Nav: << previous: 116.填充每个节点的下一个右侧节点指针 | next: 118.杨辉三角 >>
Description
tab: English
<p>Given a binary tree</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/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<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 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, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</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-ii/submissions/) | [solutions](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/solutions/)
tab: 中文
<p>给定一个二叉树:</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>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>输入</strong>:root = [1,2,3,4,5,null,7]
<strong>输出:</strong>[1,#,2,3,#,4,5,7,#]
<strong>解释:</strong>给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指针连接),'#' 表示每层的末尾。</pre>
<p><strong class="example">示例 2:</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中的节点数在范围 <code>[0, 6000]</code> 内</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p><strong>进阶:</strong></p>
<ul>
<li>你只能使用常量级额外空间。</li>
<li>使用递归解题也符合要求,本题中递归程序的隐式栈空间不计入额外空间复杂度。</li>
</ul>
<ul>
</ul>
---
[提交记录](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/submissions/) | [题解](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/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