Construct Binary Tree from Parent Array

PROBLEM :

Given an array that represents a Tree in such a way that array indexes are values in tree nodes and array values give the parent node of that particular index (or node). The value of the root node index would always be -1 as there is no parent for root. Construct the standard linked representation of given Binary Tree from this given representation.

The Binary tree  node structure has 3 fields, a data part which stores the data, a left pointer which points to the left element of the tree and a right pointer which points to the right node of the  tree.

There are multiple test cases. For each test case, this function will be called individually.

Input:
The first line of the input contains an integer 'T' denoting the number of test cases. Then 'T' test cases follow. Each test case consists of two lines. First line of each test case contains an integer N denoting the size of the tree array . Second line of each test case consists of 'N' space separated integers denoting the elements of the array .

Output:
The Output will be the sorted level order traversal of the tree you are only required to return the modified tree root.

Expected Time Complexity: O(N)

Constraints:
1<=T<=100
1<=N<=200

Example:
Input:
1
7
-1 0 0 1 1 3 5    

Output:
0 1 2 3 4 5 6       (Sorted Level Order  Traversal of the constructed tree)

Explanation:

For the array parent[] = {-1, 0, 0, 1, 1, 3, 5};
the tree generated will have a sturcture like
         0
       /   \
      1     2
     / \
    3   4
   /
  5
 /
6

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/* node structure  used in the program
 struct Node
 {
     int data;
     Node* left, *right;
}; */

/*  Function which returns the  root of
    the constructed binary tree. */
   
void ConstructTree(int [],Node *[],int ,Node **) ;

Node *createTree(int parent[], int n)
{
    if(n==0)
        return NULL ;
       
    Node *created[n] ;
    for(int i=0;i<n;i++)
        created[i]=NULL ;
   
    Node *root=NULL ;  
    for(int i=0;i<n;i++)
        ConstructTree(parent,created,i,&root) ;
       
    return root ;
}
void ConstructTree(int parent[],Node *created[],int curr,Node **root)
{
    if(created[curr]!=NULL)
        return ;
       
    created[curr]=(Node*)malloc(sizeof(Node*)) ;
    created[curr]->data=curr ;
   
    if(parent[curr]==-1)
    {
        (*root)=created[curr] ;
        return ;
    }
   
    if(created[parent[curr]]==NULL)
        ConstructTree(parent,created,parent[curr],&(*root)) ;
       
    Node *temp=created[parent[curr]] ;
   
    if(!temp->left)
        temp->left=created[curr] ;
    else
        temp->right=created[curr] ;
}

--------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )