当前位置:网站首页>Binary tree traversal series 01 - recursive traversal and recursive order
Binary tree traversal series 01 - recursive traversal and recursive order
2022-04-21 12:26:00 【Xicheng Fengyu building】
Binary tree traversal series 01- Recursive traversal and recursive order
One 、 introduction
-
The first sequence traversal
Preorder traversal is also called root first traversal , seeing the name of a thing one thinks of its function , In this traversal method , First, traverse the root node , Then traverse the left child of the root node first , Then traverse the right child of the root node in order , From this traversal rule , We can see that , There is an obvious recursive meaning , Therefore, it is a common method to use recursive method to realize the pre order traversal of the tree , It's simple
-
In the sequence traversal
In the sequence traversal , Unlike preorder traversal , The middle order traversal does not access the root node first , The middle order traversal first accesses the left child of the root node , Then access the root node , Finally, access the right child of the root node , The meaning of this is obvious , Is to put the traversal of the root node in the middle of the left child and the right child
-
After the sequence traversal
With the introduction of preorder traversal and middle order traversal , The post order traversal is very straightforward , First visit the left child of the root node , Then access the right child of the root node , Finally, visit Gen , Although post order traversal sounds as simple as pre order and middle order , In fact, the recursive implementation of subsequent traversal is also very simple , But the non recursive implementation of post order traversal is the most complex
Two 、 Three traversal recursive implementations
2.1 The first sequence traversal
public void preOrder(TreeNode root) {
if (root == null) {
return;
}
Systen.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
2.2 In the sequence traversal
public void inOrder(TreeNode root) {
if (root == null) {
return;
}
preOrder(root.left);
Systen.out.println(root.val);
preOrder(root.right);
}
2.3 After the sequence traversal
public void postOrder(TreeNode root) {
if (root == null) {
return;
}
preOrder(root.left);
preOrder(root.right);
Systen.out.println(root.val);
}
3、 ... and 、 Discussion on the generality of recursive writing
The above three recursive traversals can be written as follows
public void visit(TreeNode root) {
if (root == null) {
return;
}
// If you deal with it here , So that's the order
visit(root.left);
// If you deal with it here , So it's the middle order
visit(root.right);
// If you deal with it here , So that's the sequence
}
3.1 Recursive graph and recursive order
We can draw the above
visitRecursive graph of , As shown in the figure below , The red circle indicates the order of access , among null Nodes also need to be accessed once , According to the above recursive access order , You can get the access order of each element :1、2、4、7、7、7、4、4、2、5、5、5、2、1、3、3、6、6、6、3、1, This is the recursive order , By observing the recursive order , We can find out , Each element appears three times , Why? , In fact, we can easily draw this conclusion by observing the code :public void visit(TreeNode root) { if (root == null) { return; } // root First visit visit(root.left); // After the previous layer of recursion ends , Back to the current recursive stack ,root Second visit visit(root.right); // root Visited for the third time }in other words , Traversal of first order, middle order and second order , It's essentially the same thing , The difference is that , In the current recursive order , When an element appears for several times, it is processed , Preorder is to deal with... The first time an element appears , Middle order is to process the element the second time it appears , Post order is to process the element when it last appears

版权声明
本文为[Xicheng Fengyu building]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211221415607.html
边栏推荐
猜你喜欢

vscode 经常弹出:尝试在目标目录创建文件时发生一个错误 重试 跳过这个文件 关闭安装程序

伯克利、三星|一个快速的训练后变换器修剪框架

虚拟货币已然没有市场,为何还能对一些人产生不小的诱惑?

BEVSegFormer:一个来自任意摄像头的BEV语义分割方法

SKU中的销售属性值必须成对填写,那这是什么原因

最佳实践 | 通过使用 Jira Service Management 改进 HR 工作流程

Chris LATTNER, father of llvm: the golden age of compilers

马斯克活在旧互联网时代?

新技术又来了,拥抱AGP7.0,准备好告别Transform了吗?

Machine learning-sklearn-13 (regression family - lower - nonlinear problem: polynomial regression (a new characteristic matrix is formed after polynomial transformation))
随机推荐
There is no market for virtual currency. Why can there be no small temptation for some people?
Three.js学习项目--3D抗美援朝数据可视化
顶流“健身博主”刘畊宏
import in protocol buffer
win11的WiFi按钮不见了无法联网
[Software Testing Series II] software testing process specification
[software test series x] stress test scheme
Daily AI frontier terminology: active learning
数据库,把另一张表某字段数据添加到另一个表中,同时加入的时候加入自己需要的数据
[software test series 12] stress test report template
open-mmlab / mmpose安装、使用教程
Web--用户注册界面
Machine learning-sklearn-13 (regression family - lower - nonlinear problem: polynomial regression (a new characteristic matrix is formed after polynomial transformation))
关于ros版本问题导致MarkerArray的不显示解决
团队中听谁的
Couleurs du thème sublime
【软件测试系列二】《软件测试流程规范》
【数据可视化应用】绘制双变量映射地图(附R语言代码)
最长上升子序列(二)(贪心+二分)
Reverse crawler 30 verification code of a fourth generation slider