From 5f894a3231c91db2a3d478cad6e76be0593cea07 Mon Sep 17 00:00:00 2001 From: Edwin van Meerendonk Date: Wed, 20 Mar 2019 15:17:00 +0100 Subject: [PATCH] A solution was missing (#35552) My solution --- ...-search-in-a-binary-search-tree.english.md | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md index 3fcf953054..7ca360ff8e 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree.english.md @@ -116,6 +116,45 @@ BinarySearchTree.prototype = {
```js -// solution required +var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2)); +function Node(value) { + this.value = value; + this.left = null; + this.right = null; +} +function BinarySearchTree() { + this.root = null; + this.result = []; + + this.inorder = function(node) { + if (!node) node = this.root; + if (!node) return null; + + if (node.left) this.inorder(node.left); + this.result.push(node.value) + if (node.right) this.inorder(node.right); + return this.result; + + }; + this.preorder = function(node) { + if (!node) node = this.root; + if (!node) return null; + + this.result.push(node.value); + if (node.left) this.preorder(node.left); + if (node.right) this.preorder(node.right); + return this.result; + }; + this.postorder = function(node) { + if (!node) node = this.root; + if (!node) return null; + + if (node.left) this.postorder(node.left); + if (node.right) this.postorder(node.right); + this.result.push(node.value); + + return this.result; + }; +} ```