2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 587d8258367417b2b2512c7f
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Use Breadth First Search in a Binary Search Tree
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301718
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: use-breadth-first-search-in-a-binary-search-tree
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Here we will introduce another tree traversal method: breadth-first search. In contrast to the depth-first search methods from the last challenge, breadth-first search explores all the nodes in a given level within a tree before continuing on to the next level. Typically, queues are utilized as helper data structures in the design of breadth-first search algorithms.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In this method, we start by adding the root node to a queue. Then we begin a loop where we dequeue the first item in the queue, add it to a new array, and then inspect both its child subtrees. If its children are not null, they are each enqueued. This process continues until the queue is empty.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Let's create a breadth-first search method in our tree called `levelOrder` . This method should return an array containing the values of all the tree nodes, explored in a breadth-first manner. Be sure to return the values in the array, not the nodes themselves. A level should be traversed from left to right. Next, let's write a similar method called `reverseLevelOrder`  which performs the same search but in the reverse direction (right to left) at each level.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `BinarySearchTree`  data structure should exist.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return typeof test == 'object';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The binary search tree should have a method called `levelOrder` .
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return typeof test.levelOrder == 'function';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The binary search tree should have a method called `reverseLevelOrder` .
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return typeof test.reverseLevelOrder == 'function';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `levelOrder`  method should return an array of the tree node values explored in level order.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof test.levelOrder !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(9);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(0);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(8);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(2);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(4);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(6);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.levelOrder().join('') == '719038102546';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `reverseLevelOrder`  method should return an array of the tree node values explored in reverse level order.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof test.reverseLevelOrder !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(9);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(0);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(8);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(2);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(4);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(6);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.reverseLevelOrder().join('') == '791108305264';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `levelOrder`  method should return `null`  for an empty tree.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof test.levelOrder !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.levelOrder() == null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `reverseLevelOrder`  method should return `null`  for an empty tree.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof test.reverseLevelOrder !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.reverseLevelOrder() == null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-10 18:03:03 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-08-13 17:24:35 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								BinarySearchTree.prototype = Object.assign(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  BinarySearchTree.prototype,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    add: function(value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      function searchTree(node) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (value <  node.value )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (node.left == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            node.left = new Node(value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          } else if (node.left != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return searchTree(node.left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else if (value > node.value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (node.right == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            node.right = new Node(value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          } else if (node.right != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            return searchTree(node.right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      var node = this.root;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (node == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        this.root = new Node(value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return searchTree(node);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only change code below this line
							 
						 
					
						
							
								
									
										
										
										
											2021-02-06 04:42:36 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Only change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 00:37:30 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only 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;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only change code above this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```