Create Binary Tree From List

Create Binary Tree From List

Given Linked List Representation of Complete Binary Tree, construct the Binary tree.
Note : The complete binary tree is represented as an linked list in a way where If root node is stored at position i, its left, and right children are stored at position 2*i+1, 2*i+2 respectively.

Solution:

  • Create tree in post order
Algorithm:
  1. If index >= list.size then,
    1. return null
  2. Create node with list.get(index)
  3. node.left = Call recursively to create tree with 2 * index + 1
  4. node.right = Call recursively to create tree with 2 * index + 2
  5. Return node

Latest Source Code:
Github: ListToCompleteBinaryTreeCreator.java


Output:

List is : [10, 12, 15, 25, 30, 36]
Tree is : 
   10       
  / \   
 /   \  
 12   15   
/ \ /   
25 30 36   
               
Author: Hrishikesh Mishra