diff --git a/coding_interviews/algoexpert/depth-first-search/depth-first-search-cleaner.js b/coding_interviews/algoexpert/depth-first-search/depth-first-search-cleaner.js new file mode 100644 index 0000000..0e89890 --- /dev/null +++ b/coding_interviews/algoexpert/depth-first-search/depth-first-search-cleaner.js @@ -0,0 +1,19 @@ +class Node { + constructor(name) { + this.name = name; + this.children = []; + } + + addChild(name) { + this.children.push(new Node(name)); + return this; + } + + depthFirstSearch(array) { + array.push(this.name); + for (let child of this.children) { + child.depthFirstSearch(array); + } + return array; + } +}