![]() |
VOOZH | about |
Given a tree with v vertices, find the level of each node in a tree from the source node.
Examples:
Input :
👁 Level of Each node in a Tree from source node using BFS
Output : Node Level 0 0 1 1 2 1 3 2 4 2 5 2 6 2 7 3 Explanation :
👁 Level of Each node in a Tree from source node using BFS
Input:
👁 Level of Each node in a Tree from source node using BFS
Output : Node Level 0 0 1 1 2 1 3 2 4 2 Explanation:
👁 Level of Each node in a Tree from source node using BFS
Approach:
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.
Algorithm:
Implementation:
Nodes Level 0 --> 0 1 --> 1 2 --> 1 3 --> 2 4 --> 2 5 --> 2 6 --> 2 7 --> 3
Complexity Analysis: