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:
- If
index >= list.size
then,return null
- Create node with list.get(index)
node.left = Call recursively to create tree with 2 * index + 1
node.right = Call recursively to create tree with 2 * index + 2
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