Generate binary tree from sorted array

Generate binary tree from sorted array

Algorithm:
  • Take middle of array,  mid = (start + end) / 2
  • Create a new node with data set array[mid] and
  • recursively call same for left and right child.
  • leftChild = generate(array, start, mid – 1)
  • rightChild = generate(array, mid + 1, end);

Source Code:
Github: BinarySearchTreeGenerator.java


Output:

   8       
  / \   
 /   \  
 2   16   
  \   \ 
  5   20 
          

Author: Hrishikesh Mishra