Related Articles
Print all leaf nodes of a Binary Tree from left to right
• Difficulty Level : Easy
• Last Updated : 17 Dec, 2020

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, the output will be as shown below:

`4 6 7 9 10`

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

1. Check if the given node is null. If null, then return from the function.
2. Check if it is a leaf node. If the node is a leaf node, then print its data.
3. If in the above step, the node is not a leaf node then check if the left and right children of node exist. If yes then call the function for left and right child of the node recursively.

Below is the implementation of the above approach.

## C++

 `/* C++ program to print leaf nodes from left``   ``to right */``#include ``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;``}`

## Java

 `// Java program to print leaf nodes``// from left to right``import` `java.util.*;``  ` `class` `GFG{``     ` `// A Binary Tree Node``static` `class` `Node``{``    ``public` `int` `data;``    ``public` `Node left, right;``};`` ` `// Function to print leaf``// nodes from left to right``static` `void` `printLeafNodes(Node root)``{``     ` `    ``// If node is null, return``    ``if` `(root == ``null``)``        ``return``;``     ` `    ``// If node is leaf node, print its data    ``    ``if` `(root.left == ``null` `&&``        ``root.right == ``null``)``    ``{``        ``System.out.print(root.data + ``" "``);``        ``return``;``    ``}``     ` `    ``// If left child exists, check for leaf``    ``// recursively``    ``if` `(root.left != ``null``)``        ``printLeafNodes(root.left);``         ` `    ``// If right child exists, check for leaf``    ``// recursively``    ``if` `(root.right != ``null``)``        ``printLeafNodes(root.right);``}`` ` `// Utility function to create a new tree node``static` `Node newNode(``int` `data)``{``    ``Node temp = ``new` `Node();``    ``temp.data = data;``    ``temp.left = ``null``;``    ``temp.right = ``null``;``    ``return` `temp;``}`` ` `// Driver code``public` `static` `void` `main(String []args)``{``     ` `    ``// 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);``}``}` `// This code is contributed by pratham76`

## Python3

 `# Python3 program to print``# leaf nodes from left to right` `# Binary tree node``class` `Node:``  ` `    ``def` `__init__(``self``, data):``        ``self``.data ``=` `data``        ``self``.left ``=` `None``        ``self``.right ``=` `None` `# Function to print leaf``# nodes from left to right``def` `printLeafNodes(root: Node) ``-``> ``None``:` `    ``# If node is null, return``    ``if` `(``not` `root):``        ``return` `    ``# If node is leaf node,``    ``# print its data``    ``if` `(``not` `root.left ``and``        ``not` `root.right):``        ``print``(root.data,``              ``end ``=` `" "``)``        ``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)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Let us create binary tree shown in``    ``# above diagram``    ``root ``=` `Node(``1``)``    ``root.left ``=` `Node(``2``)``    ``root.right ``=` `Node(``3``)``    ``root.left.left ``=` `Node(``4``)``    ``root.right.left ``=` `Node(``5``)``    ``root.right.right ``=` `Node(``8``)``    ``root.right.left.left ``=` `Node(``6``)``    ``root.right.left.right ``=` `Node(``7``)``    ``root.right.right.left ``=` `Node(``9``)``    ``root.right.right.right ``=` `Node(``10``)` `    ``# print leaf nodes of the given tree``    ``printLeafNodes(root)` `# This code is contributed by sanjeev2552`

## C#

 `// C# program to print leaf nodes``// from left to right``using` `System;`` ` `class` `GFG{``    ` `// A Binary Tree Node``class` `Node``{``    ``public` `int` `data;``    ``public` `Node left, right;``};` `// Function to print leaf``// nodes from left to right``static` `void` `printLeafNodes(Node root)``{``    ` `    ``// If node is null, return``    ``if` `(root == ``null``)``        ``return``;``    ` `    ``// If node is leaf node, print its data    ``    ``if` `(root.left == ``null` `&&``        ``root.right == ``null``)``    ``{``        ``Console.Write(root.data + ``" "``);``        ``return``;``    ``}``    ` `    ``// If left child exists, check for leaf``    ``// recursively``    ``if` `(root.left != ``null``)``        ``printLeafNodes(root.left);``        ` `    ``// If right child exists, check for leaf``    ``// recursively``    ``if` `(root.right != ``null``)``        ``printLeafNodes(root.right);``}` `// Utility function to create a new tree node``static` `Node newNode(``int` `data)``{``    ``Node temp = ``new` `Node();``    ``temp.data = data;``    ``temp.left = ``null``;``    ``temp.right = ``null``;``    ``return` `temp;``}` `// Driver code``public` `static` `void` `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);``}``}` `// This code is contributed by rutvik_56`

Output:

`4 6 7 9 10`

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

https://youtu.be/rBekvLt23wY?list=PLqM7alHXFySHCXD7r1J0ky9Zg_GBB1dbk
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.