当前位置:网站首页>【js】-【树】-学习笔记
【js】-【树】-学习笔记
2022-06-24 19:42:00 【有趣的学习】
声明:本笔记是根据掘金小册总结的,如果要学习更多细节,请移步https://juejin.cn/book/6844733800300150797
1 二叉树
在 JS 中,二叉树使用对象来定义。它的结构分为三块:
数据域
左侧子结点(左子树根结点)的引用
右侧子结点(右子树根结点)的引用
- 定义二叉树构造函数
// 二叉树结点的构造函数
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
- 新建一个二叉树结点
const node = new TreeNode(1)
1.1 先序遍历
// 所有遍历函数的入参都是树的根结点对象
function preorder(root) {
# 递归边界,root 为空
if(!root) {
return
}
# 输出当前遍历的结点值
console.log('当前遍历的结点值是:', root.val)
// 递归遍历左子树
preorder(root.left)
// 递归遍历右子树
preorder(root.right)
}
1.2 中序遍历
# 所有遍历函数的入参都是树的根结点对象
function inorder(root) {
// 递归边界,root 为空
if(!root) {
return
}
// 递归遍历左子树
inorder(root.left)
// 输出当前遍历的结点值
console.log('当前遍历的结点值是:', root.val)
// 递归遍历右子树
inorder(root.right)
}
1.3 后序遍历
function postorder(root) {
// 递归边界,root 为空
if(!root) {
return
}
// 递归遍历左子树
postorder(root.left)
// 递归遍历右子树
postorder(root.right)
// 输出当前遍历的结点值
console.log('当前遍历的结点值是:', root.val)
}
边栏推荐
- Recommended course: workplace writing training
- Financial management [3]
- Accounting standards for business enterprises application [5]
- vulnhub DC: 2
- Servlet
- laravel model 注意事项
- Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
- Solution to the login error of tangdou people
- MySQL夺命10问,你能坚持到第几问?
- 花房集团二次IPO:成于花椒,困于花椒
猜你喜欢

推送Markdown格式信息到釘釘機器人
![[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

How to submit the shopee opening and settlement flow?

docker安装mysql-简单无坑

【nvm】

Learn about redlock

Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine

Dynamic menu, auto align

Beijiafu (p+f) R2000 modified radar IP

EPICS记录参考2--EPICS过程数据库概念
随机推荐
[ROS play with turtle turtle]
Uncover the secrets of Huawei cloud enterprise redis issue 16: acid'true' transactions beyond open source redis
Research Report on market evaluation and investment direction of Chinese dermatology drugs (2022 Edition)
01_SpingBoot 框架入门
China Sky Lantern market trend report, technical dynamic innovation and market forecast
Financial management [6]
go Cobra命令行工具入门
Are you afraid of being asked MySQL related questions during the interview? This 30000 word essence summary + 100 interview questions, and it's enough to hang the interviewer
Dig deep into MySQL - resolve the difference between clustered and non clustered indexes
Dynamic menu, auto align
花房集团二次IPO:成于花椒,困于花椒
The extra points and sharp tools are worthy of the trust | know that Chuangyu won the letter of thanks from the defense side of the attack and defense drill!
EPICS record Reference 3 - - field available for all Records
How to integrate Huawei cloud function services in fluent
Servlet
Getting started with the go Cobra command line tool
laravel model 注意事项
The large-scale market of graduate dormitory! Here comes the enviable graduate dormitory!
Concurrent shared model management
Paddledtx v1.0 has been released, and its security and flexibility have been comprehensively improved!
