Construct Binary Tree from Parent Array
Table of contents
No headings in the article.
Given an array of size N that can be used to represents a tree. The 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 Binary Tree from this array representation.
Note: If two elements have the same parent, the one that appears first in the array will be the left child and the other is the right child.
Example 1:
Input:
N = 7
parent[] = {-1,0,0,1,1,3,5}
Output: 0 1 2 3 4 5 6
Explanation: the tree generated
will have a structure like
0
/ \
1 2
/ \
3 4
/
5
/
6
Example 2:
Input:
N = 3
parent[] = {2, 0, -1}
Output: 2 0 1
Explanation: the tree generated will
have a sturcture like
2
/
0
/
1
Your Task:
You don’t need to read input or print anything. The task is to complete the function createTree() which takes 2 arguments parent[] and N and returns the root node of the constructed tree.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 103
class Solution{
Node root;
public Node createTree(int[] parent,int N){
Map<Integer, Node> nodeMap = new HashMap<>();
for (int i = 0; i < N; i++) {
nodeMap.put(i, new Node(i));
}
Node rootNode = null;
for (int i = 0; i < N; i++) {
if (parent[i] == -1) {
rootNode = nodeMap.get(i);
} else {
Node parentNode = nodeMap.get(parent[i]);
Node currentNode = nodeMap.get(i);
if (parentNode.left == null) {
parentNode.left = currentNode;
} else {
parentNode.right = currentNode;
}
}
}
return rootNode;
}
}
- We create a
Node
class representing a node in the binary tree. It has an integer data value and left and right child nodes.
class Node {
int data;
Node left, right;
Node(int item) {
data = item;
left = right = null;
}
}
- Then, we create a
BinaryTree
class with a methodcreateTree
that takes in an array ofparent
and the sizeN
.
class BinaryTree {
Node root;
Node createTree(int parent[], int N) {
// ... (code explained below)
}
}
- Inside the
createTree
method, we create aHashMap
to store nodes using their indices as keys.
Map<Integer, Node> nodeMap = new HashMap<>();
for (int i = 0; i < N; i++) {
nodeMap.put(i, new Node(i));
}
- We initialize a variable
rootNode
to keep track of the root node. Then, we iterate through the parent array and link the nodes accordingly.
Node rootNode = null;
for (int i = 0; i < N; i++) {
if (parent[i] == -1) {
rootNode = nodeMap.get(i);
} else {
Node parentNode = nodeMap.get(parent[i]);
Node currentNode = nodeMap.get(i);
if (parentNode.left == null) {
parentNode.left = currentNode;
} else {
parentNode.right = currentNode;
}
}
}
- Finally, we return the
rootNode
which represents the root of the constructed binary tree.
This implementation constructs the binary tree as described in the problem statement using the array representation. The nodes are created and linked based on the parent-child relationship specified in the parent
array.
Time Complexity: O(N)
- The algorithm iterates through the given array of size N to create nodes and link them to their respective parents. Thus, the time complexity is directly proportional to the size of the input array, which is O(N).
Space Complexity: O(N)
- The algorithm uses additional space to store the nodes in a HashMap, resulting in a space complexity of O(N) because the space required is directly proportional to the size of the input array.
Therefore, both the time and space complexities are linear with respect to the size of the input array.