当前位置:网站首页>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+" ");
}
边栏推荐
- 平衡二叉树(AVL) ~
- DOM operation -- operation node
- 为什么LPDDR不能完全代替DDR?
- Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
- Dynamic memory management and flexible array
- Embedded general learning route arrangement
- Lamp architecture
- 【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
- A trick to teach you to easily understand Potter's map
- Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
猜你喜欢

How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects

软件测试面试题全网独家没有之一的资深测试工程师面试题集锦

SSH Remote Management

Using easyexcel to import tables to realize batch insertion of xlsx files ----- MySQL of Linux

解决Vagrant报错b:48:in `join‘: incompatible character encodings: GBK and UTF-8 (Encoding::Compatib

金仓数据库 KingbaseES SQL 语言参考手册 (7. 条件表达式)

leetcode-aboutString

Embedded general learning route arrangement

Etcd database source code analysis - cluster membership changes log

Redis master-slave replication
随机推荐
Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
Use latex to typeset multiple-choice test paper
Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
[cloud native] introduction and use of feign of microservices
Chapter 2 - getting started
Redis 官方可视化工具,高颜值,功能真心强大!
Unity2D 动画器无法 创建过渡
为什么LPDDR不能完全代替DDR?
光量子里程碑:6分钟内解决3854个变量问题
Etcd database source code analysis - cluster membership changes log
K. Link with Bracket Sequence I dp
Select sort / insert sort / bubble sort
柠檬班自动化学习毕竟
Why can't lpddr completely replace DDR?
Redis主从复制
Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
Another open source artifact, worth collecting and learning!
Application of canoe XML in test modules
Chapter 1 - Construction of development environment
卸载手机自带APP的操作步骤
