Given parent array representation of the tree, find the height of the tree. In this parent array representation, a node would be constructed with values taken from indices of this array. For element parent[i], a node would be constructed with value i. Its parent node would be located in parent array at index parent[i]. If parent[i] is -1, then we make that node the root of the tree. For example, for the parent array {1,5,5,2,2,-1,3} parent[1] = 5. Therefore in the tree, there would be a node with value 1. This node's parent would be located in parent array at index 5. At index 5, we have an element -1. Because parent[5] is -1, we create a node with value 5 and make that node as the root.

Let's construct complete tree using this parent array {1,5,5,2,2,-1,3}.

parent[5] = -1: construct a node with value 5. Make this a node as the root.

parent[1] = parent[2] = 5: construct two nodes with values 1 and 2, and make node 5 as their parent.

parent[0] = 1: construct node with value 0 and make node 1 as its parent.

parent[3] = parent[4] = 2: make node 2 as the parent node for newly created nodes with values 3 and 4.

Finally, parent[6] = 3: make a new node with value 6 and make node 3 as its parent.

The complete constructed tree would look like following:

And output of the program should be the height of this tree - 4.

For parent array {-1,0,0,1,1,2,2} constructed binary tree would be :

And output of the program should be the height of this tree - 3.