当前位置:网站首页>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 shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
- Flutter动态化 | Fair 2.5.0 新版本特性
- 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
- Comprehensively develop the main channel of digital economy and digital group, and actively promote the utonmos digital Tibet market
- MySQL functions and related cases and exercises
- Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
- rxjs Observable filter Operator 的实现原理介绍
- Several common optimization methods matlab principle and depth analysis
- [redis] cache warm-up, cache avalanche and cache breakdown
- 服务器硬盘冷迁移后网卡无法启动问题
猜你喜欢
mysql更新时条件为一查询
Mycms we media mall v3.4.1 release, user manual update
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
Road construction issues
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
MySQL_ JDBC
Ocean CMS vulnerability - search php
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
Introduction to the implementation principle of rxjs observable filter operator
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
随机推荐
The solution of Chinese font garbled code in keil5
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
Ocean CMS vulnerability - search php
SQL Injection (POST/Search)
Go language unit test 3: go language uses gocovey library to do unit test
mysql更新时条件为一查询
When updating mysql, the condition is a query
研发团队资源成本优化实践
Go language unit test 4: go language uses gomonkey to test functions or methods
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
SQL Injection (POST/Select)
Swiftui development experience: the five most powerful principles that a programmer needs to master
Annotation and reflection
MapReduce implements matrix multiplication - implementation code
Students who do not understand the code can also send their own token, which is easy to learn BSC
Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
rxjs Observable filter Operator 的实现原理介绍
Task5: multi type emotion analysis
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
【被动收入如何挣个一百万】