Given a binary tree, we need 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,
Input : Root of the below tree
Output : 4 6 7 9 10
Corner Cases : For a tree with single node, the output should be the single node. And if root is null (empty tree), the output should be empty.
Using DFS :
The idea to do this is similar to DFS algorithm. Below is a step by step algorithm to do this:
- Check if the 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 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.
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *left, *right;
Node(int val) {
data = val;
left = nullptr;
right = nullptr;
}
};
// 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);
}
// Driver program to test above functions
int main()
{
Node *root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(8);
root->right->left->left = new Node(6);
root->right->left->right = new Node(7);
root->right->right->left = new Node(9);
root->right->right->right = new Node(10);
printLeafNodes(root);
return 0;
}
// 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 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# 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
<script>
// Javascript program to print leaf nodes
// from left to right
// A Binary Tree Node
class Node
{
constructor()
{
this.data = 0;
this.left = null;
this.right = null;
}
};
// Function to print leaf
// nodes from left to right
function printLeafNodes(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)
{
document.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
function newNode(data)
{
var temp = new Node();
temp.data = data;
temp.left = null;
temp.right = null;
return temp;
}
// Driver code
// Let us create binary tree shown in
// above diagram
var 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 rrrtnx
</script>
Output
4 6 7 9 10
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(h) where h is height of the binary tree
Using BFS:
Create an empty queue and push the root node into the queue.
Do the following while the queue is not empty:
- Remove (dequeue) the front node from the queue.
- If the node is a leaf node, print its data.
- Else:
- If the left child is not
NULL, enqueue the left child into the queue. - If the right child is not
NULL, enqueue the right child into the queue.
- If the left child is not
Below is the implementation of the above approach.
#include <bits/stdc++.h>
using namespace std;
// A Binary Tree Node
struct Node {
int data;
Node *left, *right;
Node(int d) {
data = d;
left = right = NULL;
}
};
// fun to print leaf nodes from left to right using BFS
void printleafNodes(Node* root)
{
// base case
if (!root)
return;
// BFS uses queue
queue<Node*> q;
q.push(root);
while (!q.empty()) {
root = q.front();
q.pop();
// if node is leaf node, print its data
if (!root->left && !root->right)
cout << root->data << " ";
if (root->left)
q.push(root->left);
if (root->right)
q.push(root->right);
}
}
// Driver program
int main()
{
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(8);
root->right->left->left = new Node(6);
root->right->left->right = new Node(7);
root->right->right->left = new Node(9);
root->right->right->right = new Node(10);
printleafNodes(root);
return 0;
}
// Java program to print leaf nodes from left to right using BFS
import java.util.*;
public class GFG {
// structure of node of Binary Tree
static class Node {
int data;
Node left, right;
Node(int item)
{
data = item;
left = right = null;
}
}
// fun to print leaf nodes from left to right
static void printleafNodes(Node root)
{
// base case
if (root == null)
return;
// BFS uses Queue
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
root = q.peek();
q.poll();
// if node is leaf node, print its data
if (root.left == null && root.right == null)
System.out.print(root.data + " ");
if (root.left != null)
q.add(root.left);
if (root.right != null)
q.add(root.right);
}
}
// Driver program
public static void main(String[] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(8);
root.right.left.left = new Node(6);
root.right.left.right = new Node(7);
root.right.right.left = new Node(9);
root.right.right.right = new Node(10);
printleafNodes(root);
}
}
# Python3 program to print leaf nodes from left to right using BFS
from collections import deque
# A Binary Tree Node
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# fun to creates and returns a new node
def newNode(data):
temp = Node(data)
return temp
# fun to print leaf nodes from left to right
def printleafNodes(root):
# base case
if not root:
return
# BFS uses queue
q = deque()
q.append(root)
while len(q) > 0:
root = q.popleft()
# if node is leaf node, print its data
if not root.left and not root.right:
print(root.data, end=" ")
if root.left:
q.append(root.left)
if root.right:
q.append(root.right)
# Driver program
if __name__ == '__main__':
# create a binary tree
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)
# prints leaf nodes of the given tree
printleafNodes(root)
using System;
using System.Collections.Generic;
class GFG {
// structure of node of Binary Tree
class Node {
public int data;
public Node left, right;
public Node(int item)
{
data = item;
left = right = null;
}
}
// fun to print leaf nodes from left to right
static void printleafNodes(Node root)
{
// base case
if (root == null)
return;
// BFS uses Queue
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count != 0) {
root = q.Peek();
q.Dequeue();
// if node is leaf node, print its data
if (root.left == null && root.right == null)
Console.Write(root.data + " ");
if (root.left != null)
q.Enqueue(root.left);
if (root.right != null)
q.Enqueue(root.right);
}
}
// Driver program
public static void Main(string[] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(8);
root.right.left.left = new Node(6);
root.right.left.right = new Node(7);
root.right.right.left = new Node(9);
root.right.right.right = new Node(10);
printleafNodes(root);
}
}
// Javascript program to print leaf nodes from left to right using BFS (Node.js)
// A Binary Tree Node
class Node {
constructor(data){
this.data = data;
this.left = null;
this.right = null;
}
}
// fun to print leaf nodes from left to right
function printleafNodes(root)
{
// base case
if (!root)
return;
// BFS uses queue
let q = [];
q.push(root);
while (q.length > 0) {
root = q.shift();
// if node is leaf node, print its data
if (!root.left && !root.right)
process.stdout.write(root.data + " ");
if (root.left)
q.push(root.left);
if (root.right)
q.push(root.right);
}
}
// Driver program
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(8);
root.right.left.left = new Node(6);
root.right.left.right = new Node(7);
root.right.right.left = new Node(9);
root.right.right.right = new Node(10);
printleafNodes(root);
Output
4 6 7 9 10
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(n)
