当前位置:网站首页>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
边栏推荐
- input separator
- Stability summary
- 如何添加 logs来debug ANR 问题
- Characters and integers
- Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
- Leetcode daily question - 324 Swing sort II
- [Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
- 字符和整数
- How to use dataant to monitor Apache apisex
- 【学习笔记】聚类分析
猜你喜欢

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

基于 Apache APISIX 的自动化运维平台

Pie (poj3122) super detailed and easy to understand binary introduction

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

Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer

Analysis of variance

数据标准化处理

题解 Pie(POJ3122)超详细易懂的二分入门

ThreadLocal principle

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)
随机推荐
Bitbucket failed to pull the warehouse Using SSH
Leetcode daily question - 515 Find the maximum value in each tree row
ANR问题--相机相关的debug
输入和输出实型数据
视频号如何下载视频?来看超简单方法!
[learning notes] cluster analysis
Win 10 create a gin framework project
How to add logs to debug anr problems
Comparisonchain file name sort
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
理解整个网络模型的构建
Understanding of incomplete types
新形势下的SaaS销售升级|ToB大师课
Leetcode 36. 有效的数独(可以,一次过)
关于不完全类型的认识
题解 Ananagrams(UVa156)紫书P113map的应用
员工薪资管理系统
How to understand the usability of cloud native databases?
炒股票能赚钱么?开户安全嘛
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application