diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md index 7e299cfa59..c97d47c09a 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/invert-a-binary-tree.english.md @@ -39,14 +39,14 @@ tests: ```js var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2)); function Node(value) { - this.value = value; - this.left = null; - this.right = null; + this.value = value; + this.left = null; + this.right = null; } function BinarySearchTree() { - this.root = null; - // change code below this line - // change code above this line + this.root = null; + // change code below this line + // change code above this line } ``` @@ -114,6 +114,28 @@ 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; + // change code below this line + this.invert = function(node = this.root) { + if (node) { + const temp = node.left; + node.left = node.right; + node.right = temp; + this.invert(node.left); + this.invert(node.right); + } + return node; + } + // change code above this line +} + ``` +
diff --git a/guide/english/certifications/coding-interview-prep/data-structures/invert-a-binary-tree/index.md b/guide/english/certifications/coding-interview-prep/data-structures/invert-a-binary-tree/index.md index 3a8f2d5119..e9c51bb075 100644 --- a/guide/english/certifications/coding-interview-prep/data-structures/invert-a-binary-tree/index.md +++ b/guide/english/certifications/coding-interview-prep/data-structures/invert-a-binary-tree/index.md @@ -3,8 +3,57 @@ title: Invert a Binary Tree --- ## Invert a Binary Tree -This is a stub. Help our community expand it. -This quick style guide will help ensure your pull request gets accepted. - +## Hint: 1 + +Create a invert(node = this.root) method in the BinarySearchTree constructor function. + +> _try to solve the problem now_ + + +## Hint: 2 + +Try to use recursion and think of a base case. + +> _try to solve the problem now_ + + +## Spoiler Alert! + +**Solution ahead!** + + +## Basic Code Solution: + +```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; + // change code below this line + this.invert = function(node = this.root) { + if (node) { + const temp = node.left; + node.left = node.right; + node.right = temp; + this.invert(node.left); + this.invert(node.right); + } + return node; + } + // change code above this line +} +``` + +Run Code +### Code Explanation: +* Using recursion will allow you to traverse each node once and the only extra memory used is the auxiliary temp variable that enables you to swap. You keep swapping the left and right pointers of a node until you reach the leaves which will not do anything as the left and right of them are null references. +## NOTES FOR CONTRIBUTIONS: +**DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution. +* Add an explanation of your solution. +* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**.