Make Binary Tree from Link List
PROBLEM :
Given Linked List Representation of Complete Binary Tree, construct the Binary tree.Your task is to complete the function convert which takes two arguments the first being the head denoting the head of the linked list and the second argument is root denoting the root of the tree to be constructed.
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.
Input:
The first line of input is the no of test cases T. Then T test cases follow . Each test case contains 2 lines. The first line contains an integer N denoting the no of nodes of the linked list . Then in the next line are N space separated Ki values of the linked list .
Output:
Output of each test case will be the level order traversal of the the constructed binary tree. You do not have to print it .
Constraints:
1<=T<=100
1<=N<=100
1<=Ki <=1000
Example(To be used only for expected output ) :
Input
1
5
1 2 3 4 5
Output
1 2 3 4 5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of Link list node is as follows
struct node
{
int data;
struct node* next;
};
The structure of TreeNode is as follows
struct TreeNode
{
int data;
TreeNode *left;
TreeNode *right;
};
*/
/*You are required to complete this method*/
struct TreeNode* create_Tnode(int ) ;
void convert(node *head,TreeNode * &root)
{
if(head==NULL)
return ;
queue<struct TreeNode*> q ;
root=create_Tnode(head->data) ;
q.push(root) ;
head=head->next ;
while(head)
{
struct TreeNode *parent,*Lnode,*Rnode ;
Lnode=NULL ;
Rnode=NULL ;
parent=q.front() ;
q.pop() ;
Lnode=create_Tnode(head->data) ;
q.push(Lnode) ;
head=head->next ;
if(head)
{
Rnode=create_Tnode(head->data) ;
q.push(Rnode) ;
head=head->next ;
}
if(Lnode)
parent->left=Lnode ;
if(Rnode)
parent->right=Rnode ;
}
}
struct TreeNode* create_Tnode(int data)
{
struct TreeNode *temp ;
temp=(struct TreeNode*)malloc(sizeof(struct TreeNode)) ;
temp->data=data ;
temp->left=NULL ;
temp->right=NULL ;
return temp ;
}
---------------------------------------------------------------------------------
Given Linked List Representation of Complete Binary Tree, construct the Binary tree.Your task is to complete the function convert which takes two arguments the first being the head denoting the head of the linked list and the second argument is root denoting the root of the tree to be constructed.
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.
Input:
The first line of input is the no of test cases T. Then T test cases follow . Each test case contains 2 lines. The first line contains an integer N denoting the no of nodes of the linked list . Then in the next line are N space separated Ki values of the linked list .
Output:
Output of each test case will be the level order traversal of the the constructed binary tree. You do not have to print it .
Constraints:
1<=T<=100
1<=N<=100
1<=Ki <=1000
Example(To be used only for expected output ) :
Input
1
5
1 2 3 4 5
Output
1 2 3 4 5
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of Link list node is as follows
struct node
{
int data;
struct node* next;
};
The structure of TreeNode is as follows
struct TreeNode
{
int data;
TreeNode *left;
TreeNode *right;
};
*/
/*You are required to complete this method*/
struct TreeNode* create_Tnode(int ) ;
void convert(node *head,TreeNode * &root)
{
if(head==NULL)
return ;
queue<struct TreeNode*> q ;
root=create_Tnode(head->data) ;
q.push(root) ;
head=head->next ;
while(head)
{
struct TreeNode *parent,*Lnode,*Rnode ;
Lnode=NULL ;
Rnode=NULL ;
parent=q.front() ;
q.pop() ;
Lnode=create_Tnode(head->data) ;
q.push(Lnode) ;
head=head->next ;
if(head)
{
Rnode=create_Tnode(head->data) ;
q.push(Rnode) ;
head=head->next ;
}
if(Lnode)
parent->left=Lnode ;
if(Rnode)
parent->right=Rnode ;
}
}
struct TreeNode* create_Tnode(int data)
{
struct TreeNode *temp ;
temp=(struct TreeNode*)malloc(sizeof(struct TreeNode)) ;
temp->data=data ;
temp->left=NULL ;
temp->right=NULL ;
return temp ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment