Posted on by Kalkicode
Code Binary Tree

# Print left and right leaf nodes in Binary Tree

Here given code implementation process.

``````/*
C Program
Print left and right leaf nodes in Binary Tree
*/
#include <stdio.h>
#include <stdlib.h>

// Tree Node
struct TreeNode
{
int data;
struct TreeNode *left;
struct TreeNode *right;
};
// Binary Tree
struct BinaryTree
{
struct TreeNode *root;
};
// Create new tree
struct BinaryTree *newTree()
{
// Create dynamic node
struct BinaryTree *tree = (struct BinaryTree *) malloc(sizeof(struct BinaryTree));
if (tree != NULL)
{
tree->root = NULL;
}
else
{
printf("Memory Overflow to Create tree Tree\n");
}
//return new tree
return tree;
}
// returns a new node of tree
struct TreeNode *newNode(int data)
{
// Create dynamic node
struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode));
if (node != NULL)
{
//Set data and pointer values
node->data = data;
node->left = NULL;
node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return node;
}
//Display Inorder view of binary tree
void inorder(struct TreeNode *node)
{
if (node)
{
inorder(node->left);
//Print node value
printf("  %d", node->data);
inorder(node->right);
}
}
// Print all left leaf nodes
void printLeftLeaf(struct TreeNode *node, int status)
{
if (node != NULL)
{
if (node->left == NULL && node->right == NULL)
{
if (status == 1)
{
// When get resultant left leaf node
printf("  %d", node->data);
}
return;
}
// Recursively visiting left and right subtree
printLeftLeaf(node->left, 1);
printLeftLeaf(node->right, 0);
}
}
// Print all right leaf nodes
void printRightLeaf(struct TreeNode *node, int status)
{
if (node != NULL)
{
if (node->left == NULL && node->right == NULL)
{
if (status == 1)
{
// When get resultant right leaf node
printf("  %d", node->data);
}
return;
}
// Recursively visiting left and right subtree
printRightLeaf(node->left, 0);
printRightLeaf(node->right, 1);
}
}
// Handles the request of printing leaf nodes value
void printLeafNodes(struct TreeNode *root)
{
if (root == NULL)
{
printf("\n Empty Tree \n");
return;
}
// Print all tree elements
printf("\n Tree Nodes\n");
inorder(root);
// Print all left leaf nodes
printf("\n Left Leaf Nodes\n");
printLeftLeaf(root, 1);
// Print all right leaf nodes
printf("\n Right Leaf Nodes\n");
printRightLeaf(root, 0);
printf("\n");
}
int main(int argc, char
const *argv[])
{
struct BinaryTree *tree = newTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree->root = newNode(15);
tree->root->left = newNode(11);
tree->root->right = newNode(8);
tree->root->left->left = newNode(3);
tree->root->left->right = newNode(10);
tree->root->left->right->left = newNode(7);
tree->root->right->left = newNode(6);
tree->root->right->left->right = newNode(9);
tree->root->right->right = newNode(14);
tree->root->right->left->left = newNode(5);
tree->root->right->right->right = newNode(2);
tree->root->left->right->right = newNode(4);
// Leaf node (3,7,4,5,9,2)
printLeafNodes(tree->root);
return 0;
}``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3  7  5
Right Leaf Nodes
4  9  2``````
``````/*
Java Program
Print left and right leaf nodes in Binary Tree
*/
// Binary Tree node
class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public BinaryTree()
{
this.root = null;
}
//Display Inorder view of binary tree
public void inorder(TreeNode node)
{
if (node != null)
{
inorder(node.left);
//Print node value
System.out.print("  " + node.data);
inorder(node.right);
}
}
// Print all left leaf nodes
public void printLeftLeaf(TreeNode node, boolean status)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant left leaf node
System.out.print(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
printLeftLeaf(node.left, true);
printLeftLeaf(node.right, false);
}
}
// Print all right leaf nodes
public void printRightLeaf(TreeNode node, boolean status)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant right leaf node
System.out.print(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
printRightLeaf(node.left, false);
printRightLeaf(node.right, true);
}
}
// Handles the request of printing leaf nodes value
public void printLeafNodes()
{
if (this.root == null)
{
System.out.print("\n Empty Tree \n");
return;
}
// Print all tree elements
System.out.print("\n Tree Nodes\n");
inorder(this.root);
// Print all left leaf nodes
System.out.print("\n Left Leaf Nodes\n");
printLeftLeaf(this.root, true);
// Print all right leaf nodes
System.out.print("\n Right Leaf Nodes\n");
printRightLeaf(this.root, false);
System.out.print("\n");
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(15);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(14);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
tree.root.left.right.right = new TreeNode(4);
// Leaf node (3,7,4,5,9,2)
tree.printLeafNodes();
}
}``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Print left and right leaf nodes in Binary Tree
*/

// Binary Tree node
class TreeNode
{
public: int data;
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
// Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
// Define Binary Tree
class BinaryTree
{
public: TreeNode *root;
BinaryTree()
{
this->root = NULL;
}
//Display Inorder view of binary tree
void inorder(TreeNode *node)
{
if (node != NULL)
{
this->inorder(node->left);
//Print node value
cout << "  " << node->data;
this->inorder(node->right);
}
}
// Print all left leaf nodes
void printLeftLeaf(TreeNode *node, bool status)
{
if (node != NULL)
{
if (node->left == NULL && node->right == NULL)
{
if (status == true)
{
// When get resultant left leaf node
cout << " " << node->data;
}
return;
}
// Recursively visiting left and right subtree
this->printLeftLeaf(node->left, true);
this->printLeftLeaf(node->right, false);
}
}
// Print all right leaf nodes
void printRightLeaf(TreeNode *node, bool status)
{
if (node != NULL)
{
if (node->left == NULL && node->right == NULL)
{
if (status == true)
{
// When get resultant right leaf node
cout << " " << node->data;
}
return;
}
// Recursively visiting left and right subtree
this->printRightLeaf(node->left, false);
this->printRightLeaf(node->right, true);
}
}
// Handles the request of printing leaf nodes value
void printLeafNodes()
{
if (this->root == NULL)
{
cout << "\n Empty Tree \n";
return;
}
// Print all tree elements
cout << "\n Tree Nodes\n";
this->inorder(this->root);
// Print all left leaf nodes
cout << "\n Left Leaf Nodes\n";
this->printLeftLeaf(this->root, true);
// Print all right leaf nodes
cout << "\n Right Leaf Nodes\n";
this->printRightLeaf(this->root, false);
cout << "\n";
}
};
int main()
{
BinaryTree tree = BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(15);
tree.root->left = new TreeNode(11);
tree.root->right = new TreeNode(8);
tree.root->left->left = new TreeNode(3);
tree.root->left->right = new TreeNode(10);
tree.root->left->right->left = new TreeNode(7);
tree.root->right->left = new TreeNode(6);
tree.root->right->left->right = new TreeNode(9);
tree.root->right->right = new TreeNode(14);
tree.root->right->left->left = new TreeNode(5);
tree.root->right->right->right = new TreeNode(2);
tree.root->left->right->right = new TreeNode(4);
// Leaf node (3,7,4,5,9,2)
tree.printLeafNodes();
return 0;
}``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2``````
``````// Include namespace system
using System;
/*
C# Program
Print left and right leaf nodes in Binary Tree
*/
// Binary Tree node
public class TreeNode
{
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
public class BinaryTree
{
public TreeNode root;
public BinaryTree()
{
this.root = null;
}
//Display Inorder view of binary tree
public void inorder(TreeNode node)
{
if (node != null)
{
inorder(node.left);
//Print node value
Console.Write("  " + node.data);
inorder(node.right);
}
}
// Print all left leaf nodes
public void printLeftLeaf(TreeNode node, Boolean status)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant left leaf node
Console.Write(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
printLeftLeaf(node.left, true);
printLeftLeaf(node.right, false);
}
}
// Print all right leaf nodes
public void printRightLeaf(TreeNode node, Boolean status)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant right leaf node
Console.Write(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
printRightLeaf(node.left, false);
printRightLeaf(node.right, true);
}
}
// Handles the request of printing leaf nodes value
public void printLeafNodes()
{
if (this.root == null)
{
Console.Write("\n Empty Tree \n");
return;
}
// Print all tree elements
Console.Write("\n Tree Nodes\n");
inorder(this.root);
// Print all left leaf nodes
Console.Write("\n Left Leaf Nodes\n");
printLeftLeaf(this.root, true);
// Print all right leaf nodes
Console.Write("\n Right Leaf Nodes\n");
printRightLeaf(this.root, false);
Console.Write("\n");
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(15);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(14);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
tree.root.left.right.right = new TreeNode(4);
// Leaf node (3,7,4,5,9,2)
tree.printLeafNodes();
}
}``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2``````
``````<?php
/*
Php Program
Print left and right leaf nodes in Binary Tree
*/
// Binary Tree node
class TreeNode
{
public \$data;
public \$left;
public \$right;

function __construct(\$data)
{
// Set node value
\$this->data = \$data;
\$this->left = null;
\$this->right = null;
}
}
// Define Binary Tree
class BinaryTree
{
public \$root;

function __construct()
{
\$this->root = null;
}
//Display Inorder view of binary tree
public	function inorder(\$node)
{
if (\$node != null)
{
\$this->inorder(\$node->left);
//Print node value
echo "  ". \$node->data;
\$this->inorder(\$node->right);
}
}
// Print all left leaf nodes
public	function printLeftLeaf(\$node, \$status)
{
if (\$node != null)
{
if (\$node->left == null && \$node->right == null)
{
if (\$status == true)
{
// When get resultant left leaf node
echo " ". \$node->data;
}
return;
}
// Recursively visiting left and right subtree
\$this->printLeftLeaf(\$node->left, true);
\$this->printLeftLeaf(\$node->right, false);
}
}
// Print all right leaf nodes
public	function printRightLeaf(\$node, \$status)
{
if (\$node != null)
{
if (\$node->left == null && \$node->right == null)
{
if (\$status == true)
{
// When get resultant right leaf node
echo " ". \$node->data;
}
return;
}
// Recursively visiting left and right subtree
\$this->printRightLeaf(\$node->left, false);
\$this->printRightLeaf(\$node->right, true);
}
}
// Handles the request of printing leaf nodes value
public	function printLeafNodes()
{
if (\$this->root == null)
{
echo "\n Empty Tree \n";
return;
}
// Print all tree elements
echo "\n Tree Nodes\n";
\$this->inorder(\$this->root);
// Print all left leaf nodes
echo "\n Left Leaf Nodes\n";
\$this->printLeftLeaf(\$this->root, true);
// Print all right leaf nodes
echo "\n Right Leaf Nodes\n";
\$this->printRightLeaf(\$this->root, false);
echo "\n";
}
}

