当前位置:网站首页>Leetcode114. Expand binary tree into linked list
Leetcode114. Expand binary tree into linked list
2022-06-26 05:20:00 【Java full stack R & D Alliance】
1、 Title Description
Give you the root node of the binary tree root , Please expand it into a single linked list :
- The expanded single linked list should also be used TreeNode , among right The child pointer points to the next node in the list , The left sub pointer is always null .
- The expanded single linked list should be the same as the binary tree The first sequence traversal Same order .
Example 1:
Input :root = [1,2,5,3,4,null,6]
Output :[1,null,2,null,3,null,4,null,5,null,6]
Scheme 1 Preorder traversal and expansion are carried out separately
Their thinking :
First, we can use a list Save the result of the preorder traversal . And then to list Of elements in right Pointer operation .
The code is as follows
class Solution {
public void flatten(TreeNode root) {
List<TreeNode> list = new ArrayList<TreeNode>();
preorderTraversal(root, list);
int size = list.size();
for (int i = 1; i < size; i++) {
TreeNode prev = list.get(i - 1), curr = list.get(i);
prev.left = null;
prev.right = curr;
}
}
public void preorderTraversal(TreeNode root, List<TreeNode> list) {
if (root != null) {
list.add(root);
preorderTraversal(root.left, list);
preorderTraversal(root.right, list);
}
}
}
Option two
It can be found that the order of expansion is actually the first order traversal of binary tree . Algorithm and 94 In the question, the order traverses Morris The algorithm is a bit like , We need to finish the problem in two steps .
- Insert the left subtree into the right subtree
- Connect the original right subtree to the rightmost node of the left subtree
- Consider the root node of the new right subtree , Repeat the above process all the time , Until the new right subtree is null
You can see the picture to understand the process .
1
/ \
2 5
/ \ \
3 4 6
// take 1 Where the left subtree of is inserted into the right subtree
1
\
2 5
/ \ \
3 4 6
// Connect the original right subtree to the rightmost node of the left subtree
1
\
2
/ \
3 4
\
5
\
6
// take 2 Where the left subtree of is inserted into the right subtree
1
\
2
\
3 4
\
5
\
6
// Connect the original right subtree to the rightmost node of the left subtree
1
\
2
\
3
\
4
\
5
\
6
......
The code is as follows :
public void flatten(TreeNode root) {
while (root != null) {
// The left sub tree is null, Consider the next node directly
if (root.left == null) {
root = root.right;
} else {
// Find the rightmost node of the left subtree
TreeNode pre = root.left;
while (pre.right != null) {
pre = pre.right;
}
// Connect the original right subtree to the rightmost node of the left subtree
pre.right = root.right;
// Insert the left subtree into the right subtree
root.right = root.left;
root.left = null;
// Consider next node
root = root.right;
}
}
}
边栏推荐
猜你喜欢
cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching
Codeforces Round #802 (Div. 2)(A-D)
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
关于支付接口回调地址参数字段是“notify_url”,签名过后的特殊字符url编码以后再解码后出现错误(¬ , ¢, ¤, £)
Red team scoring method statistics
Learn from small samples and run to the sea of stars
基于SDN的DDoS攻击缓解
ECCV 2020 double champion team, take you to conquer target detection on the 7th
Second day of deep learning and tensorfow
10 set
随机推荐
Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
Sentimentin tensorflow_ analysis_ cell
二次bootloader关于boot28.asm应用的注意事项,28035的
C# 39. Conversion between string type and byte[] type (actual measurement)
Codeforces Round #800 (Div. 2)
【上采样方式-OpenCV插值】
Happy New Year!
Practical cases | getting started and mastering tkinter+pyinstaller
cartographer_backend_constraint
【ARM】讯为rk3568开发板buildroot添加桌面应用
app 应用安装到手机,不显示图标,引发的思考
12 multithreading
数据存储:MySQL之InnoDB与MyISAM的区别
zencart新建的URL怎么重写伪静态
无线网络存在的安全问题及现代化解决方案
出色的学习能力,才是你唯一可持续的竞争优势
GD32F3x0 官方PWM驱动正频宽偏小(定时不准)的问题
Tp5.0 framework PDO connection MySQL error: too many connections solution
Recursively traverse directory structure and tree presentation
The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of