Given a Binary tree, the task is to count the number of nodes with two children at a given level L.
Input: 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 L = 2 Output: 1 Input: 20 / \ 8 22 / \ / \ 5 3 4 25 / \ / \ \ 1 10 2 14 6 L = 3 Output: 2
Approach: Initialize a variable count = 0. Recursively traverse the tree in a level order manner. If the current level is same as the given level, then check whether the current node has two children. If it has two children then increment the variable 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 nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Print nodes between two given level numbers of a binary tree
- Count of Nodes whose both immediate children are its prime factors
- Connect Nodes at same Level (Level Order Traversal)
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Check for Children Sum Property in a Binary Tree
- Maximum parent children sum in Binary tree
- Iterative approach to check for children sum property in a Binary Tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Swap Nodes in Binary tree of every k'th level
- Print odd positioned nodes of odd levels in level order of the given 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.