The diameter of an N-ary tree is the longest path present between any two nodes of the tree. These two nodes must be two leaf nodes. The following examples have the longest path[diameter] shaded.
Prerequisite: Diameter of a binary tree.
The path can either start from one of the nodes and goes up to one of the LCAs of these nodes and again come down to the deepest node of some other subtree or can exist as a diameter of one of the child of the current node.
The solution will exist in any one of these:
I] Diameter of one of the children of the current node
II] Sum of Height of the highest two subtree + 1
Optimizations to above solution :
We can make a hash table to store heights of all nodes. If we precompute these heights, we don’t need to call depthOfTree() for every node.
A different optimized solution: Longest path in an undirected tree
Another Approach to get diameter using DFS in one traversal:
The diameter of a tree can be calculated as for every node
- The current node isn’t part of diameter (i.e Diameter lies on of one of the children of the current node).
- The current node is part of diameter (i.e Diameter passes through the current node).
Node: Adjacency List has been used to store the Tree.
Below is the implementation of the above approach:
Diameter of tree is : 4
This article is contributed by Shubham 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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Diameter of a Binary Tree
- Diameter of a tree using DFS
- Diameter of a Binary Tree in O(n) [A new method]
- Diameter of n-ary tree using BFS
- Possible edges of a tree for given diameter, height and vertices
- DP on Trees | Set-3 ( Diameter of N-ary Tree )
- Make a tree with n vertices , d diameter and at most vertex degree k
- Diameter of a Binary Indexed Tree with N nodes
- Finding the lexicographically smallest diameter in a binary tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Binary Tree into its Mirror Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if a binary tree is subtree of another binary tree | Set 1
- Convert a given tree to its Sum Tree
- Binary Tree to Binary Search Tree Conversion
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Check whether a binary tree is a full binary tree or not