Given a binary tree, we need to write a program 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.

For Example,

For the above binary tree, output will be as shown below:

4 6 7 9 10

The idea to do this is similar to DFS algorithm. Below is step by step algorithm to do this:

- Check if given node is null. If null, then return from the function.
- Check if it is a leaf node. If the node is a leaf node, then print its data.
- If in above step, node is not a leaf node then check if left and right childs of node exists. If yes then call function for left and right childs of the node recursively.

Below is C++ implementation of above approach.

`/* C++ program to print leaf nodes from left ` ` ` `to right */` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// A Binary Tree Node ` `struct` `Node ` `{ ` ` ` `int` `data; ` ` ` `struct` `Node *left, *right; ` `}; ` ` ` `// function to print leaf ` `// nodes from left to right ` `void` `printLeafNodes(Node *root) ` `{ ` ` ` `// if node is null, return ` ` ` `if` `(!root) ` ` ` `return` `; ` ` ` ` ` `// if node is leaf node, print its data ` ` ` `if` `(!root->left && !root->right) ` ` ` `{ ` ` ` `cout << root->data << ` `" "` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// if left child exists, check for leaf ` ` ` `// recursively ` ` ` `if` `(root->left) ` ` ` `printLeafNodes(root->left); ` ` ` ` ` `// if right child exists, check for leaf ` ` ` `// recursively ` ` ` `if` `(root->right) ` ` ` `printLeafNodes(root->right); ` `} ` ` ` `// Utility function to create a new tree node ` `Node* newNode(` `int` `data) ` `{ ` ` ` `Node *temp = ` `new` `Node; ` ` ` `temp->data = data; ` ` ` `temp->left = temp->right = NULL; ` ` ` `return` `temp; ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `// Let us create binary tree shown in ` ` ` `// above diagram ` ` ` `Node *root = newNode(1); ` ` ` `root->left = newNode(2); ` ` ` `root->right = newNode(3); ` ` ` `root->left->left = newNode(4); ` ` ` `root->right->left = newNode(5); ` ` ` `root->right->right = newNode(8); ` ` ` `root->right->left->left = newNode(6); ` ` ` `root->right->left->right = newNode(7); ` ` ` `root->right->right->left = newNode(9); ` ` ` `root->right->right->right = newNode(10); ` ` ` ` ` `// print leaf nodes of the given tree ` ` ` `printLeafNodes(root); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

4 6 7 9 10

**Time Complexity**: O( n ) , where n is number of nodes in the binary tree.

This article is contributed by **Harsh Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Print the nodes of binary tree as they become the leaf node
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print the longest leaf to leaf path in a Binary tree
- Deepest left leaf node in a binary tree
- Deepest left leaf node in a binary tree | iterative approach
- Sum of all leaf nodes of binary tree
- Count Non-Leaf nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Program to count leaf nodes in a binary tree
- Pairwise Swap leaf nodes in a binary tree
- Iterative program to count leaf nodes in a Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Find height of a special binary tree whose leaf nodes are connected
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Given a binary tree, print all root-to-leaf paths