Given an integer l and a tree represented as an undirected graph rooted at vertex 0. The task is to print the number of nodes present at level l.
Input: l = 2
We have already discussed the BFS approach, in this post we will solve it using DFS.
Approach: The idea is to traverse the graph in a DFS manner. Take two variables, count and curr_level. Whenever the curr_level = l increment the value of the count.
Below is the implementation of the above approach:
- Count the number of nodes at given level in a tree using BFS.
- Level with maximum number of nodes using DFS in a N-ary tree
- Count the number of non-reachable nodes
- Print Leaf Nodes at a given Level
- Count nodes within K-distance from all nodes in a set
- Find if given vertical level of binary tree is sorted or not
- Check if a binary tree is sorted level-wise or not
- Find the maximum node at a given level in a binary tree
- Print levels with odd number of nodes and even number of nodes
- Subtree of all nodes in a tree using DFS
- Sum of all the Boundary Nodes of a Binary Tree
- Convert a tree to forest of even nodes
- Product of all nodes in a Binary Tree
- Check if two nodes are on same path in a tree
- Print all internal nodes of a 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.