Update solution for use-breadth-first-search-in-a-binary-search-tree.english.md (#38221)
* Update use-breadth-first-search-in-a-binary-search-tree.english.md * Update curriculum/challenges/english/08-coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree.english.md Committed suggested changes Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> * add debugging function to solution Ensured seed code and solution matched-up. Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com> Co-authored-by: Tom <20648924+moT01@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+Sky020@users.noreply.github.com>pull/38042/head
parent
c149eb21f9
commit
18ca64ed4e
|
@ -106,7 +106,42 @@ BinarySearchTree.prototype = Object.assign(
|
|||
<section id='solution'>
|
||||
|
||||
```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
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
|
Loading…
Reference in New Issue