Given a parent array P, where P[i] indicates the parent of ith node in the tree(assume parent of root node id indicated with -1). Find the height of the tree.

Examples:

Input : array[] = [-1 0 1 6 6 0 0 2 7] Output : height = 5 Tree formed is: 0 / | \ 5 1 6 / | \ 2 4 3 / 7 / 8

1. Start at each node and keep going to its parent until we reach -1.

2. Also keep track of the maximum height among all nodes.

## C++

`// C++ program to find the height of the generic ` `// tree(n-ary tree) if parent array is given ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find the height of tree ` `int` `findHeight(` `int` `* parent, ` `int` `n) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// Traverse each node ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// traverse to parent until -1 ` ` ` `// is reached ` ` ` `int` `p = i, current = 1; ` ` ` `while` `(parent[p] != -1) { ` ` ` `current++; ` ` ` `p = parent[p]; ` ` ` `} ` ` ` ` ` `res = max(res, current); ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `parent[] = {-1, 0, 1, 6, 6, 0, 0, 2, 7}; ` ` ` `int` `n = ` `sizeof` `(parent)/` `sizeof` `(parent[0]); ` ` ` `int` `height = findHeight(parent, n); ` ` ` `cout << ` `"Height of the given tree is: "` ` ` `<< height << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program to find the height of ` `// the generic tree(n-ary tree) if ` `// parent array is given ` `import` `java.io.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// function to find the height of tree ` ` ` `static` `int` `findHeight(` `int` `[]parent, ` `int` `n) ` ` ` `{ ` ` ` `int` `res = ` `0` `; ` ` ` ` ` `// Traverse each node ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// traverse to parent until -1 ` ` ` `// is reached ` ` ` `int` `p = i, current = ` `1` `; ` ` ` `while` `(parent[p] != -` `1` `) ` ` ` `{ ` ` ` `current++; ` ` ` `p = parent[p]; ` ` ` `} ` ` ` ` ` `res = Math.max(res, current); ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `[]parent = {-` `1` `, ` `0` `, ` `1` `, ` `6` `, ` `6` `, ` `0` `, ` ` ` `0` `, ` `2` `, ` `7` `}; ` ` ` `int` `n = parent.length; ` ` ` ` ` `int` `height = findHeight(parent, n); ` ` ` ` ` `System.out.println(` `"Height of the "` ` ` `+ ` `"given tree is: "` `+ height); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the height of ` `// the generic tree(n-ary tree) if ` `// parent array is given ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// function to find the height of tree ` ` ` `static` `int` `findHeight(` `int` `[]parent, ` `int` `n) ` ` ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// Traverse each node ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// traverse to parent until -1 ` ` ` `// is reached ` ` ` `int` `p = i, current = 1; ` ` ` `while` `(parent[p] != -1) ` ` ` `{ ` ` ` `current++; ` ` ` `p = parent[p]; ` ` ` `} ` ` ` ` ` `res = Math.Max(res, current); ` ` ` `} ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `[]parent = {-1, 0, 1, 6, 6, 0, ` ` ` `0, 2, 7}; ` ` ` `int` `n = parent.Length; ` ` ` ` ` `int` `height = findHeight(parent, n); ` ` ` ` ` `Console.WriteLine (` `"Height of the "` ` ` `+ ` `"given tree is: "` `+ height); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

Output:

Height of the given tree is: 5

This article is contributed by **Prakriti Gupta**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Height of a generic tree from parent array
- Find Height of Binary Tree represented by Parent array
- Construct Binary Tree from given Parent Array representation
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Maximum parent children sum in Binary tree
- Find right sibling of a binary tree with parent pointers
- Lowest Common Ancestor in a Binary Tree | Set 2 (Using Parent Pointer)
- Ways to color a skewed tree such that parent and child have different colors
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Practice questions on Height balanced/AVL Tree
- Height of binary tree considering even level leaves only
- How to determine if a binary tree is height-balanced?
- Possible edges of a tree for given diameter, height and vertices
- Traversal of tree with k jumps allowed between nodes of same height
- Height of a complete binary tree (or Heap) with N nodes