Open In App

Construct Binary Tree from given Parent Array representation

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
55 Likes
Like
Report

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.

Examples: 

Input: parent[] = {1, 5, 5, 2, 2, -1, 3}
Output: root of below tree
5
/ \
1 2
/ / \
0 3 4
/
6
Explanation:
Index of -1 is 5. So 5 is root.
5 is present at indexes 1 and 2. So 1 and 2 are
children of 5.
1 is present at index 0, so 0 is child of 1.
2 is present at indexes 3 and 4. So 3 and 4 are
children of 2.
3 is present at index 6, so 6 is child of 3.
Input: parent[] = {-1, 0, 0, 1, 1, 3, 5};
Output: root of below tree
0
/ \
1 2
/ \
3 4
/
5
/
6

Expected time complexity is O(n) where n is number of elements in given array.

We strongly recommend to minimize your browser and try this yourself first.

A Simple Solution to recursively construct by first searching the current root, then recurring for the found indexes (there can be at most two indexes) and making them left and right subtrees of root. This solution takes O(n2) as we have to linearly search for every node.

An Efficient Solution can solve the above problem in O(n) time. The idea is to use extra space. An array created[0..n-1] is used to keep track of created nodes. 
createTree(parent[], n) 

  1. Create an array of pointers say created[0..n-1]. The value of created[i] is NULL if node for index i is not created, else value is pointer to the created node.
  2. Do following for every index i of given array 
    createNode(parent, i, created)

createNode(parent[], i, created[]) 

  1. If created[i] is not NULL, then node is already created. So return.
  2. Create a new node with value 'i'.
  3. If parent[i] is -1 (i is root), make created node as root and return.
  4. Check if parent of 'i' is created (We can check this by checking if created[parent[i]] is NULL or not.
  5. If parent is not created, recur for parent and create the parent first.
  6. Let the pointer to parent be p. If p->left is NULL, then make the new node as left child. Else make the new node as right child of parent.

Following is the implementation of above idea. 

C++
Java Python C# JavaScript

Output
Inorder traversal of constructed tree 
6 5 3 1 4 0 2 

Time Complexity: O(n^2)

In this approach, we iterate over the parent array, and in the worst case, we need to create a node for every element in the array. And in the createNode() function, the main time complexity is due to the recursive calls that depend on the height of the tree, which can be n. So, the overall time complexity of the algorithm is O(n^2).

Space Complexity: O(n)

The space complexity of the algorithm is linear, i.e., O(n), since we need to create a node for every element of the parent array.

Another Efficient Solution:

The idea is to first create all n new tree nodes, each having values from 0 to n - 1, where n is the size of parent array, and store them in any data structure like map, array etc to keep track of which node is created for which value. Then traverse the given parent array and build the tree by setting the parent-child relationship.

Algorithm

  • Start with creating an array to store the reference of all newly created nodes corresponding to node value.
  • Create n new tree nodes, each having a value from 0 to n-1, and store them in the reference array.
  • Traverse the parent array and build the tree:
    a. If the parent is -1, set the root to the current node having the value i which is stored in the reference array at index i.
    b. If the parent's left child is NULL, then map the left child to its parent.
    c. Else, map the right child to its parent.
  • Return the root of the newly constructed tree.
  • End.

Following is the implementation of the above idea.

C++
Java Python C# JavaScript

Output
Inorder Traversal of constructed tree
6 5 3 1 4 0 2 

Time Complexity: O(n), where n is the size of parent array
Auxiliary Space: O(n)

Method 3(using dictionary/hashmap)

Another method is to use a hash table to keep track of the nodes and their indices.

Algorithm

Create an empty hash table.
Traverse the parent array from left to right.
For each node in the parent array, get its parent index.
If the parent index is -1, create a new node and set it as the root.
Otherwise, check if the parent index is present in the hash table.
If it is, create a new node and add it as a child of the parent node.
Otherwise, create the parent node and add it to the hash table with its index as the key.
Repeat steps 3 to 7 for all the nodes in the parent array.
C++
Java Python C# JavaScript

Output
Inorder traversal of constructed tree:
6 5 3 1 4 0 2 

Time complexity:  O(n), since we need to visit each node of the tree exactly once. 
Space complexity: O(h), where h is the height of the tree

Similar Problem: Find Height of Binary Tree represented by Parent array


Construct Binary Tree from Parent Array | DSA Problem
Article Tags :
Practice Tags :

Similar Reads