- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
In this problem, we are given a binary tree and we have to print all leaf nodes of the binary tree from left to right the iterative approach.
Let’s take an example to understand the problem
Input −
Output − 1 4 7
To solve this problem using the iterative approach, we will use a Depth-first search(DFS). To Traverse tree, we will start from root node and check if it is a leaf node if it is then print the node else find its child trees and traverse the child subtrees to find all leaf nodes.
Example
The below code will implement our solution −
#include <iostream> using namespace std; struct Node { int data; struct Node *left, *right; }; Node* insertNode(int data) { Node *temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } void printLTRLeafNodes(Node *root){ if (!root) return; if (!root->left && !root->right) { cout<<root->data<<"\t"; return; } if (root->left) printLTRLeafNodes(root->left); if (root->right) printLTRLeafNodes(root->right); } int main(){ Node *root = insertNode(21); root->left = insertNode(5); root->right = insertNode(36); root->left->left = insertNode(2); root->right->left = insertNode(13); root->right->right = insertNode(4); root->right->left->left = insertNode(76); root->right->left->right = insertNode(9); root->right->right->left = insertNode(17); root->right->right->right = insertNode(2); cout<<"Leaf Nodes of the tree from left to rigth are :\n"; printLTRLeafNodes(root); return 0; }
Output
Leaf Nodes of the tree from left to right are − 2 76 9 17 2
- Related Articles
- Print all leaf nodes of a binary tree from right to left in C++
- Print leaf nodes in binary tree from left to right using one stack in C++
- Product of all leaf nodes of binary tree in C++
- Print all leaf nodes of an n-ary tree using DFS in C++
- Print all internal nodes of a Binary tree in C++
- Program to print path from root to all nodes in a Complete Binary Tree using C++
- Find the sum of left leaf nodes of a given Binary Tree in C++
- Print all full nodes in a Binary Tree in C++
- Print all odd nodes of Binary Search Tree in C++
- Print all even nodes of Binary Search Tree in C++
- Print Levels of all nodes in a Binary Tree in C++ Programming.
- C++ Pairwise Swap Leaf Nodes in a Binary Tree
- Program to print the longest leaf to leaf path in a Binary tree using C++
- Count Non-Leaf nodes in a Binary Tree in C++
- Print the nodes of binary tree as they become the leaf node in C++ Programming.

Advertisements