当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢
Graphical Hands-on Tutorial--ESP32 OTA Over-the-Air Upgrade (VSCODE+IDF)
线程必备内容
123
zabbix deployment
Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
Xilinx VIVADO 中 DDR3(Naive)的使用(3)仿真测试
Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
Graphic and text hands-on tutorial--ESP32 MQTT docking EMQX local server (VSCODE+ESP-IDF)
map的一道题目<单词识别>
Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)
随机推荐
【励志】复盘的重要性
DB2查看执行过长的SQL
强烈推荐一款优秀且通用的后台管理系统
winform 在Datatable插入一笔数据
datax oracle to oracle离线json文件
图文手把手教程--ESP32 一键配网(Smartconfig、Airkiss)
Small program containers accelerate the construction of an integrated online government service platform
Leetcode刷题——路径总和
小程序容器加快一体化在线政务服务平台建设
map的一道题目<单词识别>
Leetcode刷题——构造二叉树(105. 从前序与中序遍历序列构造二叉树、106. 从中序与后序遍历序列构造二叉树)
[Hongke case] Assembling furniture based on 3D camera
Heap Sort
C#/VB.NET:在 Word 中设置文本对齐方式
Redis查询缓存
【虹科案例】基于3D相机组装家具
入门MySql表的增删查改
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
Four ways to traverse a Map
STM32前言知识总结