treeBFSelect
Written on August 5, 2019
문제
Implement a BFSelect
method on this Tree class.
BFSelect accepts a filter function, calls that function on each of the nodes in Breadth First order, and returns a flat array of node values of the tree for which the filter returns true.
풀이
var Tree = function(value) {
this.value = value;
this.children = [];
};
Tree.prototype.BFSelect = function(filter) {
let filteredResult = [];
const search = (node, depth) => {
if (filter(node.value, depth)) {
filteredResult.push(node.value);
}
if (node.children.length > 0) {
for (let i = 0; i < node.children.length; i++) {
search(node.children[i], depth + 1);
}
} else {
return;
}
};
search(this, 0);
return filteredResult;
};
👩🏻💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻♀️
in the process of becoming the best version of myself