当前位置:网站首页>树的深入和广度优先遍历(不考虑二叉树)
树的深入和广度优先遍历(不考虑二叉树)
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)
}
最近一段时间比较忙,隔了挺久总结了这一篇
边栏推荐
- Several common optimization methods matlab principle and depth analysis
- Ubuntu 14.04 下开启PHP错误提示
- PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
- 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
- 这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
- 已解决(机器学习中查看数据信息报错)AttributeError: target_names
- 8皇后问题
- 掌握Cypress命令行选项,是真正掌握Cypress的基础
- 刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
- 106. How to improve the readability of SAP ui5 application routing URL
猜你喜欢

已解决(机器学习中查看数据信息报错)AttributeError: target_names

用户和组命令练习

Introduction to the implementation principle of rxjs observable filter operator

双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆

CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team

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

PowerPoint tutorial, how to save a presentation as a video in PowerPoint?

研发团队资源成本优化实践

Libuv Library - Design Overview (Chinese version)

DQL basic query
随机推荐
Unity Render Streaming通过Js与Unity自定义通讯
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
HALCON联合C#检测表面缺陷——HALCON例程autobahn
mysql更新时条件为一查询
Heap structure and heap sort heapify
8皇后问题
MapReduce实现矩阵乘法–实现代码
The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
Students who do not understand the code can also send their own token, which is easy to learn BSC
TensorBoard可视化处理案例简析
R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
Smbms project
道路建设问题
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
Stack application (balancer)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]