当前位置:网站首页>树的深入和广度优先遍历(不考虑二叉树)
树的深入和广度优先遍历(不考虑二叉树)
2022-07-03 13:12:00 【小鸡爱吃米】
const tree = {
val: 'a',
children: [
{
val: 'b',
children: [
{
val: 'd',
children: []
},
{
val: 'e',
children: []
},
]
},
{
val: 'c',
children: [
{
val: 'f',
children: []
},
{
val: 'g',
children: []
},
]
},
]
}
深度优先遍历
const dfs = (root) => {
console.log(root.val)
// root.children.forEach(dfs)等同于root.children.forEach((child) => {dfs(child)})
root.children.forEach(dfs)
}
广度优先遍历
const bfs = () => {
const q = [root]
while(q.length > 0) {
const n = q.shift()
console.log(n.val)
n.children.forEach((child) => {
q.push(child)})
}
bfs(tree)
}
最近一段时间比较忙,隔了挺久总结了这一篇
边栏推荐
- When updating mysql, the condition is a query
- Multi table query of MySQL - multi table relationship and related exercises
- CVPR 2022 | 美团技术团队精选6篇优秀论文解读
- Spark practice 1: build spark operation environment in single node local mode
- JS 将伪数组转换成数组
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- 阿南的疑惑
- Ubuntu 14.04 下开启PHP错误提示
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
猜你喜欢
When we are doing flow batch integration, what are we doing?
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
Introduction to the implementation principle of rxjs observable filter operator
TensorBoard可视化处理案例简析
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
18W word Flink SQL God Road manual, born in the sky
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
太阳底下无新事,元宇宙能否更上层楼?
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
106. 如何提高 SAP UI5 应用路由 url 的可读性
随机推荐
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Flink SQL knows why (19): the transformation between table and datastream (with source code)
mysql中的字段问题
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
106. How to improve the readability of SAP ui5 application routing URL
Task5: multi type emotion analysis
双向链表(我们只需要关注插入和删除函数)
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
掌握Cypress命令行选项,是真正掌握Cypress的基础
Red hat satellite 6: better management of servers and clouds
Father and basketball
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
18W word Flink SQL God Road manual, born in the sky
Sequence table (implemented in C language)