当前位置:网站首页>树的深入和广度优先遍历(不考虑二叉树)
树的深入和广度优先遍历(不考虑二叉树)
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)
}
最近一段时间比较忙,隔了挺久总结了这一篇
边栏推荐
- Logseq 评测:优点、缺点、评价、学习教程
- MySQL installation, uninstallation, initial password setting and general commands of Linux
- The network card fails to start after the cold migration of the server hard disk
- In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
- 使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
- Task5: multi type emotion analysis
- 双向链表(我们只需要关注插入和删除函数)
- 今日睡眠质量记录77分
- Flink SQL knows why (13): is it difficult to join streams? (next)
- PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
猜你喜欢

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

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

Mycms we media mall v3.4.1 release, user manual update

MySQL functions and related cases and exercises

8皇后问题

今日睡眠质量记录77分

MySQL_ JDBC

Several common optimization methods matlab principle and depth analysis

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

When we are doing flow batch integration, what are we doing?
随机推荐
The reasons why there are so many programming languages in programming internal skills
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
Red Hat Satellite 6:更好地管理服务器和云
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
JSP and filter
研发团队资源成本优化实践
Kivy tutorial how to automatically load kV files
Mycms we media mall v3.4.1 release, user manual update
人身变声器的原理
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
DQL basic query
Flutter动态化 | Fair 2.5.0 新版本特性
Setting up Oracle datagurd environment
Spark practice 1: build spark operation environment in single node local mode
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
【被动收入如何挣个一百万】