当前位置:网站首页>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;
}
}
}
边栏推荐
- data = self._data_queue.get(timeout=timeout)
- Procedural life
- localStorage浏览器本地储存,解决游客不登录的情况下限制提交表单次数。
- Installation and deployment of alluxio
- How MySQL deletes all redundant duplicate data
- Status of processes and communication between processes
- Protocol selection of mobile IM system: UDP or TCP?
- Day3 data type and Operator jobs
- Experience of reading the road to wealth and freedom
- Collections and dictionaries
猜你喜欢

6.1 - 6.2 Introduction à la cryptographie à clé publique

apktool 工具使用文档
![[upsampling method opencv interpolation]](/img/6b/5e8f3c2844f0cbbbf03022e0efd5f0.png)
[upsampling method opencv interpolation]

Schematic diagram of UWB ultra high precision positioning system

Ai+ remote sensing: releasing the value of each pixel

Official image acceleration

Serious hazard warning! Log4j execution vulnerability is exposed!

cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching

How to select the data transmission format of instant messaging application

cartographer_local_trajectory_builder_2d
随机推荐
How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
The parameter field of the callback address of the payment interface is "notify_url", and an error occurs after encoding and decoding the signed special character URL (,,,,,)
C# 40. byte[]与16进制string互转
[greedy college] Figure neural network advanced training camp
zencart新建的URL怎么重写伪静态
vscode config
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
Introduction to GUI programming to game practice (II)
Recursively traverse directory structure and tree presentation
Command line interface of alluxio
创建 SSH 秘钥对 配置步骤
The beautiful scenery is natural, and the wonderful pen is obtained by chance -- how is the "wonderful pen" refined?
How to rewrite a pseudo static URL created by zenpart
Setting pseudo static under fastadmin Apache
Transport layer TCP protocol and UDP protocol
Day3 data type and Operator jobs
Chapter 9 setting up structured logging (I)
Introduction to alluxio
数据存储:MySQL之InnoDB与MyISAM的区别
RESNET in tensorflow_ Train actual combat