N-ary tree refers to the rooted tree in which each node having atmost k child nodes. The diameter of n-ary tree is the longest path between two leaf nodes.
Various approaches have already been discussed to compute diameter of tree.
1. Diameter of an N-ary tree
2. Diameter of a Binary Tree in O(n)
3. Diameter of a Binary Tree
4. Diameter of a tree using DFS
This article discuss another approach for computing diameter tree of n-ary tree using bfs.
Step 1: Run bfs to find the farthest node from rooted tree let say A
Step 2: Then run bfs from A to find farthest node from A let B
Step 3: Distance between node A and B is the diameter of given tree
Diameter of n-ary tree is 3
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 tree using DFS
- Level of Each node in a Tree from source node (using BFS)
- Count the number of nodes at given level in a tree using BFS.
- Diameter of a Binary Tree
- Diameter of an N-ary tree
- Diameter of a Binary Tree in O(n) [A new method]
- 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
- BFS vs DFS for Binary Tree
- Check if the given permutation is a valid BFS of a given Tree
- Print all paths from a given source to a destination using BFS
- Traversing directory in Java using BFS
- BFS using STL for competitive coding
- Water Jug problem using BFS
- BFS using vectors & queue as per the algorithm of CLRS
- Detect cycle in an undirected graph using BFS
- Detect Cycle in a Directed Graph using BFS
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.