当前位置:网站首页>【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)
}
边栏推荐
- 京东618会议平板排行榜公布,新锐黑马品牌会参谋角逐前三名,向国货老大华为学习
- 02_ Springboot starter case
- Learn more about redis' eight data types and application scenario analysis
- What kind of processor architecture is ARM architecture?
- 剑指 Offer 13. 机器人的运动范围
- laravel 宝塔安全配置
- 15 lines of code using mathematical formulas in wangeditor V5
- 「ARM 架构」是一种怎样的处理器架构?
- 【武汉大学】考研初试复试资料分享
- 03_SpingBoot 核心配置文件
猜你喜欢

【nvm】

What kind of processor architecture is ARM architecture?

2022 simulated 100 questions and simulated examination of high-altitude installation, maintenance and demolition

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

23研考生注意啦!备考期间最容易中招的骗局,居然是它们?!

JD 618 conference tablet ranking list announced that the new dark horse brand staff will compete for the top three, learning from Huawei, the leader of domestic products

03_SpingBoot 核心配置文件
![[untitled]](/img/ed/847e678e5a652da74d04722bbd99ff.jpg)
[untitled]

Servlet

花房集团二次IPO:成于花椒,困于花椒
随机推荐
High level application of SQL statements in MySQL database (I)
Listen to the markdown file and hot update next JS page
[Wuhan University] information sharing of the first and second postgraduate entrance examinations
推送Markdown格式信息到釘釘機器人
laravel 定时任务
Building Survey [1]
Parental delegation mechanism
Talk about GC mechanism often asked in interview
EPICS記錄參考3 -- 所有記錄都有的字段
Getting started with the go Cobra command line tool
07_SpingBoot 实现 RESTful 风格
Financial management [3]
Écoutez le fichier markdown et mettez à jour Hot next. Page JS
案例解析:用「度量」提升企业研发效能|ONES Talk
Financial management [2]
Research Report on terahertz imaging system industry - market status analysis and development prospect forecast
Building Survey [2]
2022安全员-B证考试题库及答案
laravel 创建 service层
Solution to the login error of tangdou people