function main()
{
\$tree = new BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
\$tree->root = new TreeNode(15);
\$tree->root->left = new TreeNode(11);
\$tree->root->right = new TreeNode(8);
\$tree->root->left->left = new TreeNode(3);
\$tree->root->left->right = new TreeNode(10);
\$tree->root->left->right->left = new TreeNode(7);
\$tree->root->right->left = new TreeNode(6);
\$tree->root->right->left->right = new TreeNode(9);
\$tree->root->right->right = new TreeNode(14);
\$tree->root->right->left->left = new TreeNode(5);
\$tree->root->right->right->right = new TreeNode(2);
\$tree->root->left->right->right = new TreeNode(4);
// Leaf node (3,7,4,5,9,2)
\$tree->printLeafNodes();
}
main();``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2``````
``````/*
Node Js Program
Print left and right leaf nodes in Binary Tree
*/
// Binary Tree node
class TreeNode
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
class BinaryTree
{
constructor()
{
this.root = null;
}
//Display Inorder view of binary tree
inorder(node)
{
if (node != null)
{
this.inorder(node.left);
//Print node value
process.stdout.write("  " + node.data);
this.inorder(node.right);
}
}
// Print all left leaf nodes
printLeftLeaf(node, status)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant left leaf node
process.stdout.write(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
this.printLeftLeaf(node.left, true);
this.printLeftLeaf(node.right, false);
}
}
// Print all right leaf nodes
printRightLeaf(node, status)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant right leaf node
process.stdout.write(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
this.printRightLeaf(node.left, false);
this.printRightLeaf(node.right, true);
}
}
// Handles the request of printing leaf nodes value
printLeafNodes()
{
if (this.root == null)
{
process.stdout.write("\n Empty Tree \n");
return;
}
// Print all tree elements
process.stdout.write("\n Tree Nodes\n");
this.inorder(this.root);
// Print all left leaf nodes
process.stdout.write("\n Left Leaf Nodes\n");
this.printLeftLeaf(this.root, true);
// Print all right leaf nodes
process.stdout.write("\n Right Leaf Nodes\n");
this.printRightLeaf(this.root, false);
process.stdout.write("\n");
}
}

function main()
{
var tree = new BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(15);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(14);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
tree.root.left.right.right = new TreeNode(4);
// Leaf node (3,7,4,5,9,2)
tree.printLeafNodes();
}
main();``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2``````
``````#  Python 3 Program
#  Print left and right leaf nodes in Binary Tree

