Given a Binary tree, print all the leaf nodes of a Binary tree at a given level L.
Input: 1 / \ 2 3 / / \ 4 5 6 level = 3 Output: 4 5 6 Input: 7 / \ 2 3 / \ \ 4 9 10 / 6 level = 3 Output: 4 9
Approach: 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 is leaf node or not. If it is a leaf node then print it.
Below is the implementation of the above approach:
Time Complexity:O(N) where N is number of nodes in a binary tree
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.
- Print the longest leaf to leaf path in a Binary tree
- Sum of leaf nodes at minimum level
- Difference between sums of odd level and even level nodes of a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print nodes between two given level numbers of a binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print all nodes that are at distance k from a leaf node
- Print all leaf nodes of a Binary Tree from left to right
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print leaf nodes in binary tree from left to right using one stack
- Print all leaf nodes of a binary tree from right to left
- Print all leaf nodes of an n-ary tree using DFS
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.