Posted on by Kalkicode
Code Binary Tree

# Find the minimum leaf node in a binary tree

Here given code implementation process.

``````// C program for
// Find the minimum leaf node in a 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;
}
// This is creates and returns the new binary tree node
struct TreeNode *newNode(int data)
{
// Create dynamic node
struct TreeNode *new_node =
(struct TreeNode *) malloc(sizeof(struct TreeNode));
if (new_node != NULL)
{
//Set data and pointer values
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
}
else
{
// This is indicates,
// Segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return new_node;
}
// Find the value of min leaf node recursively
void findMinLeaf(struct TreeNode *node, struct TreeNode **result)
{
if (node == NULL)
{
return;
}
if (node->left == NULL && node->right == NULL)
{
if ( *result == NULL)
{
// First leaf node
*result = node;
}
else if (( *result)->data > node->data)
{
*result = node;
}
}
// Visit left and right subtree
findMinLeaf(node->left, result);
findMinLeaf(node->right, result);
}
// Handles the request of finding the minimum leaf node in binary tree.
void minLeafNode(struct TreeNode *root)
{
if (root == NULL)
{
return;
}
struct TreeNode *result = NULL;
// Find node value
findMinLeaf(root, & result);
if (result != NULL)
{
printf(" Minimum leaf node value is : %d\n", result->data);
}
else
{
printf(" Minimum left is : None \n");
}
}
int main(int argc, char
const *argv[])
{
struct BinaryTree *tree = newTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13

-----------------
Binary tree

*/
tree->root = newNode(4);
tree->root->left = newNode(-4);
tree->root->left->right = newNode(3);
tree->root->left->right->left = newNode(10);
tree->root->left->right->left->left = newNode(9);
tree->root->left->right->left->right = newNode(21);
tree->root->left->right->right = newNode(8);
tree->root->left->left = newNode(2);
tree->root->left->left->left = newNode(1);
tree->root->left->left->left->left = newNode(31);
tree->root->right = newNode(7);
tree->root->right->right = newNode(12);
tree->root->right->right->left = newNode(15);
tree->root->right->right->left->right = newNode(13);
minLeafNode(tree->root);
return 0;
}``````

#### Output

`` Minimum leaf node value is : 8``
``````/*
Java program for
Find the minimum leaf node in a 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;
}
}
public class BinaryTree
{
public TreeNode root;
public TreeNode result;
public BinaryTree()
{
// Set initial value
this.root = null;
this.result = null;
}
// Find the value of min leaf node recursively
public void findMinLeaf(TreeNode node)
{
if (node == null)
{
return;
}
if (node.left == null && node.right == null)
{
if (this.result == null)
{
// First leaf node
this.result = node;
}
else if (this.result.data > node.data)
{
this.result = node;
}
}
// Visit left and right subtree
findMinLeaf(node.left);
findMinLeaf(node.right);
}
// Handles the request of finding the minimum leaf node in binary tree.
public void minLeafNode()
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMinLeaf(this.root);
if (result != null)
{
System.out.print(" Minimum leaf node value is : " +
(this.result.data) + "\n");
}
else
{
System.out.print(" Minimum left is : None \n");
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13

-----------------
Binary tree
*/
tree.root = new TreeNode(4);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(21);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(1);
tree.root.left.left.left.left = new TreeNode(31);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(15);
tree.root.right.right.left.right = new TreeNode(13);
tree.minLeafNode();
}
}``````

#### Output

`` Minimum leaf node value is : 8``
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Find the minimum leaf node in a 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;
}
};
class BinaryTree
{
public:
TreeNode *root;
TreeNode *result;
BinaryTree()
{
this->root = NULL;
this->result = NULL;
}
// Find the value of min leaf node recursively
void findMinLeaf(TreeNode *node)
{
if (node == NULL)
{
return;
}
if (node->left == NULL && node->right == NULL)
{
if (this->result == NULL)
{
// First leaf node
this->result = node;
}
else if (this->result->data > node->data)
{
this->result = node;
}
}
// Visit left and right subtree
this->findMinLeaf(node->left);
this->findMinLeaf(node->right);
}
// Handles the request of finding the
// minimum leaf node in binary tree.
void minLeafNode()
{
if (this->root == NULL)
{
return;
}
this->result = NULL;
// Find node value
this->findMinLeaf(this->root);
if (this->result != NULL)
{
cout << " Minimum leaf node value is : "
<< (this->result->data) << "\n";
}
else
{
cout << " Minimum left is : None \n";
}
}
};
int main()
{
BinaryTree *tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
tree->root = new TreeNode(4);
tree->root->left = new TreeNode(-4);
tree->root->left->right = new TreeNode(3);
tree->root->left->right->left = new TreeNode(10);
tree->root->left->right->left->left = new TreeNode(9);
tree->root->left->right->left->right = new TreeNode(21);
tree->root->left->right->right = new TreeNode(8);
tree->root->left->left = new TreeNode(2);
tree->root->left->left->left = new TreeNode(1);
tree->root->left->left->left->left = new TreeNode(31);
tree->root->right = new TreeNode(7);
tree->root->right->right = new TreeNode(12);
tree->root->right->right->left = new TreeNode(15);
tree->root->right->right->left->right = new TreeNode(13);
tree->minLeafNode();
return 0;
}``````

