So I'm trying to do an online assignment (this is not for course credit, I'm taking on my own through coursera) and I'm pretty confused with what's happening.

We are given an 2 line input. The first line is the number of vertices in the tree. The second line lists all the nodes parent nodes with -1 indicating it is the root.

For example, given the following input:

5

4 -1 4 1 1

The first line says there are 5 nodes in total. The second line says node 0 is a child of node 4, node 1
is the root, node 2 is a child of node 4, node 3 is a child of node 1 and node 4 is a child of node 1.

Right now I am just attempting to read in the input and store it in a tree structure.

```
#include <iostream>
#include <vector>

using namespace std;

struct Node{
    int key;
    vector<Node*> children;
};

void display(Node* root){
    cout << "My Value is " << root->key << endl;
    cout << "My children are ";
    for (int i = 0; i < root->children.size(); i++){
        cout << root->children[i]->key << " ";
    }
    cout << endl;
    for (int i = 0; i < root->children.size(); i++){
        display(root->children[i]);         //call display on all children
    }
    cout << endl;
}

int main(){

    Node* root = NULL;
    int numOfNodes;
    cin >> numOfNodes;

    int input;
    vector<int> parents;
    for(int i = 0; i < numOfNodes; i++){
        cin >> input;
        parents.push_back(input);
    }

    for(int i = 0; i < parents.size(); i++){
        Node *newNode = new Node();
        newNode->key = i;
        if(parents[i] == -1){ //root node is indicated with a -1 in input
            root = newNode;
        }
        for (int j = 0; j < parents.size(); j++){
            if(parents[j] == i){
                Node *childNode = new Node();
                childNode->key = j;
                newNode->children.push_back(childNode);
                cout << "value " << newNode->key << " is now a parent of " << childNode->key << endl;
                //delete childNode;
            }
        }
    }
    display(root);

    return 0;
}

```

I added in the display function to see whether or not I was storing the tree correctly and this is where I'm lost. The print statements I added seem to be conflicting. In main I added a cout whenever I added a childNode however by the time my program gets to the display function is seems the childNodes are missing with the exeception of the children of the root node. Here is a sample of my output.

```
./treeheight
5
4 -1 4 1 1
value 1 is now a parent of 3
value 1 is now a parent of 4
value 4 is now a parent of 0
value 4 is now a parent of 2
My Value is 1
My children are 3 4 
My Value is 3
My children are 

My Value is 4
My children are 

```

It would seem that creation of the tree in main worked as I wanted it to but when I try to reference those same nodes in display something is going wrong that I do not understand. I would have thought the output in display would read 

```
My Value is 1
My children are 3 4 
My Value is 3
My children are 

My Value is 4
My children are 2 0

```

If anyone could shed some light on what's going on here I would greatly appreciate it.