
实现源码
class node {constructor(value) {this.value = value;this.child = [];}}const a = new node('a');const c = new node('c');const f = new node('f');const b = new node('b');const d = new node('d');const e = new node('e');a.child.push(c);a.child.push(f);a.child.push(b);b.child.push(d);b.child.push(e);/*** 树的广度优先搜索* @param {*} roots* @param {*} target* @returns*/function fls (roots, target) {if(roots == null || roots.length == 0) return false;let child = [];for (let i = 0; i < roots.length; i++ ) {if(roots[i].value == target ) {return true;} else {child = child.concat(roots[i].child);}}return fls(child, target);}console.log(fls([a], 'e'));
