Skip to content

Latest commit

 

History

History

convert-sorted-array-to-binary-search-tree

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Height balanced BST

If balanced is not required, we can add all number to the right node one by one.

BST and QuickSort are good friends. The pivot here is the middle of the input array.

code looks like QuickSort, too.

function sortedArrayToBST(array)
   
   root = middle of array
   
   root.left  = sortedArrayToBST(number on the root's left)
   root.right = sortedArrayToBST(number on the root's right)