当前位置:网站首页>力扣二叉树
力扣二叉树
2022-07-31 23:45:00 【小唐学姐】
难度中等834
给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:
struct Node { int val; Node *left; Node *right; Node *next; }
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL
。
初始状态下,所有 next 指针都被设置为 NULL
。
示例 1:
输入:root = [1,2,3,4,5,6,7] 输出:[1,#,2,3,#,4,5,6,7,#] 解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。
示例 2:
输入:root = [] 输出:[]
提示:
- 树中节点的数量在
[0, 212 - 1]
范围内 -1000 <= node.val <= 1000
进阶:
- 你只能使用常量级额外空间。
- 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
Node save = root;
if(root == null) return root;
Queue<Node> queue = new LinkedList();
queue.add(root);
recur(queue, 1);
return root;
}
public void recur(Queue queue, int height) { //height是正在处理的这一行的节点个数
int count = 0;
while(count < height) {
Node root = (Node)queue.poll();
count++;
if(count != height) root.next = (Node)queue.peek(); //说明不是这一行的最后一个,让next指针指向队列首的节点即可
else root.next = null; //说明是这一行的最后一个
if(root.left != null) queue.add(root.left);
if(root.right != null) queue.add(root.right); //将儿子放进队列
}
if(queue.peek() == null) return; //如果队列为空,说明所节点素都已经处理完了
else recur(queue, height*2); //队列不为空,而且因为是完美二叉树,每一行的节点个数都是上一行的两倍
}
}
边栏推荐
- Difference between first and take(1) operators in NgRx
- 编写方法将一个数组扁平化并且去重和递增排序
- Flutter教程之 02 Flutter 桌面程序开发入门教程运行hello world (教程含源码)
- How to Design High Availability and High Performance Middleware - Homework
- Basic use of vim - bottom line mode
- The difference between /usr/local/bin and /usr/bin
- ICML2022 | 深入研究置换敏感的图神经网络
- 手写一个简单的web服务器(B/S架构)
- "APIO2010" Patrol Problem Solution
- 2022年CSP-J1 CSP-S1 第1轮初赛 报名指南
猜你喜欢
cobaltstrike
消息队列存储消息数据的MySQL表格
SQL27 View user details of different age groups
Drawing process of hand-drawn map of scenic spots
TFC CTF 2022 WEB Diamand WriteUp
2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法
Daily--Kali opens SSH (detailed tutorial)
【ACM】2022.7.31训练赛
基于单片机GSM的防火防盗系统的设计
高等代数_证明_任何矩阵都相似于一个上三角矩阵
随机推荐
信奥学习规划 信息学竞赛之路(2022.07.31)
The uniapp applet checks and prompts for updates
Xinao Learning Plan The Road to Informatics Competition (2022.07.31)
[Cloud Residency Co-Creation] [HCSD Big Celebrity Live Broadcast] Personally teach the secrets of interviews in big factories
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
Keil nRF52832下载失败
Basic use of vim - bottom line mode
I don't know what to do with sync issues
数据分析(一)——matplotlib
SQL注入 Less47(报错注入) 和Less49(时间盲注)
Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
Unity - LineRenderer show a line
SQL injection Less54 (limited number of SQL injection + union injection)
什么时候可以使用 PushGateway
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
一文概述:VPN的基本模型及业务类型
基于mysql的消息队列设计
【ACM】2022.7.31训练赛
Input and output optimization
Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)