diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md index 2c5bbc1bef..2ec80e96e9 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md @@ -106,7 +106,42 @@ BinarySearchTree.prototype = Object.assign(
```js -// solution required +var displayTree = tree => console.log(JSON.stringify(tree, null, 2)); +function Node(value) { + this.value = value; + this.left = null; + this.right = null; +} +function BinarySearchTree() { + this.root = null; + // change code below this line + this.levelOrder = (root = this.root) => { + if(!root) return null; + let queue = [root]; + let results = []; + while(queue.length > 0) { + let node = queue.shift(); + results.push(node.value); + if(node.left) queue.push(node.left); + if(node.right) queue.push(node.right); + } + return results; + } + + this.reverseLevelOrder = (root = this.root) => { + if(!root) return null; + let queue = [root]; + let results = [] ; + while ( queue.length > 0) { + let node = queue.shift(); + results.push(node.value); + if(node.right) queue.push(node.right); + if(node.left ) queue.push(node.left); + } + return results; + } + // change code above this line +} ```