Given Linked List Representation of Complete Binary Tree, construct the Binary tree. A complete binary tree can be represented in an array in the following approach.
If the root node is stored at index i, its left, and right children are stored at indices 2*i+1, and 2*i+2 respectively.
Suppose a tree is represented by a linked list in the same way, how do we convert this into a normally linked representation of a binary tree where every node has data, left and right pointers? In the linked list representation, we cannot directly access the children of the current node unless we traverse the list.

We are mainly given level order traversal in sequential access form. We know head of linked list is always is root of the tree. We take the first node as root and we also know that the next two nodes are left and right children of root. So we know partial Binary Tree. The idea is to do Level order traversal of the partially built Binary Tree using queue and traverse the linked list at the same time. At every step, we take the parent node from queue, make next two nodes of linked list as children of the parent node, and enqueue the next two nodes to queue.
1. Create an empty queue.
2. Make the first node of the list as root, and enqueue it to the queue.
3. Until we reach the end of the list, do the following.
………a. Dequeue one node from the queue. This is the current parent.
………b. Traverse two nodes in the list, add them as children of the current parent.
………c. Enqueue the two nodes into the queue.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
#include <queue>
using namespace std;
struct ListNode
{
int data;
ListNode* next;
};
struct BinaryTreeNode
{
int data;
BinaryTreeNode *left, *right;
};
void push( struct ListNode** head_ref, int new_data)
{
struct ListNode* new_node = new ListNode;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
BinaryTreeNode* newBinaryTreeNode( int data)
{
BinaryTreeNode *temp = new BinaryTreeNode;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void convertList2Binary(ListNode *head, BinaryTreeNode* &root)
{
queue<BinaryTreeNode *> q;
if (head == NULL)
{
root = NULL;
return ;
}
root = newBinaryTreeNode(head->data);
q.push(root);
head = head->next;
while (head)
{
BinaryTreeNode* parent = q.front();
q.pop();
BinaryTreeNode *leftChild = NULL, *rightChild = NULL;
leftChild = newBinaryTreeNode(head->data);
q.push(leftChild);
head = head->next;
if (head)
{
rightChild = newBinaryTreeNode(head->data);
q.push(rightChild);
head = head->next;
}
parent->left = leftChild;
parent->right = rightChild;
}
}
void inorderTraversal(BinaryTreeNode* root)
{
if (root)
{
inorderTraversal( root->left );
cout << root->data << " " ;
inorderTraversal( root->right );
}
}
int main()
{
struct ListNode* head = NULL;
push(&head, 36);
push(&head, 30);
push(&head, 25);
push(&head, 15);
push(&head, 12);
push(&head, 10);
BinaryTreeNode *root;
convertList2Binary(head, root);
cout << "Inorder Traversal of the constructed Binary Tree is: \n" ;
inorderTraversal(root);
return 0;
}
|
Java
import java.util.*;
class ListNode
{
int data;
ListNode next;
ListNode( int d)
{
data = d;
next = null ;
}
}
class BinaryTreeNode
{
int data;
BinaryTreeNode left, right = null ;
BinaryTreeNode( int data)
{
this .data = data;
left = right = null ;
}
}
class BinaryTree
{
ListNode head;
BinaryTreeNode root;
void push( int new_data)
{
ListNode new_node = new ListNode(new_data);
new_node.next = head;
head = new_node;
}
BinaryTreeNode convertList2Binary(BinaryTreeNode node)
{
Queue<BinaryTreeNode> q =
new LinkedList<BinaryTreeNode>();
if (head == null )
{
node = null ;
return null ;
}
node = new BinaryTreeNode(head.data);
q.add(node);
head = head.next;
while (head != null )
{
BinaryTreeNode parent = q.peek();
BinaryTreeNode leftChild = null , rightChild = null ;
leftChild = new BinaryTreeNode(head.data);
q.add(leftChild);
head = head.next;
if (head != null )
{
rightChild = new BinaryTreeNode(head.data);
q.add(rightChild);
head = head.next;
}
parent.left = leftChild;
parent.right = rightChild;
q.poll();
}
return node;
}
void inorderTraversal(BinaryTreeNode node)
{
if (node != null )
{
inorderTraversal(node.left);
System.out.print(node.data + " " );
inorderTraversal(node.right);
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.push( 36 );
tree.push( 30 );
tree.push( 25 );
tree.push( 15 );
tree.push( 12 );
tree.push( 10 );
BinaryTreeNode node = tree.convertList2Binary(tree.root);
System.out.println( "Inorder Traversal of the" +
" constructed Binary Tree is:" );
tree.inorderTraversal(node);
}
}
|
Python3
class ListNode:
def __init__( self , data):
self .data = data
self . next = None
class BinaryTreeNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
class Conversion:
def __init__( self , data = None ):
self .head = None
self .root = None
def push( self , new_data):
new_node = ListNode(new_data)
new_node. next = self .head
self .head = new_node
def convertList2Binary( self ):
q = []
if self .head is None :
self .root = None
return
self .root = BinaryTreeNode( self .head.data)
q.append( self .root)
self .head = self .head. next
while ( self .head):
parent = q.pop( 0 )
leftChild = None
rightChild = None
leftChild = BinaryTreeNode( self .head.data)
q.append(leftChild)
self .head = self .head. next
if ( self .head):
rightChild = BinaryTreeNode( self .head.data)
q.append(rightChild)
self .head = self .head. next
parent.left = leftChild
parent.right = rightChild
def inorderTraversal( self , root):
if (root):
self .inorderTraversal(root.left)
print (root.data,end = " " )
self .inorderTraversal(root.right)
conv = Conversion()
conv.push( 36 )
conv.push( 30 )
conv.push( 25 )
conv.push( 15 )
conv.push( 12 )
conv.push( 10 )
conv.convertList2Binary()
print ( "Inorder Traversal of the constructed Binary Tree is:" )
conv.inorderTraversal(conv.root)
|
C#
using System;
using System.Collections.Generic;
public class ListNode
{
public int data;
public ListNode next;
public ListNode( int d)
{
data = d;
next = null ;
}
}
public class BinaryTreeNode
{
public int data;
public BinaryTreeNode left, right = null ;
public BinaryTreeNode( int data)
{
this .data = data;
left = right = null ;
}
}
public class BinaryTree
{
ListNode head;
BinaryTreeNode root;
void push( int new_data)
{
ListNode new_node = new ListNode(new_data);
new_node.next = head;
head = new_node;
}
BinaryTreeNode convertList2Binary(BinaryTreeNode node)
{
Queue<BinaryTreeNode> q =
new Queue<BinaryTreeNode>();
if (head == null )
{
node = null ;
return null ;
}
node = new BinaryTreeNode(head.data);
q.Enqueue(node);
head = head.next;
while (head != null )
{
BinaryTreeNode parent = q.Peek();
BinaryTreeNode pp = q.Dequeue();
BinaryTreeNode leftChild = null , rightChild = null ;
leftChild = new BinaryTreeNode(head.data);
q.Enqueue(leftChild);
head = head.next;
if (head != null )
{
rightChild = new BinaryTreeNode(head.data);
q.Enqueue(rightChild);
head = head.next;
}
parent.left = leftChild;
parent.right = rightChild;
}
return node;
}
void inorderTraversal(BinaryTreeNode node)
{
if (node != null )
{
inorderTraversal(node.left);
Console.Write(node.data + " " );
inorderTraversal(node.right);
}
}
public static void Main()
{
BinaryTree tree = new BinaryTree();
tree.push(36);
tree.push(30);
tree.push(25);
tree.push(15);
tree.push(12);
tree.push(10);
BinaryTreeNode node = tree.convertList2Binary(tree.root);
Console.WriteLine( "Inorder Traversal of the" +
" constructed Binary Tree is:" );
tree.inorderTraversal(node);
}
}
|
Javascript
<script>
class ListNode {
constructor(d) {
this .data = d;
this .next = null ;
}
}
class BinaryTreeNode {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
class BinaryTree {
constructor() {
this .head = null ;
this .root = null ;
}
push(new_data) {
var new_node = new ListNode(new_data);
new_node.next = this .head;
this .head = new_node;
}
convertList2Binary(node) {
var q = [];
if ( this .head == null ) {
node = null ;
return null ;
}
node = new BinaryTreeNode( this .head.data);
q.push(node);
this .head = this .head.next;
while ( this .head != null ) {
var parent = q.shift();
var leftChild = null ,
rightChild = null ;
leftChild = new BinaryTreeNode( this .head.data);
q.push(leftChild);
this .head = this .head.next;
if ( this .head != null ) {
rightChild = new BinaryTreeNode( this .head.data);
q.push(rightChild);
this .head = this .head.next;
}
parent.left = leftChild;
parent.right = rightChild;
}
return node;
}
inorderTraversal(node) {
if (node != null ) {
this .inorderTraversal(node.left);
document.write(node.data + " " );
this .inorderTraversal(node.right);
}
}
}
var tree = new BinaryTree();
tree.push(36);
tree.push(30);
tree.push(25);
tree.push(15);
tree.push(12);
tree.push(10);
var node = tree.convertList2Binary(tree.root);
document.write(
"Inorder Traversal of the" + " constructed Binary Tree is:<br>"
);
tree.inorderTraversal(node);
</script>
|
Output
Inorder Traversal of the constructed Binary Tree is:
25 12 30 10 36 15
Time Complexity: O(n), where n is the number of nodes.
Auxiliary Space: O(b), Here b is the maximum number of nodes at any level.
This article is compiled by Ravi Chandra Enaganti. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Feb, 2023
Like Article
Save Article