当前位置:网站首页>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 】
}
}
边栏推荐
- [reading notes] copywriting realization -- four golden steps for writing effective copywriting
- JS daily development tips (continuous update)
- Golang multi graph generation gif
- Test function in pychram
- Common interview questions for performance test
- Kmeans
- 网页不能右键 F12 查看源代码解决方案
- How to verify whether the contents of two files are the same
- Redis tutorial
- GCC usage, makefile summary
猜你喜欢
[小样本分割]论文解读Prior Guided Feature Enrichment Network for Few-Shot Segmentation
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
Chapter 03_ User and authority management
Take you through a circuit board, from design to production (dry goods)
别再说不会解决 “跨域“ 问题啦
衡量两个向量相似度的方法:余弦相似度、pytorch 求余弦相似度:torch.nn.CosineSimilarity(dim=1, eps=1e-08)
还在浪费脑细胞自学吗,这份面试笔记绝对是C站天花板
倍福TwinCAT3 Ads相关错误详细列表
Basic concepts of database
Elk elegant management server log
随机推荐
Learning notes for introduction to C language multithreaded programming
10、Scanner.next() 无法读取空格/indexOf -1
JUC learning
家居网购项目
Thread data sharing and security -threadlocal
[us match preparation] complete introduction to word editing formula
go实现命令行的工具cli
数据库中COMMENT关键字的使用
The best learning method in the world: Feynman learning method
线程数据共享和安全 -ThreadLocal
Basic concept and classification of sorting
The shell script uses two bars to receive external parameters
Latest interface automation interview questions
EtherCAT简介
Cookie&Session
EtherCAT原理概述
FCN全卷積網絡理解及代碼實現(來自pytorch官方實現)
split(),splice(),slice()傻傻分不清楚?
torch.histc
深度学习中的随机种子torch.manual_seed(number)、torch.cuda.manual_seed(number)