#  Binary Tree node
class TreeNode :

def __init__(self, data) :
#  Set node value
self.data = data
self.left = None
self.right = None

#  Define Binary Tree
class BinaryTree :

def __init__(self) :
self.root = None

# Display Inorder view of binary tree
def inorder(self, node) :
if (node != None) :
self.inorder(node.left)
# Print node value
print("  ", node.data, end = "")
self.inorder(node.right)

#  Print all left leaf nodes
def printLeftLeaf(self, node, status) :
if (node != None) :
if (node.left == None and node.right == None) :
if (status == True) :
#  When get resultant left leaf node
print(" ", node.data, end = "")

return

#  Recursively visiting left and right subtree
self.printLeftLeaf(node.left, True)
self.printLeftLeaf(node.right, False)

#  Print all right leaf nodes
def printRightLeaf(self, node, status) :
if (node != None) :
if (node.left == None and node.right == None) :
if (status == True) :
#  When get resultant right leaf node
print(" ", node.data, end = "")

return

#  Recursively visiting left and right subtree
self.printRightLeaf(node.left, False)
self.printRightLeaf(node.right, True)

#  Handles the request of printing leaf nodes value
def printLeafNodes(self) :
if (self.root == None) :
print("\n Empty Tree ")
return

#  Print all tree elements
print("\n Tree Nodes")
self.inorder(self.root)
#  Print all left leaf nodes
print("\n Left Leaf Nodes")
self.printLeftLeaf(self.root, True)
#  Print all right leaf nodes
print("\n Right Leaf Nodes")
self.printRightLeaf(self.root, False)
print(end = "\n")

def main() :
tree = BinaryTree()
#
#             15
#           /   \
#          /     \
#         /       \
#        /         \
#       11          8
#      /  \        / \
#     3   10      6   14
#        /  \    / \   \
#       7    4  5   9   2
#     -----------------------
#        Binary Tree
#     -----------------------

tree.root = TreeNode(15)
tree.root.left = TreeNode(11)
tree.root.right = TreeNode(8)
tree.root.left.left = TreeNode(3)
tree.root.left.right = TreeNode(10)
tree.root.left.right.left = TreeNode(7)
tree.root.right.left = TreeNode(6)
tree.root.right.left.right = TreeNode(9)
tree.root.right.right = TreeNode(14)
tree.root.right.left.left = TreeNode(5)
tree.root.right.right.right = TreeNode(2)
tree.root.left.right.right = TreeNode(4)
#  Leaf node (3,7,4,5,9,2)
tree.printLeafNodes()

if __name__ == "__main__": main()``````

#### Output

`````` Tree Nodes
3   11   7   10   4   15   5   6   9   8   14   2
Left Leaf Nodes
3  7  5
Right Leaf Nodes
4  9  2``````
``````#    Ruby Program
#    Print left and right leaf nodes in Binary Tree

#  Binary Tree node
class TreeNode
# Define the accessor and reader of class TreeNode
attr_accessor :data, :left, :right

def initialize(data)
#  Set node value
self.data = data
self.left = nil
self.right = nil
end

end

#  Define Binary Tree
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_accessor :root

def initialize()
self.root = nil
end

# Display Inorder view of binary tree
def inorder(node)
if (node != nil)
self.inorder(node.left)
# Print node value
print("  ", node.data)
self.inorder(node.right)
end

end

#  Print all left leaf nodes
def printLeftLeaf(node, status)
if (node != nil)
if (node.left == nil && node.right == nil)
if (status == true)
#  When get resultant left leaf node
print(" ", node.data)
end

return
end

#  Recursively visiting left and right subtree
self.printLeftLeaf(node.left, true)
self.printLeftLeaf(node.right, false)
end

end

#  Print all right leaf nodes
def printRightLeaf(node, status)
if (node != nil)
if (node.left == nil && node.right == nil)
if (status == true)
#  When get resultant right leaf node
print(" ", node.data)
end

return
end

#  Recursively visiting left and right subtree
self.printRightLeaf(node.left, false)
self.printRightLeaf(node.right, true)
end

end

#  Handles the request of printing leaf nodes value
def printLeafNodes()
if (self.root == nil)
print("\n Empty Tree \n")
return
end

