当前位置:网站首页>Preorder, inorder and postorder traversal of binary tree
Preorder, inorder and postorder traversal of binary tree
2022-07-07 12:29:00 【Xiaoding Chong duck!】
One 、 Binary tree
Binary tree (binary tree) Refers to the degree of nodes in the tree No more than 2 The ordered tree of , Its nodes are divided into left and right subtrees .
Full binary tree : If a binary tree has only a degree of 0 The node and degree of are 2 The node of , And the degree is 0 The nodes of are on the same layer , Then this binary tree is a full binary tree .
Perfect binary tree : Leaf nodes can only appear in the lowest and lower layers , And the lowest leaf nodes are concentrated on the left of the tree .
Two 、 The generation of binary tree
function NodeTree (value) {
this.value = value;
this.left = null;
this.right = null;
}
let root = new NodeTree('A');
let t1 = new NodeTree('B');
let t2 = new NodeTree('C');
let t3 = new NodeTree('D');
let t4 = new NodeTree('E');
let t5 = new NodeTree('F');
root.left = t1;
root.right = t2;
t1.left = t3;
t1.right = t4;
t2.left = t5;
console.log(root);The generated binary tree is as follows :


3、 ... and 、 Traversal of binary tree
1、 The former sequence traversal
Preorder traversal is to traverse the root node first , Then traverse the left subtree , Finally, traverse the right subtree , namely root —> Left —> Right
The code is as follows :
/**
* The former sequence traversal
* @param {NodeTree} root
*/
let formerSequenceTraversal = function (root) {
if (!root || root.value === null) {
return null;
}
console.log(root.value);
formerSequenceTraversal(root.left);
formerSequenceTraversal(root.right);
}The output is as follows :
A、B、D、E、C、F
2、 In the sequence traversal
Middle order traversal is to traverse the left subtree first , Then traverse the root node , Finally, traverse the right subtree , namely Left —> root —> Right
The code is as follows :
/**
* In the sequence traversal
* @param {NodeTree} root
*/
let middleSequenceTraversal = function (root) {
if (!root || root.value === null) {
return null;
}
middleSequenceTraversal(root.left);
console.log(root.value);
middleSequenceTraversal(root.right);
}The output is as follows :
D、B、E、A、F、C
3、 After the sequence traversal
Post order traversal is to traverse the left subtree first , Traversing right subtree again , Finally, traverse the root node , namely Left —> Right —> root
The code is as follows :
/**
* After the sequence traversal
* @param {NodeTree} root
*/
let afterSequenceTraversal = function (root) {
if (!root || root.value === null) {
return null;
}
afterSequenceTraversal(root.left);
afterSequenceTraversal(root.right);
console.log(root.value);
}The output is as follows :
D、E、B、F、C、A
边栏推荐
- Inverted index of ES underlying principle
- 数据库系统原理与应用教程(011)—— 关系数据库
- sql-lab (54-65)
- @Bean与@Component用在同一个类上,会怎么样?
- 【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
- What is an esp/msr partition and how to create an esp/msr partition
- Detailed explanation of debezium architecture of debezium synchronization
- 《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
- 防红域名生成的3种方法介绍
- 【深度学习】图像多标签分类任务,百度PaddleClas
猜你喜欢

数据库系统原理与应用教程(007)—— 数据库相关概念

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6

Sonar:cognitive complexity
![111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]](/img/2e/da45198bb6fb73749809ba0c4c1fc5.png)
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]

SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)

SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)

The IDM server response shows that you do not have permission to download the solution tutorial

Hi3516 full system type burning tutorial

@Bean与@Component用在同一个类上,会怎么样?

Sonar:Cognitive Complexity认知复杂度
随机推荐
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
Review and arrangement of HCIA
Tutorial on the principle and application of database system (011) -- relational database
What are the top-level domain names? How is it classified?
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
What is an esp/msr partition and how to create an esp/msr partition
Configure an encrypted web server
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
Several methods of checking JS to judge empty objects
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Tutorial on principles and applications of database system (009) -- conceptual model and data model
问题:先后键入字符串和字符,结果发生冲突
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
EPP+DIS学习之路(2)——Blink!闪烁!
Static vxlan configuration
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
数据库系统原理与应用教程(009)—— 概念模型与数据模型
110. Network security penetration test - [privilege promotion 8] - [windows sqlserver xp_cmdshell stored procedure authorization]
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
Sonar:cognitive complexity