Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
All PRs
Where Relevant
Description
What is this PR about ?
Sort a search tree
What was implemented ?
Sort a search tree using recursion
if(!tree.getNode()) return
Used a first recursive call to build the sorted left tree by concatenating its current node once the recursion algorithm starts to pop calls one by one in reverse order from its stack.
Used a second recursive call to build the sorted right tree through the first recursive call
Returned the combined left and right tree which are now sorted and filtered out undefined values that were pushed as the recursive calls hit an undefined node.
Runtime and space complexity: O(n x m) where n and m are the depth of the right and left trees
What did you learn ?
What a binary search tree is
How to represent a binary search tree as an object with currentNode value and nested left and right trees
How to use recursion to walk through all its nodes both in the right and left trees
How to use concatenation in a recursive algorithms to avoid having to store the result array outside of the recursive function thus introducing side effects.
What links/documents relate to this PR ?
Testing
Steps required to test
node sort-binary-tree.js
All tests should pass
Test cases can be found in interviews/recursion/sort-binary-tree/testData