What is your solution suggestion?
solution
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.inorder = function(){
if(this.root === null){
return null;
}
let nodes = [];
function leftDataRight(root){
if (root.left){
leftDataRight(root.left)
}
nodes.push(root.value);
if (root.right) {
leftDataRight(root.right);
}
}
leftDataRight(this.root);
return nodes;
}
this.preorder = function(){
if(this.root === null){
return null;
}
let nodes = [];
function dataLeftRight(root){
nodes.push(root.value);
if (root.left){
dataLeftRight(root.left)
}
if (root.right) {
dataLeftRight(root.right);
}
}
dataLeftRight(this.root);
return nodes;
}
this.postorder = function(){
if(this.root === null){
return null;
}
let nodes = [];
function leftRightData(root){
if (root.left){
leftRightData(root.left)
}
if (root.right) {
leftRightData(root.right);
}
nodes.push(root.value);
}
leftRightData(this.root);
return nodes;
}
// Only change code above this line
}
Challenge: Use Depth First Search in a Binary Search Tree
Link to the challenge: