当前位置:网站首页>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
边栏推荐
- Explanation of memory dump triggered by software watchdog and anr
- API gateway Apache APIs IX helps the evolution of snowball dual active architecture
- 2. integrate filter
- [learning notes] factor analysis
- 视频号如何下载视频?来看超简单方法!
- 酷学院华少:如何在SaaS赛道里做成一家头部公司
- 题解 Ananagrams(UVa156)紫书P113map的应用
- Is the inter-bank certificate of deposit reliable and safe
- 学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
- Anr problem - camera related debug
猜你喜欢

Bitbucket 使用 SSH 拉取仓库失败的问题

【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)

Automatic operation and maintenance platform based on Apache APIs

I almost ran away
oracle delete误删除表数据后如何恢复

Leetcode daily question - 515 Find the maximum value in each tree row

Pyechart drawing multiple Y-axis line graphs

Mongodb - replica set and sharding

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer

Pie (poj3122) super detailed and easy to understand binary introduction
随机推荐
3. integrate listener
LeetCode每日一题——515. 在每个树行中找最大值
2. integrate filter
开通挖财账号安全吗?是靠谱的吗?
【读书会第13期】视频文件的封装格式
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
Bitbucket 使用 SSH 拉取仓库失败的问题
Ehcache configuration data, convenient for self checking
On the complexity of software development and the way to improve its efficiency
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Real number operation
Is the inter-bank certificate of deposit reliable and safe
大智慧上怎么进行开户啊, 安全吗
图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer
ANR问题--相机相关的debug
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
新形势下的SaaS销售升级|ToB大师课
【学习笔记】因子分析
Leetcode daily question - 515 Find the maximum value in each tree row
APISIX 助力中东社交软件,实现本地化部署