2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 587d8258367417b2b2512c81
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Delete a Node with One Child in a Binary Search Tree
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 1
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301638
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: delete-a-node-with-one-child-in-a-binary-search-tree
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Now that we can delete leaf nodes let's move on to the second case: deleting a node with one child. For this case, say we have a tree with the following nodes 1 — 2 — 3 where 1 is the root. To delete 2, we simply need to make the right reference in 1 point to 3. More generally to delete a node with only one child, we make that node's parent reference the next node in the tree.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								We've provided some code in our `remove`  method that accomplishes the tasks from the last challenge. We find the target to delete and its parent and define the number of children the target node has. Let's add the next case here for target nodes with only one child. Here, we'll have to determine if the single child is a left or right branch in the tree and then set the correct reference in the parent to point to this node. In addition, let's account for the case where the target is the root node (this means the parent node will be `null` ). Feel free to replace all the starter code with your own as long as it passes the tests.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `BinarySearchTree`  data structure should exist.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return typeof test == 'object';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The binary search tree should have a method called `remove` .
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return typeof test.remove == 'function';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2019-07-18 17:32:12 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Trying to remove an element that does not exist should return `null` .
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.remove(100) == null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If the root node has no children, deleting it should set the root to `null` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    test.add(500);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(500);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.inorder() == null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `remove`  method should remove leaf nodes from the tree.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(5);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(6);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(12);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(12);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.inorder().join('') == '567';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `remove`  method should remove nodes with one child.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(-1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(16);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(16);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(7);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(3);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.inorder().join('') == '-1';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Removing the root in a tree with two nodes should set the second to be the root.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (function () {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var test = false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof BinarySearchTree !== 'undefined') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      test = new BinarySearchTree();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (typeof test.remove !== 'function') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return false;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(15);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.add(27);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    test.remove(15);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return test.inorder().join('') == '27';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 13:04:05 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								BinarySearchTree.prototype = Object.assign(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  BinarySearchTree.prototype,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    add: function(value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      var node = this.root;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (node == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        this.root = new Node(value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        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;
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 13:04:05 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        return searchTree(node);
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 13:04:05 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    inorder: function() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (this.root == null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        var result = new Array();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        function traverseInOrder(node) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (node.left != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            traverseInOrder(node.left);
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 13:04:05 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          result.push(node.value);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          if (node.right != null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            traverseInOrder(node.right);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 13:04:05 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        traverseInOrder(this.root);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return result;
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2019-06-20 13:16:31 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 13:04:05 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --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;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  this.remove = function(value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (this.root === null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var target;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var parent = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Find the target value and its parent
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    (function findValue(node = this.root) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (value == node.value) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        target = node;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else if (value <  node.value  & &  node . left  ! = =  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        parent = node;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return findValue(node.left);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else if (value <  node.value  & &  node . left  = ==  null )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else if (value > node.value & &  node.right !== null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        parent = node;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return findValue(node.right);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }.bind(this)());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (target === null) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Count the children of the target to delete
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var children =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      (target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Case 1: Target has no children
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (children === 0) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (target == this.root) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        this.root = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (parent.left == target) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          parent.left = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          parent.right = null;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Case 2: Target has one child
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Only change code below this line
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// solution required
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```