#### Output

`` Minimum leaf node value is : 8``
``````// Include namespace system
using System;
/*
Csharp program for
Find the minimum leaf node in a 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;
}
}
public class BinaryTree
{
public TreeNode root;
public TreeNode result;
public BinaryTree()
{
// Set initial value
this.root = null;
this.result = null;
}
// Find the value of min leaf node recursively
public void findMinLeaf(TreeNode node)
{
if (node == null)
{
return;
}
if (node.left == null && node.right == null)
{
if (this.result == null)
{
// First leaf node
this.result = node;
}
else if (this.result.data > node.data)
{
this.result = node;
}
}
// Visit left and right subtree
this.findMinLeaf(node.left);
this.findMinLeaf(node.right);
}
// Handles the request of finding the
// minimum leaf node in binary tree.
public void minLeafNode()
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMinLeaf(this.root);
if (this.result != null)
{
Console.Write(" Minimum leaf node value is : " +
(this.result.data) + "\n");
}
else
{
Console.Write(" Minimum left is : None \n");
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
tree.root = new TreeNode(4);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(21);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(1);
tree.root.left.left.left.left = new TreeNode(31);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(15);
tree.root.right.right.left.right = new TreeNode(13);
tree.minLeafNode();
}
}``````

#### Output

`` Minimum leaf node value is : 8``
``````package main
import "fmt"
/*
Go program for
Find the minimum leaf node in a binary tree
*/
// Binary Tree node
type TreeNode struct {
data int
left * TreeNode
right * TreeNode
}
func getTreeNode(data int) * TreeNode {
var me *TreeNode = &TreeNode {}
// Set node value
me.data = data
me.left = nil
me.right = nil
return me
}
type BinaryTree struct {
root * TreeNode
result * TreeNode
}
func getBinaryTree() * BinaryTree {
var me *BinaryTree = &BinaryTree {}
// Set initial value
me.root = nil
me.result = nil
return me
}
// Find the value of min leaf node recursively
func(this *BinaryTree) findMinLeaf(node * TreeNode) {
if node == nil {
return
}
if node.left == nil && node.right == nil {
if this.result == nil {
// First leaf node
this.result = node
} else if this.result.data > node.data {
this.result = node
}
}
// Visit left and right subtree
this.findMinLeaf(node.left)
this.findMinLeaf(node.right)
}
// Handles the request of finding the minimum leaf node in binary tree.
func(this BinaryTree) minLeafNode() {
if this.root == nil {
return
}
this.result = nil
// Find node value
this.findMinLeaf(this.root)
if this.result != nil {
fmt.Print(" Minimum leaf node value is : ", (this.result.data), "\n")
} else {
fmt.Print(" Minimum left is : None \n")
}
}
func main() {
var tree * BinaryTree = getBinaryTree()
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
tree.root = getTreeNode(4)
tree.root.left = getTreeNode(-4)
tree.root.left.right = getTreeNode(3)
tree.root.left.right.left = getTreeNode(10)
tree.root.left.right.left.left = getTreeNode(9)
tree.root.left.right.left.right = getTreeNode(21)
tree.root.left.right.right = getTreeNode(8)
tree.root.left.left = getTreeNode(2)
tree.root.left.left.left = getTreeNode(1)
tree.root.left.left.left.left = getTreeNode(31)
tree.root.right = getTreeNode(7)
tree.root.right.right = getTreeNode(12)
tree.root.right.right.left = getTreeNode(15)
tree.root.right.right.left.right = getTreeNode(13)
tree.minLeafNode()
}``````

