Hieght From Parent Array

0

0 votes
Problem

Given a parent array arr[] of a binary tree of N nodes. Element at index i in the array arr[] represents the parent of ith node, i.e, arr[i] = parent(i). Find the height of this binary tree.
Note: There will be a node in the array arr[], where arr[i] = -1, which means this node is the root of a binary tree.

Input: first line contains the size of array and second line contains space-separated elements of array

Example 1:

Input:
7
-1 0 0 1 1 3 5
Output: 5
Explanation: Tree formed is:
                    0
                   / \
                  1   2
                 / \
                3   4
               /
              5
             /
            6      Height of the tree= 5

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?