当前位置:网站首页>LeetCode117. 填充每个节点的下一个右侧节点指针_II
LeetCode117. 填充每个节点的下一个右侧节点指针_II
2022-06-28 20:58:00 【Yuyy】
思路
116题还能通过画图看出规律,但这是升级版,非完美二叉树。这题很容易想到用层次遍历,但是是中等难度,肯定不是最优解。
分析下,层次遍历时间复杂度O(N),已经做到极致了。那么只能从空间复杂度下手,层次遍历空间复杂度为O(N),如果不存副本,倒是可以优化。
遍历某一层时,就将下一层串起来,这样下一层就可以直接遍历,不用存到队列里。第一层没有上一层,但第一层只有root节点,不需要串起来。
题目
给定一个二叉树
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
Related Topics
- 树
- 深度优先搜索
- 广度优先搜索
- 二叉树
- 441
- 0
代码
class Solution {
/**
* 下一层的头结点
*/
private Node nextRowHead;
/**
* 下一层的尾结点
*/
private Node nextRowTail;
public Node connect(Node root) {
if (root == null) {
return root;
}
Node curr = root;
do {
// 内层循环结束后curr为null,但第一次进来时不需要赋值,此时为root
if (curr == null) {
// 从下一行的头结点开始遍历
curr = nextRowHead;
nextRowHead = null;
}
do {
buildNextRow(curr.left);
buildNextRow(curr.right);
curr = curr.next;
// 当前层的下一个节点
} while (curr != null);
// 下一层
} while (nextRowHead != null);
return root;
}
/**
* 将下一层的节点串起来
*/
private void buildNextRow(Node node) {
if (node != null) {
if (nextRowHead == null) {
nextRowHead = node;
nextRowTail = node;
} else {
nextRowTail.next = node;
nextRowTail = node;
}
}
}
}Post Views: 143
边栏推荐
- T检验(检验两个总体的均值差异是否显著)
- [learning notes] Introduction to principal component analysis
- 1. integrate servlets
- [Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
- Is the rapid SSL wildcard certificate genuine in 1981
- 题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
- Ref attribute, props configuration, mixin mixing, plug-in, scoped style
- input separator
- Leetcode daily question - 710 Random numbers in the blacklist
- 国产数据库名录一览
猜你喜欢

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

RT-Thread线程同步与线程通信

Ehcache配置资料,方便自己查
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication

不同框架的绘制神经网络结构可视化

with torch.no_grad():的使用原因

with torch. no_ Grad(): reason for using

Ehcache configuration data, convenient for self checking
![[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
随机推荐
Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
API 网关 Apache APISIX 助力雪球双活架构演进
圆球等的相关计算
Flask——总结
T-test (test whether the mean difference between the two populations is significant)
Understanding of incomplete types
ref属性,props配置,mixin混入,插件,scoped样式
券商公司开户哪个最靠谱最安全呢
On the complexity of software development and the way to improve its efficiency
ThreadLocal原理
Leetcode 36. 有效的数独(可以,一次过)
算力时代怎么「算」?「算网融合」先发优势很重要!
国产数据库名录一览
如何使用 DataAnt 监控 Apache APISIX
LeetCode每日一题——515. 在每个树行中找最大值
How to analyze the relationship between enterprise digital transformation and data asset management?
Query rewriting for opengauss kernel analysis
[learning notes] Introduction to principal component analysis
[book club issue 13] packaging format of video files
API gateway Apache APIs IX helps the evolution of snowball dual active architecture