Given a tree with v vertices, find the level of each node in a tree from the source node.
Input : Output : Node Level 0 0 1 1 2 1 3 2 4 2 5 2 6 2 7 3 Explanation : Input: Output : Node Level 0 0 1 1 2 1 3 2 4 2 Explanation:
BFS(Breadth-First Search) is a graph traversal technique where a node and its neighbours are visited first and then the neighbours of neighbours. In simple terms, it traverses level-wise from the source. First, it traverses level 1 nodes (direct neighbours of source node) and then level 2 nodes (neighbours of source node) and so on. The BFS can be used to determine the level of each node from a given source node.
- Create the tree, a queue to store the nodes and insert the root or starting node in the queue. Create an extra array level of size v (number of vertices) and create a visited array.
- Run a loop while size of queue is greater than 0.
- Mark the current node as visited.
- Pop one node from the queue and insert its childrens (if present) and update the size of the inserted node as level[child] = level[node] + 1.
- Print all the node and its level.
Nodes Level 0 --> 0 1 --> 1 2 --> 1 3 --> 2 4 --> 2 5 --> 2 6 --> 2 7 --> 3
- Time Complexity: O(n).
In BFS traversal every node is visited only once, so Time Complexity is O(n).
- Space Complexity: O(n).
The space is required to store the nodes in a queue.
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.
- Minimum cost path from source node to destination node via an intermediate node
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Minimum number of reversals to reach node 0 from every other node
- XOR of all the nodes in the sub-tree of the given node
- Check whether the cost of going from any node to any other node via all possible paths is same
- Right sibling of each node in a tree given as array of edges
- Find parent of each node in a tree for multiple queries
- Burn the binary tree starting from the target node
- Count the number of nodes at a given level in a tree using DFS
- Count the number of nodes at given level in a tree using BFS.
- Find the node whose sum with X has maximum set bits
- Find the node whose sum with X has minimum set bits
- Minimum value of distance of farthest node in a Graph
- Print the degree of every node from the given Prufer sequence
- Find the node whose absolute difference with X gives minimum value
- Find the node whose absolute difference with X gives maximum value
- Find all reachable nodes from every node present in a given set
- k'th heaviest adjacent node in a graph where each vertex has weight
- Print the node with the maximum degree in the prufer sequence
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.