当前位置:网站首页>Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
2022-07-26 05:54:00 【Study and pursue high efficiency】
The key point is to do some questions by yourself
The key point is to do some questions by yourself
Among the three methods of traversing binary trees
root - Left - Right
Left - root - Right
Left - Right - root
Remember that every node is a root
So when traversing the preamble , Will first “ Ignore ” Right subtree
When traversing the middle order , Will not print first The left subtree of the previous root ( Because maybe the left subtree of the previous root , It is also the root of other nodes )
During subsequent traversal , Traverse each root , To print out the root node
Be careful : Nodes traversed each time , In fact, they are all root nodes
1. The former sequence traversal ( Each traversal node is treated as the root )
2. In the sequence traversal ( Only traverse the left subtree of the root , Then print the root )
3. After the sequence traversal ( Only traverse the left and right subtrees , Then print the root )
- NLR: The former sequence traversal (Preorder Traversal Also called preorder traversal )—— Access root —> The left subtree of the root —> Right subtree of root .
- LNR: In the sequence traversal (Inorder Traversal)—— The left subtree of the root —> The root node —> Right subtree of root .
- LRN: After the sequence traversal (Postorder Traversal)—— The left subtree of the root —> Right subtree of root —> The root node
Preorder traversal results :1 2 3 4 5 6( Each traversal node is treated as the root )
Middle order traversal result :3 2 1 5 4 6( Only traverse the left subtree of the root , Then print the root )
Subsequent traversal results :3 1 5 6 4 1( Only traverse the left and right subtrees , Then print the root )
// The former sequence traversal
void preOrder(TreeNode root) {
if(root == null) return;
System.out.print(root.val+" ");
preOrder(root.left);
preOrder(root.right);
}
// In the sequence traversal
void inOrder(TreeNode root) {
if(root == null) return;
inOrder(root.left);
System.out.print(root.val+" ");
inOrder(root.right);
}
// After the sequence traversal
void postOrder(TreeNode root) {
if(root == null) return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val+" ");
}
边栏推荐
- K. Link with Bracket Sequence I dp
- Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
- 招标信息获取
- flex布局
- Redis persistence RDB
- Mba-day28 concept of number - exercise questions
- You'd better not take this kind of project!
- Redis主从复制
- Benji Banas launched the second season of earn while playing bonus activities, supporting the use of multiple Benji passes!
- Three graduates, three years of experience in embedded software
猜你喜欢

Qt编写物联网管理平台47-通用数据库设置

ES Cluster in Red status: what about write & delete operations?

Debugging sharp weapon! A lightweight log library log.c

Establishment of log collection and analysis platform-1-environment preparation

Unity2d animator cannot create transition

Is the transaction in mysql45 isolated or not?

Why can't lpddr completely replace DDR?

金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十))

vagrant下载速度慢的解决方法

Dynamic memory management and flexible array
随机推荐
Lamp architecture
520 for what? DIY is a high-value RGB clock that girls want to watch
The idea YML file code does not prompt the solution
No EGL display error resolution
sdc中对cdc的处理方式
柠檬班自动化学习毕竟
Kingbasees SQL language reference manual of Jincang database (9. Common DDL clauses)
Why can't lpddr completely replace DDR?
Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details
ETCD数据库源码分析——Cluster membership changes日志
数据库sql语言实战
FTP experiment and overview
Servlet filter details
Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
I also found excellent software and hardware projects, all open source
Using easyexcel to import tables to realize batch insertion of xlsx files ----- MySQL of Linux
Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
LNMP architecture
vagrant下载速度慢的解决方法
