VOOZH about

URL: https://www.geeksforgeeks.org/dsa/height-generic-tree-parent-array/

⇱ Height of a generic tree from parent array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Height of a generic tree from parent array

Last Updated : 1 Nov, 2024

Given a tree of size n as array parent[0..n-1] where every index i in the parent[] represents a node and the value at i represents the immediate parent of that node. For root, the node value will be -1. Find the height of the generic tree given the parent links.

Examples:

Input: parent[] = [-1, 0, 0, 0, 3, 1, 1, 2]
Output : 2

👁 height-of-a-generic-tree-from-parent-array-2


Input: parent[] = [-1, 0, 1, 2, 3]
Output : 4

👁 height-of-a-generic-tree-from-parent-array

Here, a generic tree is sometimes also called an N-ary tree or N-way tree where N denotes the maximum number of child a node can have. In this problem, the array represents n number of nodes in the tree.

The naive approach is to traverse up the tree from the node till the root node is reached with node value -1. While Traversing for each node stores maximum path length. The Time Complexity of this solution is O(n^2).

[Expected Approach - 1] Using BFS - O(n) Time and O(n) Space

Build graph for N-ary Tree in O(n) time and apply BFS on the stored graph in O(n) time and while doing BFS store maximum reached level. This solution does two iterations to find the height of N-ary tree.

Below is the implementation of the above approach: 


Output
2

[Expected Approach - 2] Without using map - O(n) Time and O(n) Space

We can find the height of the N-ary Tree in only one iteration. We visit nodes from 0 to n-1 iteratively and mark the unvisited ancestors recursively if they are not visited before till we reach a node which is visited, or we reach the root node. If we reach the visited node while traversing up the tree using parent links, then we use its height and will not go further in recursion.

Explanation For Example 1:

👁 height-of-a-generic-tree-from-parent-array-2


  • For node 0: Check for Root node is true, 
    Return 0 as height, Mark node 0 as visited 
  • For node 1: Recur for an immediate ancestor, i.e 0, which is already visited 
    So, Use its height and return height(node 0) +1 
    Mark node 1 as visited 
  • For node 2: Recur for an immediate ancestor, i.e 0, which is already visited 
    So, Use its height and return height(node 0) +1 
    Mark node 2 as visited 
  • For node 3: Recur for an immediate ancestor, i.e 0, which is already visited 
    So, Use its height and return height(node 0) +1 
    Mark node 3 as visited 
  • For node 4: Recur for an immediate ancestor, i.e 3, which is already visited 
    So, Use its height and return height(node 3) +1 
    Mark node 3 as visited 
  • For node 5: Recur for an immediate ancestor, i.e 1, which is already visited 
    So, Use its height and return height(node 1) +1 
    Mark node 5 as visited 
  • For node 6: Recur for an immediate ancestor, i.e 1, which is already visited 
    So, Use its height and return height(node 1) +1 
    Mark node 6 as visited 
  • For node 7: Recur for an immediate ancestor, i.e 2, which is already visited 
    So, Use its height and return height(node 2) +1 
  • Mark node 7 as visited
    Hence, we processed each node in the N-ary tree only once. 

Below is the implementation of the above approach: 


Output
2
Comment
Article Tags: