当前位置:网站首页>[JS] - [tree] - learning notes
[JS] - [tree] - learning notes
2022-06-24 23:17:00 【Interesting learning】
【js】-【 Tree Foundation 】- Learning notes
Statement : This note is based on the Nuggets brochure , If you want to learn more details , Please move https://juejin.cn/book/6844733800300150797
1 Binary tree
stay JS in , Binary trees use objects to define . Its structure is divided into three parts :
Data fields
Left child node ( The root node of the left subtree ) References to
Right child node ( The root node of the right subtree ) References to
- Define a binary tree constructor
// The constructor of a binary tree node
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
- Create a new binary tree node
const node = new TreeNode(1)
1.1 The first sequence traversal
// The input parameters of all traversal functions are the root node objects of the tree
function preorder(root) {
# Recursive boundary ,root It's empty
if(!root) {
return
}
# Output the current traversal node value
console.log(' The node value currently traversed is :', root.val)
// Recursively traverses the left subtree
preorder(root.left)
// Recursively traverses the right subtree
preorder(root.right)
}
1.2 In the sequence traversal
# The input parameters of all traversal functions are the root node objects of the tree
function inorder(root) {
// Recursive boundary ,root It's empty
if(!root) {
return
}
// Recursively traverses the left subtree
inorder(root.left)
// Output the current traversal node value
console.log(' The node value currently traversed is :', root.val)
// Recursively traverses the right subtree
inorder(root.right)
}
1.3 After the sequence traversal
function postorder(root) {
// Recursive boundary ,root It's empty
if(!root) {
return
}
// Recursively traverses the left subtree
postorder(root.left)
// Recursively traverses the right subtree
postorder(root.right)
// Output the current traversal node value
console.log(' The node value currently traversed is :', root.val)
}
边栏推荐
- What kind of processor architecture is ARM architecture?
- Laravel message queue
- Selection (027) - what is the output of the following code?
- Uip1.0 active sending problem understanding
- Laravel 认证模块 auth
- Servlet
- 对抗训练理论分析:自适应步长快速对抗训练
- 研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
- Tech talk activity review kubernetes skills of cloud native Devops
- Push markdown format information to the nailing robot
猜你喜欢

研究生宿舍大盘点!令人羡慕的研究生宿舍来了!
How should we measure agile R & D projects?

idea创建模块提示已存在

Uncover the secrets of Huawei cloud enterprise redis issue 16: acid'true' transactions beyond open source redis

2022 safety officer-b certificate examination question bank and answers

花房集团二次IPO:成于花椒,困于花椒

案例解析:用「度量」提升企业研发效能|ONES Talk
![[Wuhan University] information sharing of the first and second postgraduate entrance examinations](/img/ec/884e656a921e20a5679a2960c9ac4d.jpg)
[Wuhan University] information sharing of the first and second postgraduate entrance examinations

伪原创智能改写api百度-收录良好

Servlet
随机推荐
Laravel user authorization
监听 Markdown 文件并热更新 Next.js 页面
golang convert json string to map
sql -CONVERT函数
RT-thread使用rt-kprintf
docker安装redis-简单而无坑
Concurrent shared model management
[text data mining] Chinese named entity recognition: HMM model +bilstm_ CRF model (pytoch) [research and experimental analysis]
#22Map介绍与API
【js】-【字符串-应用】- 学习笔记
Record the range of data that MySQL update will lock
03_ Spingboot core profile
Laravel message queue
A big factory interview must ask: how to solve the problem of TCP reliable transmission? 8 pictures for you to learn in detail
[postgraduate entrance examination English] prepare for 2023, learn list9 words
Epics record reference 4 -- fields for all input records and fields for all output records
Docker-mysql8-master-slave
Tech talk activity review kubernetes skills of cloud native Devops
Simulated 100 questions and online simulated examination of high voltage electrician examination in 2022
go Cobra命令行工具入门
