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.
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.
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 firstname.lastname@example.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.
Improved By : vt_m
- Insertion in a Binary Tree
- Find root of the tree where children id sum for every node is given
- Write a Program to Find the Maximum Depth or Height of a Tree
- Find Height of Binary Tree represented by Parent array
- Mirror of n-ary Tree
- HashSet vs TreeSet in Java
- Preorder from Inorder and Postorder traversals
- Count pairs in a binary tree whose sum is equal to a given value x
- Find the maximum node at a given level in a binary tree
- Print nodes in top view of Binary Tree | Set 2