Minimal Tree
4.2 Minimal Tree: Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.
This algorithm can be solved by continuously taking the midpoint of the array and inserting it into the Binary search tree using level order insertion.
Or even more simply, this algorithm follows a pre-order traversal pattern: ->access, ->left, -> right. We can apply this recursive procedure using our midpoint rule.
I've referenced below an image that describes the way I went about this process.
I've noticed that procedure of belowing the tree comes out to be a level order tranversal (or BFS). Each node represents an low and high that can calculate the index of nums to insert into the tree next. In addition every subsequential node after that is based on its previous.
This follows the BST route, albeit it has been modified to print out actually level by level.
All that is required next of this is to simplify insert the elements printed out in the for loop.
Last updated