当前位置:网站首页>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
边栏推荐
- Cenos openssh upgrade to version 8.4
- Static routing assignment of network reachable and telent connections
- 2022 年第八届“认证杯”中国高校风险管理与控制能力挑战赛
- ps链接图层的使用方法和快捷键,ps图层链接怎么做的
- 2022 8th "certification Cup" China University risk management and control ability challenge
- An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
- 什么是ESP/MSR 分区,如何建立ESP/MSR 分区
- "Series after reading" my God! It's so simple to understand throttling and anti shake~
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- 【深度学习】图像多标签分类任务,百度PaddleClas
猜你喜欢
ENSP MPLS layer 3 dedicated line
Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
BGP actual network configuration
@Bean与@Component用在同一个类上,会怎么样?
Completion report of communication software development and Application
idm服务器响应显示您没有权限下载解决教程
【PyTorch实战】图像描述——让神经网络看图讲故事
数据库系统原理与应用教程(011)—— 关系数据库
Attack and defense world ----- summary of web knowledge points
Learning and using vscode
随机推荐
Will the filing free server affect the ranking and weight of the website?
<No. 8> 1816. Truncate sentences (simple)
Is it safe to open an account in Ping An Securities mobile bank?
[statistical learning methods] learning notes - improvement methods
Epp+dis learning path (1) -- Hello world!
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
Static routing assignment of network reachable and telent connections
RHSA first day operation
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
SQL head injection -- injection principle and essence
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
What are the technical differences in source code anti disclosure
[deep learning] image multi label classification task, Baidu paddleclas
Typescript interface inheritance
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Zero shot, one shot and few shot
"Series after reading" my God! It's so simple to understand throttling and anti shake~
SQL blind injection (WEB penetration)