当前位置:网站首页>树的深入和广度优先遍历(不考虑二叉树)
树的深入和广度优先遍历(不考虑二叉树)
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)
}
最近一段时间比较忙,隔了挺久总结了这一篇
边栏推荐
- The principle of human voice transformer
- KEIL5出现中文字体乱码的解决方法
- Flink SQL knows why (12): is it difficult to join streams? (top)
- Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
- 掌握Cypress命令行选项,是真正掌握Cypress的基础
- 父亲和篮球
- Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
- The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
- 顺序表(C语言实现)
- Unity EmbeddedBrowser浏览器插件事件通讯
猜你喜欢

Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication

物联网毕设 --(STM32f407连接云平台检测数据)

MySQL

双向链表(我们只需要关注插入和删除函数)

Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)

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

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

Error running 'application' in idea running: the solution of command line is too long

这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
![[sort] bucket sort](/img/52/95514b5a70cea75821883e016d8adf.jpg)
[sort] bucket sort
随机推荐
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
JSP and filter
Error running 'application' in idea running: the solution of command line is too long
Father and basketball
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
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
软件测试工作那么难找,只有外包offer,我该去么?
json序列化时案例总结
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
Annotation and reflection
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
编程内功之编程语言众多的原因
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Libuv库 - 设计概述(中文版)
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
[sort] bucket sort
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
Unity EmbeddedBrowser浏览器插件事件通讯
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?