Given a binary tree, the task is to print all leaf nodes of the given binary tree from left to right. That is, the nodes should be printed in the order they appear from left to right in the given tree.
Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : 4 5 6 7 Input : 4 / \ 5 9 / \ / \ 8 3 7 2 / / \ 12 6 1 Output : 12 3 7 6 1
We have already discussed the iterative approach using two stacks.
Approach:The idea is to perform iterative postorder traversal using one stack and print the leaf nodes.
Below is the implementation of the above approach:
4 5 6 7
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 All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print all leaf nodes of a binary tree from right to left
- Print left and right leaf nodes separately in Binary Tree
- Print all leaf nodes of a Binary Tree from left to right
- Print the longest leaf to leaf path in a Binary tree
- Convert left-right representation of a binary tree to down-right
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Create Balanced Binary Tree using its Leaf Nodes without using extra space
- Deepest left leaf node in a binary tree
- Deepest left leaf node in a binary tree | iterative approach
- Print all leaf nodes of an n-ary tree using DFS
- Deepest right leaf node in a binary tree | Iterative approach
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Program to count leaf nodes in a binary tree
- Find height of a special binary tree whose leaf nodes are connected
- Iterative program to count leaf nodes in 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.