> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/trees/construct-binary-tree-from-parent-array.md).

# Construct Binary Tree from Parent Array

Given an array of size **N** 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 Binary Tree from this array representation.

**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. The first line of each test case contains an integer **N** denoting the size of the tree array. The 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.

**User Task:**\
The task is to complete the function **createTree()** which takes 2 arguments(tree array and N) and returns the root node of the tree constructed.

**Expected Time Complexity:** O(N).\
**Expected Auxiliary Space:** O(N).

**Constraints:**\
1 <= T <= 1000\
1 <= N <= 103

**Example:**\
**Input:**\
2\
7\
-1 0 0 1 1 3 5\
5\
-1 0 0 2 2   \
**Output:**\
0 1 2 3 4 5 6\
0 1 2 3 4

**Explanation:**\
**Testcase 1:** For the array parent\[] = {-1, 0, 0, 1, 1, 3, 5}; the tree generated will have a sturcture like        &#x20;

&#x20;         0\
&#x20;      /   \\\
&#x20;     1     2\
&#x20;    / \\\
&#x20;   3   4\
&#x20;  /\
&#x20; 5\
/\
6\
**Testcase 2:** For the array parent\[] = {-1 0 0 2 2}; the tree generated will have a sturcture like\
&#x20;                 0\
&#x20;              /      \\\
&#x20;            1        2\
&#x20;         /      \\\
&#x20;       3        4

```java
class Solution {
    private static Node construct(int[] arr) {
        HashMap<Integer, Node> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++)
            map.put(i, new Node(i));
        int ans;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == -1)
                ans = i;
            else {
                Node parent = map.get(arr[i]);
                Node child = map.get(i);
                if (parent.left == null)
                    parent.left = child;
                else
                    parent.right = child;
            }
        }
        return map.get(ans);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/trees/construct-binary-tree-from-parent-array.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