#### Output

`` Minimum leaf node value is : 8``
``````<?php
/*
Php program for
Find the minimum leaf node in a binary tree
*/
// Binary Tree node
class TreeNode
{
public \$data;
public \$left;
public \$right;
public	function __construct(\$data)
{
// Set node value
\$this->data = \$data;
\$this->left = NULL;
\$this->right = NULL;
}
}
class BinaryTree
{
public \$root;
public \$result;
public	function __construct()
{
\$this->root = NULL;
\$this->result = NULL;
}
// Find the value of min leaf node recursively
public	function findMinLeaf(\$node)
{
if (\$node == NULL)
{
return;
}
if (\$node->left == NULL && \$node->right == NULL)
{
if (\$this->result == NULL)
{
// First leaf node
\$this->result = \$node;
}
else if (\$this->result->data > \$node->data)
{
\$this->result = \$node;
}
}
// Visit left and right subtree
\$this->findMinLeaf(\$node->left);
\$this->findMinLeaf(\$node->right);
}
// Handles the request of finding the
// minimum leaf node in binary tree.
public	function minLeafNode()
{
if (\$this->root == NULL)
{
return;
}
\$this->result = NULL;
// Find node value
\$this->findMinLeaf(\$this->root);
if (\$this->result != NULL)
{
echo(" Minimum leaf node value is : ".
(\$this->result->data).
"\n");
}
else
{
echo(" Minimum left is : None \n");
}
}
}

function main()
{
\$tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
\$tree->root = new TreeNode(4);
\$tree->root->left = new TreeNode(-4);
\$tree->root->left->right = new TreeNode(3);
\$tree->root->left->right->left = new TreeNode(10);
\$tree->root->left->right->left->left = new TreeNode(9);
\$tree->root->left->right->left->right = new TreeNode(21);
\$tree->root->left->right->right = new TreeNode(8);
\$tree->root->left->left = new TreeNode(2);
\$tree->root->left->left->left = new TreeNode(1);
\$tree->root->left->left->left->left = new TreeNode(31);
\$tree->root->right = new TreeNode(7);
\$tree->root->right->right = new TreeNode(12);
\$tree->root->right->right->left = new TreeNode(15);
\$tree->root->right->right->left->right = new TreeNode(13);
\$tree->minLeafNode();
}
main();``````

#### Output

`` Minimum leaf node value is : 8``
``````/*
Node JS program for
Find the minimum leaf node in a binary tree
*/
// Binary Tree node
class TreeNode
{
constructor(data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
this.root = null;
this.result = null;
}
// Find the value of min leaf node recursively
findMinLeaf(node)
{
if (node == null)
{
return;
}
if (node.left == null && node.right == null)
{
if (this.result == null)
{
// First leaf node
this.result = node;
}
else if (this.result.data > node.data)
{
this.result = node;
}
}
// Visit left and right subtree
this.findMinLeaf(node.left);
this.findMinLeaf(node.right);
}
// Handles the request of finding the
// minimum leaf node in binary tree.
minLeafNode()
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMinLeaf(this.root);
if (this.result != null)
{
process.stdout.write(" Minimum leaf node value is : " +
(this.result.data) + "\n");
}
else
{
process.stdout.write(" Minimum left is : None \n");
}
}
}

function main()
{
var tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
tree.root = new TreeNode(4);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(21);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(1);
tree.root.left.left.left.left = new TreeNode(31);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(15);
tree.root.right.right.left.right = new TreeNode(13);
tree.minLeafNode();
}
main();``````

#### Output

`` Minimum leaf node value is : 8``
``````#  Python 3 program for
#  Find the minimum leaf node in a binary tree

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

class BinaryTree :
def __init__(self) :
self.root = None
self.result = None

#  Find the value of min leaf node recursively
def findMinLeaf(self, node) :
if (node == None) :
return

if (node.left == None and node.right == None) :
if (self.result == None) :
#  First leaf node
self.result = node
elif (self.result.data > node.data) :
self.result = node

#  Visit left and right subtree
self.findMinLeaf(node.left)
self.findMinLeaf(node.right)

#  Handles the request of finding the
#  minimum leaf node in binary tree.
def minLeafNode(self) :
if (self.root == None) :
return

self.result = None
#  Find node value
self.findMinLeaf(self.root)
if (self.result != None) :
print(" Minimum leaf node value is : ", (self.result.data) )
else :
print(" Minimum left is : None ")

