当前位置:网站首页>树的深入和广度优先遍历(不考虑二叉树)
树的深入和广度优先遍历(不考虑二叉树)
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)
}
最近一段时间比较忙,隔了挺久总结了这一篇
边栏推荐
- CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
- rxjs Observable filter Operator 的实现原理介绍
- 106. 如何提高 SAP UI5 应用路由 url 的可读性
- Mysql database basic operation - regular expression
- Kivy教程之 如何自动载入kv文件
- 显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
- Asp. Net core1.1 without project JSON, so as to generate cross platform packages
- mysql中的字段问题
- JS convert pseudo array to array
- Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
猜你喜欢

Flink code is written like this. It's strange that the window can be triggered (bad programming habits)

Complete deep neural network CNN training with tensorflow to complete picture recognition case 2

Detailed explanation of multithreading

The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can

Unity EmbeddedBrowser浏览器插件事件通讯

MyCms 自媒体商城 v3.4.1 发布,使用手册更新

Flink SQL knows why (13): is it difficult to join streams? (next)

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL

使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2

Mycms we media mall v3.4.1 release, user manual update
随机推荐
道路建设问题
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
Libuv Library - Design Overview (Chinese version)
mysql中的字段问题
Swiftui development experience: the five most powerful principles that a programmer needs to master
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
[redis] cache warm-up, cache avalanche and cache breakdown
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
Task5: multi type emotion analysis
Unity render streaming communicates with unity through JS
106. How to improve the readability of SAP ui5 application routing URL
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
JSP and filter
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
Detailed explanation of multithreading
常见的几种最优化方法Matlab原理和深度分析