# 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.
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

## C++

 `// C++ Program to find Diameter of n-ary tree ` `#include ` `using` `namespace` `std; ` ` `  `// Here 10000 is maximum number of nodes in ` `// given tree. ` `int` `diameter; ` ` `  `// The Function to do bfs traversal. ` `// It uses iterative approach to do bfs ` `// bfsUtil() ` `int` `bfs(``int` `init, vector<``int``> arr[], ``int` `n) ` `{ ` `    ``// Initializing queue ` `    ``queue<``int``> q; ` `    ``q.push(init); ` ` `  `    ``int` `visited[n + 1]; ` `    ``for` `(``int` `i = 0; i <= n; i++) { ` `        ``visited[i] = 0; ` `        ``diameter[i] = 0; ` `    ``} ` ` `  `    ``// Pushing each node in queue ` `    ``q.push(init); ` ` `  `    ``// Mark the traversed node visited ` `    ``visited[init] = 1; ` `    ``while` `(!q.empty()) { ` `        ``int` `u = q.front(); ` `        ``q.pop(); ` `        ``for` `(``int` `i = 0; i < arr[u].size(); i++) { ` `            ``if` `(visited[arr[u][i]] == 0) { ` `                ``visited[arr[u][i]] = 1; ` ` `  `                ``// Considering weight of edges equal to 1 ` `                ``diameter[arr[u][i]] += diameter[u] + 1; ` `                ``q.push(arr[u][i]); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// return index of max value in diameter ` `    ``return` `int``(max_element(diameter + 1, ` `                           ``diameter + n + 1) ` `               ``- diameter); ` `} ` ` `  `int` `findDiameter(vector<``int``> arr[], ``int` `n) ` `{ ` `    ``int` `init = bfs(1, arr, n); ` `    ``int` `val = bfs(init, arr, n); ` `    ``return` `diameter[val]; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Input number of nodes ` `    ``int` `n = 6; ` ` `  `    ``vector<``int``> arr[n + 1]; ` ` `  `    ``// Input nodes in adjacency list ` `    ``arr.push_back(2); ` `    ``arr.push_back(3); ` `    ``arr.push_back(6); ` `    ``arr.push_back(4); ` `    ``arr.push_back(1); ` `    ``arr.push_back(5); ` `    ``arr.push_back(1); ` `    ``arr.push_back(2); ` `    ``arr.push_back(2); ` `    ``arr.push_back(1); ` ` `  `    ``printf``(``"Diameter of n-ary tree is %d\n"``, ` `           ``findDiameter(arr, n)); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java Program to find Diameter of n-ary tree ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Here 10000 is maximum number of nodes in ` `// given tree. ` `static` `int` `diameter[] = ``new` `int``[``10001``]; ` ` `  `// The Function to do bfs traversal. ` `// It uses iterative approach to do bfs ` `// bfsUtil() ` `static` `int` `bfs(``int` `init,  ` `               ``Vector>arr, ``int` `n) ` `{ ` `    ``// Initializing queue ` `    ``Queue q = ``new` `LinkedList<>(); ` `    ``q.add(init); ` ` `  `    ``int` `visited[] = ``new` `int``[n + ``1``]; ` `    ``for` `(``int` `i = ``0``; i <= n; i++)  ` `    ``{ ` `        ``visited[i] = ``0``; ` `        ``diameter[i] = ``0``; ` `    ``} ` ` `  `    ``// Pushing each node in queue ` `    ``q.add(init); ` ` `  `    ``// Mark the traversed node visited ` `    ``visited[init] = ``1``; ` `    ``while` `(q.size() > ``0``)  ` `    ``{ ` `        ``int` `u = q.peek(); ` `        ``q.remove(); ` `        ``for` `(``int` `i = ``0``;  ` `                 ``i < arr.get(u).size(); i++)  ` `        ``{ ` `            ``if` `(visited[arr.get(u).get(i)] == ``0``) ` `            ``{ ` `                ``visited[arr.get(u).get(i)] = ``1``; ` ` `  `                ``// Considering weight of edges equal to 1 ` `                ``diameter[arr.get(u).get(i)] += diameter[u] + ``1``; ` `                ``q.add(arr.get(u).get(i)); ` `            ``} ` `        ``} ` `    ``} ` `    ``int` `in = ``0``; ` `    ``for``(``int` `i = ``0``; i <= n; i++) ` `    ``{ ` `        ``if``(diameter[i] > diameter[in]) ` `        ``in = i; ` `    ``} ` `     `  `    ``// return index of max value in diameter ` `    ``return` `in; ` `} ` ` `  `static` `int` `findDiameter(Vector> arr, ``int` `n) ` `{ ` `    ``int` `init = bfs(``1``, arr, n); ` `    ``int` `val = bfs(init, arr, n); ` `    ``return` `diameter[val]; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``// Input number of nodes ` `    ``int` `n = ``6``; ` ` `  `    ``Vector> arr = ``new`  `    ``Vector>(); ` `     `  `    ``for``(``int` `i = ``0``; i < n + ``1``; i++) ` `    ``{ ` `        ``arr.add(``new` `Vector()); ` `    ``} ` ` `  `    ``// Input nodes in adjacency list ` `    ``arr.get(``1``).add(``2``); ` `    ``arr.get(``1``).add(``3``); ` `    ``arr.get(``1``).add(``6``); ` `    ``arr.get(``2``).add(``4``); ` `    ``arr.get(``2``).add(``1``); ` `    ``arr.get(``2``).add(``5``); ` `    ``arr.get(``3``).add(``1``); ` `    ``arr.get(``4``).add(``2``); ` `    ``arr.get(``5``).add(``2``); ` `    ``arr.get(``6``).add(``1``); ` ` `  `    ``System.out.printf(``"Diameter of n-ary tree is %d\n"``, ` `                                 ``findDiameter(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## C#

 `// C# Program to find Diameter of n-ary tree ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` `     `  `// Here 10000 is maximum number of nodes  ` `// in given tree. ` `static` `int` `[]diameter = ``new` `int``; ` ` `  `// The Function to do bfs traversal. ` `// It uses iterative approach to do bfs ` `// bfsUtil() ` `static` `int` `bfs(``int` `init,  ` `               ``List>arr, ``int` `n) ` `{ ` `    ``// Initializing queue ` `    ``Queue<``int``> q = ``new` `Queue<``int``>(); ` `    ``q.Enqueue(init); ` ` `  `    ``int` `[]visited = ``new` `int``[n + 1]; ` `    ``for` `(``int` `i = 0; i <= n; i++)  ` `    ``{ ` `        ``visited[i] = 0; ` `        ``diameter[i] = 0; ` `    ``} ` ` `  `    ``// Pushing each node in queue ` `    ``q.Enqueue(init); ` ` `  `    ``// Mark the traversed node visited ` `    ``visited[init] = 1; ` `    ``while` `(q.Count > 0)  ` `    ``{ ` `        ``int` `u = q.Peek(); ` `        ``q.Dequeue(); ` `        ``for` `(``int` `i = 0;  ` `                 ``i < arr[u].Count; i++)  ` `        ``{ ` `            ``if` `(visited[arr[u][i]] == 0) ` `            ``{ ` `                ``visited[arr[u][i]] = 1; ` ` `  `                ``// Considering weight of edges equal to 1 ` `                ``diameter[arr[u][i]] += diameter[u] + 1; ` `                ``q.Enqueue(arr[u][i]); ` `            ``} ` `        ``} ` `    ``} ` `    ``int` `iN = 0; ` `    ``for``(``int` `i = 0; i <= n; i++) ` `    ``{ ` `        ``if``(diameter[i] > diameter[iN]) ` `        ``iN = i; ` `    ``} ` `     `  `    ``// return index of max value in diameter ` `    ``return` `iN; ` `} ` ` `  `static` `int` `findDiameter(List> arr, ``int` `n) ` `{ ` `    ``int` `init = bfs(1, arr, n); ` `    ``int` `val = bfs(init, arr, n); ` `    ``return` `diameter[val]; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` `    ``// Input number of nodes ` `    ``int` `n = 6; ` ` `  `    ``List> arr = ``new` `    ``List>(); ` `     `  `    ``for``(``int` `i = 0; i < n + 1; i++) ` `    ``{ ` `        ``arr.Add(``new` `List<``int``>()); ` `    ``} ` ` `  `    ``// Input nodes in adjacency list ` `    ``arr.Add(2); ` `    ``arr.Add(3); ` `    ``arr.Add(6); ` `    ``arr.Add(4); ` `    ``arr.Add(1); ` `    ``arr.Add(5); ` `    ``arr.Add(1); ` `    ``arr.Add(2); ` `    ``arr.Add(2); ` `    ``arr.Add(1); ` ` `  `    ``Console.Write(``"Diameter of n-ary tree is {0}\n"``, ` `                              ``findDiameter(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```Diameter of n-ary tree is 3
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Improved By : andrew1234, princiraj1992

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.