#  Print all tree elements
print("\n Tree Nodes\n")
self.inorder(self.root)
#  Print all left leaf nodes
print("\n Left Leaf Nodes\n")
self.printLeftLeaf(self.root, true)
#  Print all right leaf nodes
print("\n Right Leaf Nodes\n")
self.printRightLeaf(self.root, false)
print("\n")
end

end

def main()
tree = BinaryTree.new()
#
#             15
#           /   \
#          /     \
#         /       \
#        /         \
#       11          8
#      /  \        / \
#     3   10      6   14
#        /  \    / \   \
#       7    4  5   9   2
#     -----------------------
#        Binary Tree
#     -----------------------

tree.root = TreeNode.new(15)
tree.root.left = TreeNode.new(11)
tree.root.right = TreeNode.new(8)
tree.root.left.left = TreeNode.new(3)
tree.root.left.right = TreeNode.new(10)
tree.root.left.right.left = TreeNode.new(7)
tree.root.right.left = TreeNode.new(6)
tree.root.right.left.right = TreeNode.new(9)
tree.root.right.right = TreeNode.new(14)
tree.root.right.left.left = TreeNode.new(5)
tree.root.right.right.right = TreeNode.new(2)
tree.root.left.right.right = TreeNode.new(4)
#  Leaf node (3,7,4,5,9,2)
tree.printLeafNodes()
end

