当前位置:网站首页>Leetcode - using sequence traversal features first completed 114. The binary tree to the list
Leetcode - using sequence traversal features first completed 114. The binary tree to the list
2022-08-04 11:12:00 【lonelyMangoo】
题目:114. 二叉树展开为链表
这道题很有意思,Because of the various recursion in the comment area, I really can't understand it,Until I saw a completed comment that exploits the binary tree features,Just imitate it yourself,Just record and share.
思路一:重新建树
思路:All nodes are recorded in a linked list,然后设置.Be sure to save the node here,Because this question requires modification of the originalroot,而不是创建新的节点.
public void flatten(TreeNode root) {
//先序遍历+建树
if(root==null) return ;
List<TreeNode> buildList = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
// in preorder 左右,So on the stack 右左
//Because the middle is processed first
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
buildList.add(pop);
if (pop.right != null) {
stack.push(pop.right);
}
if (pop.left != null) {
stack.push(pop.left);
}
}
for (int i = 0; i < buildList.size()-1; i++) {
buildList.get(i).left=null;
buildList.get(i).right=buildList.get(i+1);
}
buildList.get(buildList.size()-1).left=null;
buildList.get(buildList.size()-1).right=null;
}
思路二:Take advantage of the preorder traversal feature
The method above requires extra space,This space complexity is O(1)
思路:Connect the current left subtree to the right,Then the first left subtree to be found、Then find the predecessor node of the right subtree,Connect the right subtree to the predecessor,Then move the whole left subtree to the right subtree.
听起来有点绕,来个例子,cur表示当前处理的节点,right_pre表示curThe predecessor of the right subtree
Take the graph in the example as an example,从根节点开始
cur = 1
找到cur.left的最右节点right_pre,也就是4,然后把cur.right 接到right_pre后面.再把cur.left接到cur的右边
处理完了之后:
然后cur = cur.right,Why do you have the confidence to do so,因为curThe ones on the left have been moved to the right,And it is moved according to the preorder traversal,So just look to the right.cur=2
将4 接到 3 后面,3 再接到2后面,就能得到
But keep going to the right,See if there are any unhandled left children.
代码:
public void flatten(TreeNode root) {
TreeNode cur = root;
while ( cur!=null ){
//in the pre-order table,找到当前节点的下一个节点
TreeNode right_pre = cur.left;
//找到cur.right的前一个,也就是左子树的最右节点
while (right_pre!=null && right_pre.right != null ){
right_pre = right_pre.right;
}
//拼接操作
if(right_pre!=null){
right_pre.right=cur.right;
//Then connect the whole left side to the right side
cur.right=cur.left;
cur.left=null;
}
//继续寻找下一个
cur = cur.right;
}
}
边栏推荐
- Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)
- 数据化管理洞悉零售及电子商务运营——零售密码
- Mysql——》类型转换符binary
- 技术干货 | 用零信任保护代码安全
- Camunda整体架构和相关概念
- 少即是多:视觉SLAM的点稀疏化(IROS 2022)
- 音频编辑 合唱
- JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
- Win11 file types, how to change?Win11 modify the file suffix
- Meishe Q&A Room | Meiying VS Meishe Cloud Editing
猜你喜欢
A topic of map
Heap Sort
第二批养老理财试点产品发行 一小时销售20亿元
Maple 2022 software installation package download and installation tutorial
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
MATLAB程序设计与应用 3.2 矩阵变换
Small program containers accelerate the construction of an integrated online government service platform
Camunda整体架构和相关概念
复盘:经典的HR面试问题,这些问题可以挖掘你个人的素质,看看你是否合适合我们部门
随机推荐
命令模式(Command)
Mysql数据类型
C语言*小白的探险历程
Super Learning Method
Business collocations
Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)
helm安装
中介者模式(Mediator)
Small program containers accelerate the construction of an integrated online government service platform
mongo-导出数据到mysql
Heap Sort
datax oracle to oracle增量同步
onlyoffice设置跟踪变化trackChanges默认为对自己启动
RL78 development environment
【LeetCode】1403.非递增顺序的最小子序列
[Hongke case] Assembling furniture based on 3D camera
Heap Sort
OD-Model【5】:YOLOv1
数据化管理洞悉零售及电子商务运营——零售密码
Business collocations