Given each node's parent in a binary tree with n up to 20, print the height (distance from the root) of every node.
Easy3TreeDFSInterviewNo attempts yetTime limit2sMemory limit512 MBA binary tree is a structure made of nodes, and each node has at most two children. One child is the left child and the other is the right child. If node B is a child of node A, then A is the parent of B. Exactly one node in a binary tree has no parent, and that node is the root of the tree. The height of a node N is the number of edges on the path from the root to N. The root has height 0.
Compute the height of every node in the tree. The nodes are identified by the integers from 1 to n, where n is the number of nodes.
Look at the following tree.

The root is node 1. The left child of 1 is 2 and the right child of 1 is 3. Nodes 4, 5, 6 and 7 have no children. The heights are:
The next tree is a bit different.

Node 1 is still the root and has 2 and 3 as its left and right children, but 3 has only a right child, and node 4 has only a left child, node 5. The heights are:
The first line contains the number of nodes n. (1≤n≤20)
Each of the following n lines contains one integer, the parent of a node. That is, the second line of the input contains the parent of node 1, the third line the parent of node 2, and so on. The root is marked with -1. Node 1 is not always the root.
Print n lines. The first line holds the height of node 1, the second line holds the height of node 2, and so on.