main()``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2
``````
``````/*
Scala Program
Print left and right leaf nodes in Binary Tree
*/
// Binary Tree node
class TreeNode(var data: Int , var left: TreeNode , var right: TreeNode)
{
def this(data: Int)
{
this(data, null, null);
}
}
// Define Binary Tree
class BinaryTree(var root: TreeNode)
{
def this()
{
this(null);
}
//Display Inorder view of binary tree
def inorder(node: TreeNode): Unit = {
if (node != null)
{
this.inorder(node.left);
//Print node value
print("  " + node.data);
this.inorder(node.right);
}
}
// Print all left leaf nodes
def printLeftLeaf(node: TreeNode, status: Boolean): Unit = {
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant left leaf node
print(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
this.printLeftLeaf(node.left, true);
this.printLeftLeaf(node.right, false);
}
}
// Print all right leaf nodes
def printRightLeaf(node: TreeNode, status: Boolean): Unit = {
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant right leaf node
print(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
this.printRightLeaf(node.left, false);
this.printRightLeaf(node.right, true);
}
}
// Handles the request of printing leaf nodes value
def printLeafNodes(): Unit = {
if (this.root == null)
{
print("\n Empty Tree \n");
return;
}
// Print all tree elements
print("\n Tree Nodes\n");
this.inorder(this.root);
// Print all left leaf nodes
print("\n Left Leaf Nodes\n");
this.printLeftLeaf(this.root, true);
// Print all right leaf nodes
print("\n Right Leaf Nodes\n");
this.printRightLeaf(this.root, false);
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(15);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(8);
tree.root.left.left = new TreeNode(3);
tree.root.left.right = new TreeNode(10);
tree.root.left.right.left = new TreeNode(7);
tree.root.right.left = new TreeNode(6);
tree.root.right.left.right = new TreeNode(9);
tree.root.right.right = new TreeNode(14);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
tree.root.left.right.right = new TreeNode(4);
// Leaf node (3,7,4,5,9,2)
tree.printLeafNodes();
}
}``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2``````
``````/*
Swift 4 Program
Print left and right leaf nodes in Binary Tree
*/
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode? ;
var right: TreeNode? ;
init(_ data: Int)
{
// Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode? ;
init()
{
self.root = nil;
}
//Display Inorder view of binary tree
func inorder(_ node: TreeNode? )
{
if (node  != nil)
{
self.inorder(node!.left);
//Print node value
print("  ", node!.data, terminator: "");
self.inorder(node!.right);
}
}
// Print all left leaf nodes
func printLeftLeaf(_ node: TreeNode? , _ status : Bool)
{
if (node  != nil)
{
if (node!.left == nil && node!.right == nil)
{
if (status == true)
{
// When get resultant left leaf node
print(" ", node!.data, terminator: "");
}
return;
}
// Recursively visiting left and right subtree
self.printLeftLeaf(node!.left, true);
self.printLeftLeaf(node!.right, false);
}
}
// Print all right leaf nodes
func printRightLeaf(_ node: TreeNode? , _ status : Bool)
{
if (node  != nil)
{
if (node!.left == nil && node!.right == nil)
{
if (status == true)
{
// When get resultant right leaf node
print(" ", node!.data, terminator: "");
}
return;
}
// Recursively visiting left and right subtree
self.printRightLeaf(node!.left, false);
self.printRightLeaf(node!.right, true);
}
}
// Handles the request of printing leaf nodes value
func printLeafNodes()
{
if (self.root == nil)
{
print("\n Empty Tree ");
return;
}
// Print all tree elements
print("\n Tree Nodes");
self.inorder(self.root);
// Print all left leaf nodes
print("\n Left Leaf Nodes");
self.printLeftLeaf(self.root, true);
// Print all right leaf nodes
print("\n Right Leaf Nodes");
self.printRightLeaf(self.root, false);
print(terminator: "\n");
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree.root = TreeNode(15);
tree.root!.left = TreeNode(11);
tree.root!.right = TreeNode(8);
tree.root!.left!.left = TreeNode(3);
tree.root!.left!.right = TreeNode(10);
tree.root!.left!.right!.left = TreeNode(7);
tree.root!.right!.left = TreeNode(6);
tree.root!.right!.left!.right = TreeNode(9);
tree.root!.right!.right = TreeNode(14);
tree.root!.right!.left!.left = TreeNode(5);
tree.root!.right!.right!.right = TreeNode(2);
tree.root!.left!.right!.right = TreeNode(4);
// Leaf node (3,7,4,5,9,2)
tree.printLeafNodes();
}
main();``````

#### Output

`````` Tree Nodes
3   11   7   10   4   15   5   6   9   8   14   2
Left Leaf Nodes
3  7  5
Right Leaf Nodes
4  9  2``````
``````/*
Kotlin Program
Print left and right leaf nodes in Binary Tree
*/
// Binary Tree node
class TreeNode
{
var data: Int;
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
// Define Binary Tree
class BinaryTree
{
var root: TreeNode ? ;
constructor()
{
this.root = null;
}
//Display Inorder view of binary tree
fun inorder(node: TreeNode ? ): Unit
{
if (node != null)
{
this.inorder(node.left);
//Print node value
print("  " + node.data);
this.inorder(node.right);
}
}
// Print all left leaf nodes
fun printLeftLeaf(node: TreeNode ? , status : Boolean): Unit
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant left leaf node
print(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
this.printLeftLeaf(node.left, true);
this.printLeftLeaf(node.right, false);
}
}
// Print all right leaf nodes
fun printRightLeaf(node: TreeNode ? , status : Boolean): Unit
{
if (node != null)
{
if (node.left == null && node.right == null)
{
if (status == true)
{
// When get resultant right leaf node
print(" " + node.data);
}
return;
}
// Recursively visiting left and right subtree
this.printRightLeaf(node.left, false);
this.printRightLeaf(node.right, true);
}
}
// Handles the request of printing leaf nodes value
fun printLeafNodes(): Unit
{
if (this.root == null)
{
print("\n Empty Tree \n");
return;
}
// Print all tree elements
print("\n Tree Nodes\n");
this.inorder(this.root);
// Print all left leaf nodes
print("\n Left Leaf Nodes\n");
this.printLeftLeaf(this.root, true);
// Print all right leaf nodes
print("\n Right Leaf Nodes\n");
this.printRightLeaf(this.root, false);
print("\n");
}
}
fun main(args: Array < String > ): Unit
{
var tree: BinaryTree = BinaryTree();
/*
15
/   \
/     \
/       \
/         \
11          8
/  \        / \
3   10      6   14
/  \    / \   \
7    4  5   9   2
-----------------------
Binary Tree
-----------------------
*/
tree.root = TreeNode(15);
tree.root?.left = TreeNode(11);
tree.root?.right = TreeNode(8);
tree.root?.left?.left = TreeNode(3);
tree.root?.left?.right = TreeNode(10);
tree.root?.left?.right?.left = TreeNode(7);
tree.root?.right?.left = TreeNode(6);
tree.root?.right?.left?.right = TreeNode(9);
tree.root?.right?.right = TreeNode(14);
tree.root?.right?.left?.left = TreeNode(5);
tree.root?.right?.right?.right = TreeNode(2);
tree.root?.left?.right?.right = TreeNode(4);
// Leaf node (3,7,4,5,9,2)
tree.printLeafNodes();
}``````

#### Output

`````` Tree Nodes
3  11  7  10  4  15  5  6  9  8  14  2
Left Leaf Nodes
3 7 5
Right Leaf Nodes
4 9 2``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post