Diameter of n-ary tree using BFS
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.
- Diameter of an N-ary tree
- Diameter of a Binary Tree in O(n)
- Diameter of a Binary Tree
- 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
Time complexity: O(n^2)
Auxiliary Space: O(10001)
My Personal Notes arrow_drop_up
Please Login to comment...