当前位置:网站首页>树的深入和广度优先遍历(不考虑二叉树)
树的深入和广度优先遍历(不考虑二叉树)
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)
}
最近一段时间比较忙,隔了挺久总结了这一篇
边栏推荐
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
- Task5: multi type emotion analysis
- PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
- SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected
- This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
- TensorBoard可视化处理案例简析
- pytorch 载入历史模型时更换gpu卡号,map_location设置
- mysql更新时条件为一查询
- Flutter动态化 | Fair 2.5.0 新版本特性
猜你喜欢

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

Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory

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

This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version

Road construction issues

Mysql database basic operation - regular expression

Libuv库 - 设计概述(中文版)

mysql更新时条件为一查询

Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code

Flutter dynamic | fair 2.5.0 new version features
随机推荐
Swiftui development experience: the five most powerful principles that a programmer needs to master
The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
Resource Cost Optimization Practice of R & D team
栈应用(平衡符)
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
Flink SQL knows why (12): is it difficult to join streams? (top)
Libuv库 - 设计概述(中文版)
DQL basic query
HALCON联合C#检测表面缺陷——HALCON例程autobahn
Introduction to the implementation principle of rxjs observable filter operator
8 Queen question
Oracle memory management
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
Detailed explanation of multithreading
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
掌握Cypress命令行选项,是真正掌握Cypress的基础
Ubuntu 14.04 下开启PHP错误提示
[sort] bucket sort
mysql更新时条件为一查询
常见的几种最优化方法Matlab原理和深度分析