当前位置:网站首页>Depth and breadth first traversal of tree (regardless of binary tree)
Depth and breadth first traversal of tree (regardless of binary tree)
2022-07-03 13:46:00 【Chicks Love Rice】
const tree = {
val: 'a',
children: [
{
val: 'b',
children: [
{
val: 'd',
children: []
},
{
val: 'e',
children: []
},
]
},
{
val: 'c',
children: [
{
val: 'f',
children: []
},
{
val: 'g',
children: []
},
]
},
]
}
Depth-first traversal
const dfs = (root) => {
console.log(root.val)
// root.children.forEach(dfs) Equate to root.children.forEach((child) => {dfs(child)})
root.children.forEach(dfs)
}
Breadth first traversal
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)
}
I've been busy recently , After a long time, I summarized this article
边栏推荐
- The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
- 全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
- PHP maze game
- Road construction issues
- Multi table query of MySQL - multi table relationship and related exercises
- JSP and filter
- 网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
- Spark practice 1: build spark operation environment in single node local mode
- 太阳底下无新事,元宇宙能否更上层楼?
- MyCms 自媒体商城 v3.4.1 发布,使用手册更新
猜你喜欢
RichView TRVStyle ListStyle 列表样式(项目符号编号)
DQL basic query
Bidirectional linked list (we only need to pay attention to insert and delete functions)
Multi table query of MySQL - multi table relationship and related exercises
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
File uploading and email sending
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
随机推荐
SQL Injection (POST/Search)
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
Go language unit test 3: go language uses gocovey library to do unit test
树的深入和广度优先遍历(不考虑二叉树)
太阳底下无新事,元宇宙能否更上层楼?
Libuv库 - 设计概述(中文版)
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
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
【556. 下一个更大元素 III】
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
[556. Next larger element III]
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
Sequence table (implemented in C language)
logback日志的整理
The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
Father and basketball
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
Red Hat Satellite 6:更好地管理服务器和云
Mycms we media mall v3.4.1 release, user manual update