# Find the maximum leaf node in a binary tree

Here given code implementation process.

``````// C program for
// Find the maximum 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 max leaf node recursively
void findMaxLeaf(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
findMaxLeaf(node->left, result);
findMaxLeaf(node->right, result);
}
// Handles the request of finding the maximum leaf node in binary tree.
void maxLeafNode(struct TreeNode *root)
{
if (root == NULL)
{
return;
}
struct TreeNode *result = NULL;
// Find node value
findMaxLeaf(root, & result);
if (result != NULL)
{
printf(" Maximum leaf node value is : %d\n", result->data);
}
else
{
printf(" Max 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
/    / \     \
3    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(3);
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);
maxLeafNode(tree->root);
return 0;
}``````

#### Output

`` Maximum leaf node value is : 21``
``````/*
Java program for
Find the maximum 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 tree root to null
this.root = null;
this.result = null;
}
// Find the value of max leaf node recursively
public void findMaxLeaf(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
findMaxLeaf(node.left);
findMaxLeaf(node.right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
public void maxLeafNode()
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMaxLeaf(this.root);
if (this.result != null)
{
System.out.print(" Maximum leaf node value is : " +
this.result.data + "\n");
}
else
{
System.out.print(" Max left is : None \n");
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3);
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.maxLeafNode();
}
}``````

#### Output

`` Maximum leaf node value is : 21``
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program for
Find the maximum 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 max leaf node recursively
void findMaxLeaf(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->findMaxLeaf(node->left);
this->findMaxLeaf(node->right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
void maxLeafNode()
{
if (this->root == NULL)
{
return;
}
this->result = NULL;
// Find node value
this->findMaxLeaf(this->root);
if (this->result != NULL)
{
cout << " Maximum leaf node value is : "
<< this->result->data << "\n";
}
else
{
cout << " Max left is : None \n";
}
}
};
int main()
{
BinaryTree *tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3);
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->maxLeafNode();
return 0;
}``````

#### Output

`` Maximum leaf node value is : 21``
``````// Include namespace system
using System;
/*
Csharp program for
Find the maximum 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 tree root to null
this.root = null;
this.result = null;
}
// Find the value of max leaf node recursively
public void findMaxLeaf(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.findMaxLeaf(node.left);
this.findMaxLeaf(node.right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
public void maxLeafNode()
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMaxLeaf(this.root);
if (this.result != null)
{
Console.WriteLine(" Maximum leaf node value is : " +
this.result.data );
}
else
{
Console.Write(" Max left is : None \n");
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3);
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.maxLeafNode();
}
}``````

#### Output

`` Maximum leaf node value is : 21``
``````package main
import "fmt"
/*
Go program for
Find the maximum 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 tree root to null
me.root = nil
me.result = nil
return me
}
// Find the value of max leaf node recursively
func(this *BinaryTree) findMaxLeaf(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.findMaxLeaf(node.left)
this.findMaxLeaf(node.right)
}
// Handles the request of finding the
// maximum leaf node in binary tree.
func(this BinaryTree) maxLeafNode() {
if this.root == nil {
return
}
this.result = nil
// Find node value
this.findMaxLeaf(this.root)
if this.result != nil {
fmt.Print(" Maximum leaf node value is : ", this.result.data, "\n")
} else {
fmt.Print(" Max left is : None \n")
}
}
func main() {
var tree * BinaryTree = getBinaryTree()
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3)
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.maxLeafNode()
}``````

#### Output

`` Maximum leaf node value is : 21``
``````<?php
/*
Php program for
Find the maximum 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 max leaf node recursively
public	function findMaxLeaf(\$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->findMaxLeaf(\$node->left);
\$this->findMaxLeaf(\$node->right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
public	function maxLeafNode()
{
if (\$this->root == NULL)
{
return;
}
\$this->result = NULL;
// Find node value
\$this->findMaxLeaf(\$this->root);
if (\$this->result != NULL)
{
echo(" Maximum leaf node value is : ".\$this->result->data."\n");
}
else
{
echo(" Max left is : None \n");
}
}
}

function main()
{
\$tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3);
\$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->maxLeafNode();
}
main();``````

#### Output

`` Maximum leaf node value is : 21``
``````/*
Node JS program for
Find the maximum 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 max leaf node recursively
findMaxLeaf(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.findMaxLeaf(node.left);
this.findMaxLeaf(node.right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
maxLeafNode()
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMaxLeaf(this.root);
if (this.result != null)
{
process.stdout.write(" Maximum leaf node value is : " +
this.result.data + "\n");
}
else
{
process.stdout.write(" Max left is : None \n");
}
}
}

function main()
{
var tree = new BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3);
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.maxLeafNode();
}
main();``````

#### Output

`` Maximum leaf node value is : 21``
``````#  Python 3 program for
#  Find the maximum 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 max leaf node recursively
def findMaxLeaf(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.findMaxLeaf(node.left)
self.findMaxLeaf(node.right)

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

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

def main() :
tree = BinaryTree()
#          4
#        /   \
#      -4     7
#      / \     \
#     2   3     12
#    /   / \    /
#   1   10  8  15
#  /    / \     \
# 3    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(3)
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.maxLeafNode()

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

#### Output

`` Maximum leaf node value is :  21``
``````#  Ruby program for
#  Find the maximum 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 max leaf node recursively
def findMaxLeaf(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.findMaxLeaf(node.left)
self.findMaxLeaf(node.right)
end

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

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

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

end

end

def main()
tree = BinaryTree.new()
#         4
#       /   \
#     -4     7
#     / \     \
#    2   3     12
#   /   / \    /
#  1   10  8  15
# /    / \     \
# 3    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(3)
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.maxLeafNode()
end

main()``````

#### Output

`````` Maximum leaf node value is : 21
``````
``````/*
Scala program for
Find the maximum 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 max leaf node recursively
def findMaxLeaf(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
findMaxLeaf(node.left);
findMaxLeaf(node.right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
def maxLeafNode(): Unit = {
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMaxLeaf(this.root);
if (this.result != null)
{
print(" Maximum leaf node value is : " +
this.result.data + "\n");
}
else
{
print(" Max 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
/    / \     \
3    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(3);
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.maxLeafNode();
}
}``````

#### Output

`` Maximum leaf node value is : 21``
``````/*
Swift 4 program for
Find the maximum 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 max leaf node recursively
func findMaxLeaf(_ 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.findMaxLeaf(node!.left);
self.findMaxLeaf(node!.right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
func maxLeafNode()
{
if (self.root == nil)
{
return;
}
self.result = nil;
// Find node value
self.findMaxLeaf(self.root);
if (self.result  != nil)
{
print(" Maximum leaf node value is : ", self.result!.data );
}
else
{
print(" Max left is : None ");
}
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3);
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.maxLeafNode();
}
main();``````

#### Output

`` Maximum leaf node value is :  21``
``````/*
Kotlin program for
Find the maximum 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 max leaf node recursively
fun findMaxLeaf(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.findMaxLeaf(node.left);
this.findMaxLeaf(node.right);
}
// Handles the request of finding the
// maximum leaf node in binary tree.
fun maxLeafNode(): Unit
{
if (this.root == null)
{
return;
}
this.result = null;
// Find node value
this.findMaxLeaf(this.root);
if (this.result != null)
{
print(" Maximum leaf node value is : " +
this.result!!.data + "\n");
}
else
{
print(" Max left is : None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: BinaryTree = BinaryTree();
/*
4
/   \
-4     7
/ \     \
2   3     12
/   / \    /
1   10  8  15
/    / \     \
3    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(3);
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.maxLeafNode();
}``````

#### Output

`` Maximum leaf node value is : 21``

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