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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count the number of nodes at given level in a tree using BFS.
- Count nodes with two children at level L in a Binary Tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Count the nodes in the given Tree whose weight is a Perfect Number
- Count the nodes in the given tree whose weight is a powerful number
- Count nodes in the given tree whose weight is a fibonacci number
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Count the nodes in the given tree whose weight is even
- Count the nodes in the given tree whose weight is prime
- Count the nodes of the given tree whose weight has X as a factor
- Count the nodes in the given tree whose weight is even parity
- Count the nodes in the given tree whose sum of digits of weight is odd
- Count the nodes in the given tree whose weight is a power of two
- Count the nodes of the tree whose weighted string contains a vowel
- Count the nodes of the given tree whose weighted string is a palindrome
- Count of leaf nodes of the tree whose weighted string is a palindrome
- Count of subtrees from an N-ary tree consisting of single colored nodes
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Find maximum count of duplicate nodes in a Binary Search 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 email@example.com. 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.