def main() :
tree = BinaryTree()
#              4
#            /   \
#          -4     7
#          / \     \
#         2   3     12
#        /   / \    /
#       1   10  8  15
#      /    / \     \
#    31   9   21    13
#    -----------------
#    Binary tree
tree.root = TreeNode(4)
tree.root.left = TreeNode(-4)
tree.root.left.right = TreeNode(3)
tree.root.left.right.left = TreeNode(10)
tree.root.left.right.left.left = TreeNode(9)
tree.root.left.right.left.right = TreeNode(21)
tree.root.left.right.right = TreeNode(8)
tree.root.left.left = TreeNode(2)
tree.root.left.left.left = TreeNode(1)
tree.root.left.left.left.left = TreeNode(31)
tree.root.right = TreeNode(7)
tree.root.right.right = TreeNode(12)
tree.root.right.right.left = TreeNode(15)
tree.root.right.right.left.right = TreeNode(13)
tree.minLeafNode()

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

#### Output

`` Minimum leaf node value is :  8``
``````#  Ruby program for
#  Find the minimum leaf node in a 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

class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_accessor :root, :result
def initialize()
self.root = nil
self.result = nil
end

#  Find the value of min leaf node recursively
def findMinLeaf(node)
if (node == nil)
return
end

if (node.left == nil && node.right == nil)
if (self.result == nil)
#  First leaf node
self.result = node
elsif (self.result.data > node.data)
self.result = node
end

end

#  Visit left and right subtree
self.findMinLeaf(node.left)
self.findMinLeaf(node.right)
end

#  Handles the request of finding the
#  minimum leaf node in binary tree.
def minLeafNode()
if (self.root == nil)
return
end

self.result = nil
#  Find node value
self.findMinLeaf(self.root)
if (self.result != nil)
print(" Minimum leaf node value is : ",
(self.result.data) ,"\n")
else

print(" Minimum left is : None \n")
end

end

end

def main()
tree = BinaryTree.new()
#              4
#            /   \
#          -4     7
#          / \     \
#         2   3     12
#        /   / \    /
#       1   10  8  15
#      /    / \     \
#    31   9   21    13
#    -----------------
#    Binary tree
tree.root = TreeNode.new(4)
tree.root.left = TreeNode.new(-4)
tree.root.left.right = TreeNode.new(3)
tree.root.left.right.left = TreeNode.new(10)
tree.root.left.right.left.left = TreeNode.new(9)
tree.root.left.right.left.right = TreeNode.new(21)
tree.root.left.right.right = TreeNode.new(8)
tree.root.left.left = TreeNode.new(2)
tree.root.left.left.left = TreeNode.new(1)
tree.root.left.left.left.left = TreeNode.new(31)
tree.root.right = TreeNode.new(7)
tree.root.right.right = TreeNode.new(12)
tree.root.right.right.left = TreeNode.new(15)
tree.root.right.right.left.right = TreeNode.new(13)
tree.minLeafNode()
end

main()``````

#### Output

