JS:使用类实现树的深度优先遍历和广度优先遍历(五颗星)
树的深度优先遍历:递归实现
树的广度优先遍历:队列实现
class Node {
constructor(data){
this.data=data;
this.children=[];
}
//深度优先遍历
dfs(){
console.log(this.data);
for(const child of this.children){
child.dfs();
}
}
//广度优先遍历
bfs() {
const queue = [];
queue.push(this);
while (queue.length) {
const node = queue.shi
剩余60%内容,订阅专栏后可继续查看/也可单篇购买
前端面试题 文章被收录于专栏
前端面试的一些常问问题、问题的具体实现(可直接运行)以及底层原理