Product of all leaf nodes of binary tree

Here given code implementation process.
/*
C Program
Product of all leaf nodes of 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;
}
void leafNodeProduct(struct TreeNode *node, int *product)
{
if (node != NULL)
{
if (node->left == NULL && node->right == NULL)
{
( *product) *= node->data;
return;
}
// Recursively visiting left and right subtree
leafNodeProduct(node->left, product);
leafNodeProduct(node->right, product);
}
}
//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);
}
}
// Handles the request to find product of all leaf nodes
void leafProduct(struct TreeNode *root)
{
if (root == NULL)
{
printf("\n Empty Tree \n");
return;
}
int product = 1;
leafNodeProduct(root, & product);
inorder(root);
printf("\n Product of leaf nodes is : %d \n", product);
}
int main(int argc, char
const *argv[])
{
struct BinaryTree *tree = newTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree->root = newNode(1);
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(4);
tree->root->right->left->left = newNode(5);
tree->root->right->right->right = newNode(2);
// Leaf node (3,7,5,9,2)
leafProduct(tree->root);
return 0;
}
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
/*
Java Program
Product of all leaf nodes of 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 int product;
public BinaryTree()
{
this.root = null;
this.product = 1;
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
public void leafNodeProduct(TreeNode node)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
this.product *= node.data;
return;
}
// Recursively visiting left and right subtree
leafNodeProduct(node.left);
leafNodeProduct(node.right);
}
}
// Handles the request to find product of all leaf nodes
public void leafProduct()
{
if (this.root == null)
{
System.out.print("\n Empty Tree \n");
return;
}
this.product = 1;
leafNodeProduct(this.root);
inorder(this.root);
System.out.print("\n Product of leaf nodes is : " + this.product + " \n");
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
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(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
// Leaf node (3,7,5,9,2)
tree.leafProduct();
}
}
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Product of all leaf nodes of 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;
int product;
BinaryTree()
{
this->root = NULL;
this->product = 1;
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
void leafNodeProduct(TreeNode *node)
{
if (node != NULL)
{
if (node->left == NULL && node->right == NULL)
{
this->product *= node->data;
return;
}
// Recursively visiting left and right subtree
this->leafNodeProduct(node->left);
this->leafNodeProduct(node->right);
}
}
// Handles the request to find product of all leaf nodes
void leafProduct()
{
if (this->root == NULL)
{
cout << "\n Empty Tree \n";
return;
}
this->product = 1;
this->leafNodeProduct(this->root);
this->inorder(this->root);
cout << "\n Product of leaf nodes is : " << this->product << " \n";
}
};
int main()
{
BinaryTree tree = BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
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(4);
tree.root->right->left->left = new TreeNode(5);
tree.root->right->right->right = new TreeNode(2);
// Leaf node (3,7,5,9,2)
tree.leafProduct();
return 0;
}
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
// Include namespace system
using System;
/*
C# Program
Product of all leaf nodes of 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 int product;
public BinaryTree()
{
this.root = null;
this.product = 1;
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
public void leafNodeProduct(TreeNode node)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
this.product *= node.data;
return;
}
// Recursively visiting left and right subtree
leafNodeProduct(node.left);
leafNodeProduct(node.right);
}
}
// Handles the request to find product of all leaf nodes
public void leafProduct()
{
if (this.root == null)
{
Console.Write("\n Empty Tree \n");
return;
}
this.product = 1;
leafNodeProduct(this.root);
inorder(this.root);
Console.Write("\n Product of leaf nodes is : " + this.product + " \n");
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
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(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
// Leaf node (3,7,5,9,2)
tree.leafProduct();
}
}
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
<?php
/*
Php Program
Product of all leaf nodes of 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;
public $product;
function __construct()
{
$this->root = null;
$this->product = 1;
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
public function leafNodeProduct($node)
{
if ($node != null)
{
if ($node->left == null && $node->right == null)
{
$this->product *= $node->data;
return;
}
// Recursively visiting left and right subtree
$this->leafNodeProduct($node->left);
$this->leafNodeProduct($node->right);
}
}
// Handles the request to find product of all leaf nodes
public function leafProduct()
{
if ($this->root == null)
{
echo "\n Empty Tree \n";
return;
}
$this->product = 1;
$this->leafNodeProduct($this->root);
$this->inorder($this->root);
echo "\n Product of leaf nodes is : ". $this->product ." \n";
}
}
function main()
{
$tree = new BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
$tree->root = new TreeNode(1);
$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(4);
$tree->root->right->left->left = new TreeNode(5);
$tree->root->right->right->right = new TreeNode(2);
// Leaf node (3,7,5,9,2)
$tree->leafProduct();
}
main();
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
/*
Node Js Program
Product of all leaf nodes of 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;
this.product = 1;
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
leafNodeProduct(node)
{
if (node != null)
{
if (node.left == null && node.right == null)
{
this.product *= node.data;
return;
}
// Recursively visiting left and right subtree
this.leafNodeProduct(node.left);
this.leafNodeProduct(node.right);
}
}
// Handles the request to find product of all leaf nodes
leafProduct()
{
if (this.root == null)
{
process.stdout.write("\n Empty Tree \n");
return;
}
this.product = 1;
this.leafNodeProduct(this.root);
this.inorder(this.root);
process.stdout.write("\n Product of leaf nodes is : " + this.product + " \n");
}
}
function main()
{
var tree = new BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
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(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
// Leaf node (3,7,5,9,2)
tree.leafProduct();
}
main();
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
# Python 3 Program
# Product of all leaf nodes of 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
self.product = 1
# 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)
# Find the product of all exist leaf nodes in given binary tree
def leafNodeProduct(self, node) :
if (node != None) :
if (node.left == None and node.right == None) :
self.product *= node.data
return
# Recursively visiting left and right subtree
self.leafNodeProduct(node.left)
self.leafNodeProduct(node.right)
# Handles the request to find product of all leaf nodes
def leafProduct(self) :
if (self.root == None) :
print("\n Empty Tree ")
return
self.product = 1
self.leafNodeProduct(self.root)
self.inorder(self.root)
print("\n Product of leaf nodes is : ", self.product ," ")
def main() :
tree = BinaryTree()
#
# 1
# / \
# / \
# 11 8
# / \ / \
# 3 10 6 4
# / / \ \
# 7 5 9 2
# -----------------------
# Binary Tree
# -----------------------
tree.root = TreeNode(1)
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(4)
tree.root.right.left.left = TreeNode(5)
tree.root.right.right.right = TreeNode(2)
# Leaf node (3,7,5,9,2)
tree.leafProduct()
if __name__ == "__main__": main()
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
# Ruby Program
# Product of all leaf nodes of binary tree
# Binary Tree node
class TreeNode
# Define the accessor and reader of class TreeNode
attr_reader :data, :left, :right
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_reader :root, :product
attr_accessor :root, :product
def initialize()
self.root = nil
self.product = 1
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
# Find the product of all exist leaf nodes in given binary tree
def leafNodeProduct(node)
if (node != nil)
if (node.left == nil && node.right == nil)
self.product *= node.data
return
end
# Recursively visiting left and right subtree
self.leafNodeProduct(node.left)
self.leafNodeProduct(node.right)
end
end
# Handles the request to find product of all leaf nodes
def leafProduct()
if (self.root == nil)
print("\n Empty Tree \n")
return
end
self.product = 1
self.leafNodeProduct(self.root)
self.inorder(self.root)
print("\n Product of leaf nodes is : ", self.product ," \n")
end
end
def main()
tree = BinaryTree.new()
#
# 1
# / \
# / \
# 11 8
# / \ / \
# 3 10 6 4
# / / \ \
# 7 5 9 2
# -----------------------
# Binary Tree
# -----------------------
tree.root = TreeNode.new(1)
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(4)
tree.root.right.left.left = TreeNode.new(5)
tree.root.right.right.right = TreeNode.new(2)
# Leaf node (3,7,5,9,2)
tree.leafProduct()
end
main()
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
/*
Scala Program
Product of all leaf nodes of 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 , var product: Int)
{
def this()
{
this(null, 1);
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
def leafNodeProduct(node: TreeNode): Unit = {
if (node != null)
{
if (node.left == null && node.right == null)
{
this.product *= node.data;
return;
}
// Recursively visiting left and right subtree
this.leafNodeProduct(node.left);
this.leafNodeProduct(node.right);
}
}
// Handles the request to find product of all leaf nodes
def leafProduct(): Unit = {
if (this.root == null)
{
print("\n Empty Tree \n");
return;
}
this.product = 1;
this.leafNodeProduct(this.root);
this.inorder(this.root);
print("\n Product of leaf nodes is : " + this.product + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree.root = new TreeNode(1);
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(4);
tree.root.right.left.left = new TreeNode(5);
tree.root.right.right.right = new TreeNode(2);
// Leaf node (3,7,5,9,2)
tree.leafProduct();
}
}
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
/*
Swift 4 Program
Product of all leaf nodes of 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? ;
var product: Int;
init()
{
self.root = nil;
self.product = 1;
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
func leafNodeProduct(_ node: TreeNode? )
{
if (node != nil)
{
if (node!.left == nil && node!.right == nil)
{
self.product *= node!.data;
return;
}
// Recursively visiting left and right subtree
self.leafNodeProduct(node!.left);
self.leafNodeProduct(node!.right);
}
}
// Handles the request to find product of all leaf nodes
func leafProduct()
{
if (self.root == nil)
{
print("\n Empty Tree ");
return;
}
self.product = 1;
self.leafNodeProduct(self.root);
self.inorder(self.root);
print("\n Product of leaf nodes is : ", self.product ," ");
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree.root = TreeNode(1);
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(4);
tree.root!.right!.left!.left = TreeNode(5);
tree.root!.right!.right!.right = TreeNode(2);
// Leaf node (3,7,5,9,2)
tree.leafProduct();
}
main();
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
/*
Kotlin Program
Product of all leaf nodes of 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 ? ;
var product: Int;
constructor()
{
this.root = null;
this.product = 1;
}
//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);
}
}
// Find the product of all exist leaf nodes in given binary tree
fun leafNodeProduct(node: TreeNode ? ): Unit
{
if (node != null)
{
if (node.left == null && node.right == null)
{
this.product *= node.data;
return;
}
// Recursively visiting left and right subtree
this.leafNodeProduct(node.left);
this.leafNodeProduct(node.right);
}
}
// Handles the request to find product of all leaf nodes
fun leafProduct(): Unit
{
if (this.root == null)
{
print("\n Empty Tree \n");
return;
}
this.product = 1;
this.leafNodeProduct(this.root);
this.inorder(this.root);
print("\n Product of leaf nodes is : " + this.product + " \n");
}
}
fun main(args: Array < String > ): Unit
{
var tree: BinaryTree = BinaryTree();
/*
1
/ \
/ \
11 8
/ \ / \
3 10 6 4
/ / \ \
7 5 9 2
-----------------------
Binary Tree
-----------------------
*/
tree.root = TreeNode(1);
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(4);
tree.root?.right?.left?.left = TreeNode(5);
tree.root?.right?.right?.right = TreeNode(2);
// Leaf node (3,7,5,9,2)
tree.leafProduct();
}
Output
3 11 7 10 1 5 6 9 8 4 2
Product of leaf nodes is : 1890
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.
New Comment