treeDFSelect
Written on July 23, 2019
문제
Implement a DFSelect
method on this Tree class.
DFSelect accepts a filter function, calls that function on each of the nodes in Depth 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.DFSelect = function(filter) {
let filtered = [];
function traversing(node, depth) {
if (filter(node.value, depth)) {
filtered.push(node.value);
}
if (node.children.length > 0) {
for (let i = 0; i < node.children.length; i++) {
traversing(node.children[i], depth + 1);
}
} else {
return;
}
}
traversing(this, 0);
return filtered;
};
👩🏻💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻♀️
in the process of becoming the best version of myself