当前位置:网站首页>The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
The preorder traversal of leetcode 144 binary tree and the expansion of leetcode 114 binary tree into a linked list
2022-07-01 03:30:00 【It's seventh uncle】
LeetCode 144 Preorder traversal of two tree
Title Description :
Give you the root node of the binary tree root , Of its node value Before the order Traverse .
Example 1:
Input :root = [1,null,2,3]
Output :[1,2,3]
Example 2:
Input :root = []
Output :[]
One 、 Access the root node according to —— The left subtree —— The right subtree traverses the tree , And when you visit the left or right subtree , We traverse in the same way , Until you walk through the whole tree .
You can use the complete code :
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
preorder(root, ans);
return ans;
}
private void preorder(TreeNode root, List<Integer> ans) {
if (root == null) {
return;
}
ans.add(root.val);
preorder(root.left, ans);
preorder(root.right, ans);
}

LeetCode 114 The binary tree is expanded into a list
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]
Example 2:
Input :root = []
Output :[]
One 、 First, find... Through the current root node cur The last right node of the pre order of the left subtree predecessor, Connect this node to the right subtree ; stretch 【 take curr.left=null;curr.right=next】next Namely cur.left; Then continue to loop to the left , Connect , Traversing to the end will always stretch
- Time complexity :O(n). among n It's the number of nodes in a binary tree . In the process of expanding to a single linked list , Each node needs to be accessed once , In the process of finding the precursor node , Each node can be accessed once more at most .
- Spatial complexity :O(1)




You can use the complete code :
public void flatten(TreeNode root) {
TreeNode curr = root;
while (curr != null) {
if (curr.left != null) {
TreeNode next = curr.left;
TreeNode predecessor = next;
while (predecessor.right != null) {
// find cur The last node before the left subtree
predecessor = predecessor.right;
}
predecessor.right = curr.right; // Connect cur The last node of the pre order of the subtree and the right subtree
curr.left = null; // Partial extension
curr.right = next;
}
curr = curr.right; // cur.right Even if it is the original cur The left node , Then continue to traverse to the left , Connect the rightmost node of the left subtree with other parts 【 Traversing to the end will always stretch 】
}
}

边栏推荐
- pytest-fixture
- Stop saying that you can't solve the "cross domain" problem
- Nacos
- 实现pow(x,n)函数
- Introduction to the core functions of webrtc -- an article to understand peerconnectionfactoryinterface rtcconfiguration peerconnectioninterface
- Clion and C language
- ECMAScript 6.0
- md5sum操作
- Common interview questions for performance test
- Cookie&Session
猜你喜欢

终极套娃 2.0 | 云原生交付的封装

POI exports excel and displays hierarchically according to parent-child nodes

Server rendering technology JSP

Chapitre 03 Bar _ Gestion des utilisateurs et des droits

C#实现基于广度优先BFS求解无权图最短路径----完整程序展示

FCN full Convolution Network Understanding and Code Implementation (from pytorch Official Implementation)

Edge drawing: a combined real-time edge and segment detector

CX5120控制汇川IS620N伺服报错E15解决方案

Detailed list of errors related to twincat3 ads of Beifu

A few lines of transaction codes cost me 160000 yuan
随机推荐
ASGNet论文和代码解读2
Introduction to ieda right click source file menu
手把手带你了解一块电路板,从设计到制作(干货)
5、【WebGIS实战】软件操作篇——服务发布及权限管理
线程数据共享和安全 -ThreadLocal
多元线性回归
Mybati SQL statement printing
Nacos
[reading notes] copywriting realization -- four golden steps for writing effective copywriting
Test function in pychram
Kmeans
Include() of array
Ctfshow blasting WP
HTB-Lame
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Thread data sharing and security -threadlocal
Subnet division and subnet summary
Edge Drawing: A combined real-time edge and segment detector 翻译
multiple linear regression
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理