当前位置:网站首页>LeetCode 0117. 填充每个节点的下一个右侧节点指针 II
LeetCode 0117. 填充每个节点的下一个右侧节点指针 II
2022-07-17 17:15:00 【Tisfy】
【LetMeFly】117.填充每个节点的下一个右侧节点指针 II
力扣题目链接:https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/
给定一个二叉树
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
初始状态下,所有 next 指针都被设置为 NULL。
进阶:
- 你只能使用常量级额外空间。
- 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
示例:

输入:root = [1,2,3,4,5,null,7] 输出:[1,#,2,3,#,4,5,7,#] 解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指针连接),'#' 表示每层的末尾。
提示:
- 树中的节点数小于
6000 -100 <= node.val <= 100
方法一:层次遍历
这道题和LeetCode 116. 填充每个节点的下一个右侧节点指针 几乎一模一样。
区别是LeetCode0116 保证了二叉树为完全二叉树。
如果不考虑空间复杂度必须为 O ( 1 ) O(1) O(1)的情况下,只使用层次遍历,那么这两道题的代码可以完全相同。
具体实现方法请参考https://leetcode.letmefly.xyz/2022/07/16/LeetCode 0116.填充每个节点的下一个右侧节点指针
- 时间复杂度 O ( N ) O(N) O(N),其中 N N N是节点的个数
- 空间复杂度 O ( M ) O(M) O(M),其中 M M M是节点数最多的一层的节点的数量
AC代码
C++
typedef pair<Node*, int> pii;
class Solution {
public:
Node* connect(Node* root) {
if (!root)
return root;
queue<pii> q;
q.push({
root, 1});
int lastLayer = 0;
Node* lastNode;
while (q.size()) {
auto[thisNode, thisLayer] = q.front();
q.pop();
if (thisLayer == lastLayer) {
lastNode->next = thisNode;
}
lastNode = thisNode;
lastLayer = thisLayer;
if (thisNode->left)
q.push({
thisNode->left, thisLayer + 1});
if (thisNode->right)
q.push({
thisNode->right, thisLayer + 1});
}
return root;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125828883
边栏推荐
- Competition notes: numpy learning notes
- Flask源码分析(三):上下文
- XML file parsing
- Yu Meimei, Ji Gongdu
- When will the moon appear
- Possible problems in inserting Excel data into MySQL database
- Binary tree 2-symmetry recursion problem
- Stable super odds, 9 years old mixin | 2022 Jincang innovative product launch was successfully held
- About the "bottom reading" mentality, it makes you exhausted 2020-03-15
- Flask source code analysis (III): Context
猜你喜欢

可视化ETL工具Kettle概念、安装及实战案例

Review the 2008 financial crisis and become a long-term investor 2020-03-19

The difference and use between get request and post request

虞美人·寄公度

AE如何制作星云粒子特效

Uio-66 - (COOH) 2 modified polyamide nanofiltration membrane | zif-8/pvp composite nanofiber membrane | uio-66-nh2 modified polyamide nanofiltration membrane

Metal organic framework / nitrogen carbide nano sheet (uio-66/hocn) composite | mil-101 loaded Au Pd alloy nanoparticles | chemical reagent MOF customization

关于“抄底”心态,让你筋疲力尽 2020-03-15

关于TCP/IP协议漏洞的安全措施

ASP. Net collaborative OA office service management platform source code
随机推荐
Equivalent domain name
Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology
2022年最新吉林建筑安全员模拟题库及答案
Knowledge sorting of MySQL
The active and standby cache of redis cluster is full, causing frequent active and standby switchover
Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field
全球金融危机来袭,如何科学理性投资?2020-03-17
MOF customized product | n-k2ti4o9/g-c3n4/uio-66 ternary composite | paper based au-aginse2-zif-8 Nanocomposite
VMware导入ova/ovf虚拟机文件
O & M LITTLE WHITE Growth record - architecture week 6
When will the moon appear
Att & CK actual combat series - red team actual combat (-)
最小交换次数
水调歌头·明月几时有
Acwing4405. 统计子矩阵
10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!
How to invest scientifically and rationally when the global financial crisis strikes? 2020-03-17
Logical operator 1 (Gretel software - Jiuye training)
July training (day 17) - breadth first search
标签球问题