当前位置:网站首页>LeetCode116. 填充每个节点的下一个右侧节点指针
LeetCode116. 填充每个节点的下一个右侧节点指针
2022-06-28 20:59:00 【Yuyy】
本文最后更新于 484 天前,其中的信息可能已经有所发展或是发生改变。
一、思路
经过画图发现,儿子之间连接没问题。但是不同父亲的孙子连接有问题,解决一个倒还好,巧妙的是经过递归,可以解决所有这类问题。 总是想着把问题分治,但分治完了却没想到可以还原回去。。。
二、问题
给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
初始状态下,所有 next 指针都被设置为 NULL。
进阶:
- 你只能使用常量级额外空间。
- 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
示例:
输入:root = [1,2,3,4,5,6,7]
输出:[1,#,2,3,#,4,5,6,7,#]
解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。提示:
- 树中节点的数量少于
4096 -1000 <= node.val <= 1000
Related Topics
- 树
- 深度优先搜索
- 广度优先搜索
\n
- 404
- 0
三、代码
public Node connect(Node root) {
if (root == null) {
return null;
}
convert(root.left, root.right);
return root;
}
private void convert(Node left, Node right) {
if (left == null || right == null) {
return;
}
left.next = right;
convert(left.left, left.right);
convert(right.left, right.right);
convert(left.right, right.left);
}Post Views: 297
边栏推荐
- LeetCode每日一题——30. 串联所有单词的子串
- with torch. no_ Grad(): reason for using
- Explanation of memory dump triggered by software watchdog and anr
- [Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
- Various types of long
- A few lines of code can realize complex excel import and export. This tool class is really powerful!
- Understand the construction of the entire network model
- Pie (poj3122) super detailed and easy to understand binary introduction
- 实型数运算
- Bitbucket failed to pull the warehouse Using SSH
猜你喜欢
![[try to hack] cobalt strike (I)](/img/2b/5d274078b7d7ebd05b7c6d9e020868.png)
[try to hack] cobalt strike (I)

Apisik helps Middle East social software realize localized deployment
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

API 网关 Apache APISIX 助力雪球双活架构演进

LeetCode每日一题——515. 在每个树行中找最大值
How to recover after Oracle delete accidentally deletes table data

CNN-LSTM的flatten

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

力扣树的进一步应用
随机推荐
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
Keyword long
pyechart绘制多条y轴折线图
视频号如何下载视频?来看超简单方法!
The blocks problem (uva101) Purple Book p110vector application
SaaS sales upgrade under the new situation | tob Master Course
软件watchdog和ANR触发memory dump讲解
With a market value of $120billion, how did intuit, an old tax giant, do it?
[learning notes] Introduction to principal component analysis
API 网关 Apache APISIX 助力雪球双活架构演进
Input and output real data
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
Can you make money by speculating in stocks? It's safe to open an account
03.hello_ rust
T检验(检验两个总体的均值差异是否显著)
GlobalSign的泛域名SSL证书
题解 The Blocks Problem(UVa101)紫书P110vector的应用
No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
如何添加 logs来debug ANR 问题
Application of Andy s first dictionary (uva10815) Purple Book p112set