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
- Preorder Traversal of N-ary Tree Without Recursion
- Find n-th node of inorder traversal
- Iterative diagonal traversal of binary tree
- Delete leaf nodes with value as x
- Binary tree to string with brackets
- Diameter of a Binary Tree in O(n) [A new method]
- Diameter of a tree using DFS
- Longest path in an undirected tree
- Diameter of an N-ary tree
- Depth of an N-Ary tree
- Number of ways to traverse an N-ary tree
- Lowest Common Ancestor in a Binary Tree | Set 1
- Applications of Minimum Spanning Tree Problem
- Diameter of a Binary Tree
- If you are given two traversal sequences, can you construct the binary tree?
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.