`````` Minimum leaf node value is : 8
``````
``````/*
Scala program for
Find the minimum leaf node in a binary tree
*/
// Binary Tree node
class TreeNode(var data: Int,
var left: TreeNode,
var right: TreeNode)
{
def this(data: Int)
{
// Set node value
this(data,null,null);
}
}
class BinaryTree(var root: TreeNode,
var result: TreeNode)
{
def this()
{
this(null,null);
}
// Find the value of min leaf node recursively
def findMinLeaf(node: TreeNode): Unit = {
if (node == null)
{
return;
}
if (node.left == null && node.right == null)
{
if (this.result == null)
{
// First leaf node
this.result = node;
}
else if (this.result.data > node.data)
{
this.result = node;
}
}
// Visit left and right subtree
findMinLeaf(node.left);
findMinLeaf(node.right);
}
// Handles the request of finding the minimum leaf node in binary tree.
def minLeafNode(): Unit = {
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMinLeaf(this.root);
if (result != null)
{
print(" Minimum leaf node value is : " + (this.result.data) + "\n");
}
else
{
print(" Minimum left is : None \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
tree.root = new TreeNode(4);
tree.root.left = new TreeNode(-4);
tree.root.left.right = new TreeNode(3);
tree.root.left.right.left = new TreeNode(10);
tree.root.left.right.left.left = new TreeNode(9);
tree.root.left.right.left.right = new TreeNode(21);
tree.root.left.right.right = new TreeNode(8);
tree.root.left.left = new TreeNode(2);
tree.root.left.left.left = new TreeNode(1);
tree.root.left.left.left.left = new TreeNode(31);
tree.root.right = new TreeNode(7);
tree.root.right.right = new TreeNode(12);
tree.root.right.right.left = new TreeNode(15);
tree.root.right.right.left.right = new TreeNode(13);
tree.minLeafNode();
}
}``````

#### Output

`` Minimum leaf node value is : 8``
``````/*
Swift 4 program for
Find the minimum leaf node in a 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;
}
}
class BinaryTree
{
var root: TreeNode? ;
var result: TreeNode? ;
init()
{
self.root = nil;
self.result = nil;
}
// Find the value of min leaf node recursively
func findMinLeaf(_ node: TreeNode? )
{
if (node == nil)
{
return;
}
if (node!.left == nil && node!.right == nil)
{
if (self.result == nil)
{
// First leaf node
self.result = node;
}
else if (self.result!.data > node!.data)
{
self.result = node;
}
}
// Visit left and right subtree
self.findMinLeaf(node!.left);
self.findMinLeaf(node!.right);
}
// Handles the request of finding the
// minimum leaf node in binary tree.
func minLeafNode()
{
if (self.root == nil)
{
return;
}
self.result = nil;
// Find node value
self.findMinLeaf(self.root);
if (self.result  != nil)
{
print(" Minimum leaf node value is : ",
(self.result!.data) );
}
else
{
print(" Minimum left is : None ");
}
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
tree.root = TreeNode(4);
tree.root!.left = TreeNode(-4);
tree.root!.left!.right = TreeNode(3);
tree.root!.left!.right!.left = TreeNode(10);
tree.root!.left!.right!.left!.left = TreeNode(9);
tree.root!.left!.right!.left!.right = TreeNode(21);
tree.root!.left!.right!.right = TreeNode(8);
tree.root!.left!.left = TreeNode(2);
tree.root!.left!.left!.left = TreeNode(1);
tree.root!.left!.left!.left!.left = TreeNode(31);
tree.root!.right = TreeNode(7);
tree.root!.right!.right = TreeNode(12);
tree.root!.right!.right!.left = TreeNode(15);
tree.root!.right!.right!.left!.right = TreeNode(13);
tree.minLeafNode();
}
main();``````

#### Output

`` Minimum leaf node value is :  8``
``````/*
Kotlin program for
Find the minimum leaf node in a 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;
}
}
class BinaryTree
{
var root: TreeNode ? ;
var result: TreeNode ? ;
constructor()
{
this.root = null;
this.result = null;
}
// Find the value of min leaf node recursively
fun findMinLeaf(node: TreeNode ? ): Unit
{
if (node == null)
{
return;
}
if (node.left == null && node.right == null)
{
if (this.result == null)
{
// First leaf node
this.result = node;
}
else if (this.result!!.data > node.data)
{
this.result = node;
}
}
// Visit left and right subtree
this.findMinLeaf(node.left);
this.findMinLeaf(node.right);
}
// Handles the request of finding the minimum leaf node in binary tree.
fun minLeafNode(): Unit
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMinLeaf(this.root);
if (this.result != null)
{
print(" Minimum leaf node value is : " +
(this.result!!.data) + "\n");
}
else
{
print(" Minimum left is : None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: BinaryTree = BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
31   9   21    13
-----------------
Binary tree
*/
tree.root = TreeNode(4);
tree.root?.left = TreeNode(-4);
tree.root?.left?.right = TreeNode(3);
tree.root?.left?.right?.left = TreeNode(10);
tree.root?.left?.right?.left?.left = TreeNode(9);
tree.root?.left?.right?.left?.right = TreeNode(21);
tree.root?.left?.right?.right = TreeNode(8);
tree.root?.left?.left = TreeNode(2);
tree.root?.left?.left?.left = TreeNode(1);
tree.root?.left?.left?.left?.left = TreeNode(31);
tree.root?.right = TreeNode(7);
tree.root?.right?.right = TreeNode(12);
tree.root?.right?.right?.left = TreeNode(15);
tree.root?.right?.right?.left?.right = TreeNode(13);
tree.minLeafNode();
}``````

#### Output

`` Minimum leaf node value is : 8``

## 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