当前位置:网站首页>填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
2022-06-29 17:34:00 【REN_林森】
分层遍历 -> 利用好每个点
前言
层次遍历是操作二叉树的基础遍历之一,通过在分层中做任何需求,完成分层遍历的各种变体。
除此之外,利用好每一个点/甚至是自己创造的点,尽可能的降低时空复杂度。有时利用给的数组和矩阵,原地修改,降低时空复杂度。
一、填充每个节点的下一个右侧节点指针

二、分层与利用next
1、分层遍历
// 完美二叉树
public class Connect {
// 分层遍历即可
public Node connect(Node root) {
if (root == null) return null;
Queue<Node> que = new LinkedList<>();
que.offer(root);
int size = que.size();
Node pre = null;
while (!que.isEmpty()) {
Node cur = que.poll();
if (cur.left != null) que.offer(cur.left);
if (cur.right != null) que.offer(cur.right);
if (pre != null) pre.next = cur;
pre = cur;
if (--size == 0) {
size = que.size();
pre = null;
}
}
return root;
}
// 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;
}
}
;
}
2、利用已有的next
// 递归 + 额外常数空间。需要利用二叉树自身特性。
class Connect2 {
// 利用已设置的next
public Node connect(Node root) {
if (root == null) return null;
Node upDummy = new Node(0, null, null, root);
Node downDummy = new Node();
// 通过已有next+加上下层dummyHead,一层一层的设置next,循环下去。
while (upDummy.next != null) {
Node pUp = upDummy;
Node pDown = downDummy;
for (; pUp.next != null; pUp = pUp.next) {
// 两个孩子都为空,下一层链表没什么赋值的。
if (pUp.next.left == null && pUp.next.right == null) continue;
// 两个孩子都有,则需要将两个孩子串联起来,并得到tail节点。
if (pUp.next.left != null && pUp.next.right != null) {
// 串两个孩子
pUp.next.left.next = pUp.next.right;
// 将串好的两个孩子接到前面的链表后,即tail节点处。
pDown.next = pUp.next.left;
// 更新tail指针,为下一次cycle做准备。
pDown = pUp.next.right;
} else {
// 只有一个孩子的情况,不需要串,只需要把不为空的孩子接到tail处,再更新tail节点。
pDown.next = pUp.next.left == null ? pUp.next.right : pUp.next.left;
pDown = pDown.next;
}
}
// update for next cycle
upDummy = downDummy;
downDummy = new Node();
}
// 返回原始的根节点。
return root;
}
// 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;
}
}
;
}
总结
1)分层遍历。
2)利用好每个特别的地方,发挥它的价值,降低时空复杂度。
参考文献
边栏推荐
- Redis bloom filter and cuckoo filter
- 在线文本数字识别列表求和工具
- Master slave replication of MySQL
- 毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- Freedom自由协议质押挖矿系统开发
- SAAS 服务的优势都有哪些
- R语言将距离矩阵输入给hclust函数进行层次聚类分析,method参数指定两个组合数据点间的距离计算方式、plot函数可视化层次聚类的树状图(dendrogram)
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合、接着再和第三个图像横向组合起来(三幅图各占比例为50%、25%、25%)
- Browser large screen capture
猜你喜欢

手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学

疫情居家外包项目之协作开发| 社区征文

0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram

在线SQL转CSV工具

Automatic vending machine

How to create a virtual image

序列检测器

mysql如何查询表的字符集编码

如何创建虚拟形象

Tencent cloud released orbit, an automated delivery and operation and maintenance product, to promote enterprise applications to be fully cloud native
随机推荐
Bags of Binary Words for Fast Place Recognition in Image Sequenc
L'intercepteur handlerinterceptor personnalisé permet l'authentification de l'utilisateur
The soft youth under the blessing of devcloud makes education "smart" in the cloud
mysql.sock的概念是什么
KUKA robot external axis configuration what you must know
Self taught structure (small turtle C language)
Multi mode concurrent implementation of tortoise and rabbit race in go language
R语言使用epiDisplay包的kap函数(kap.2.raters函数)计算Kappa统计量的值(总一致性、期望一致性)、对两个评分对象的结果进行一致性分析、评分的类别为多个类别
External automatic (PLC start robot)
使用autoIt 上传文件
Redis bloom filter and cuckoo filter
在线SQL转CSV工具
regular expression
传承中华美德,关注中老年大健康,育润奶粉敬老情浓
Kubernetes部署Dashboard(WEB UI管理界面)
The dplyr package filter function of R language filters the data in dataframe data through combinatorial logic (and logic). The content of one field is equal to one of the specified vectors, and the v
R语言将距离矩阵输入给hclust函数进行层次聚类分析,method参数指定两个组合数据点间的距离计算方式、plot函数可视化层次聚类的树状图(dendrogram)
如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?
手把手教你在windows上安装mysql8.0最新版本数据库,保姆级教学
关于日期相加减问题