Find maximum among all left nodes in Binary Tree
Given a binary tree that contains nodes of integer values. Our goal is to find the largest left node among all the node which is exist in given tree. For example.

In above tree the left largest node is 11. In case tree is empty or tree have the no right child then result is None. Here given code implementation process.
// Java program for
// Find maximum among all left nodes in Binary Tree
class TreeNode
{
// Data value
public int data;
// Indicates left and right subtree
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public TreeNode root;
public TreeNode result;
public BinaryTree()
{
this.root = null;
this.result = null;
}
public void findMaxLeftNode(TreeNode node)
{
if (node != null)
{
if (node.left != null)
{
if (this.result == null ||
this.result.data < node.left.data)
{
// Get new max left child node
this.result = node.left;
}
}
// Visit left subtree
findMaxLeftNode(node.left);
// Visit right subtree
findMaxLeftNode(node.right);
}
}
public void maxLeftChild()
{
this.result = null;
this.findMaxLeftNode(this.root);
if (this.result == null)
{
// When no left child node
System.out.println("None");
}
else
{
// Display calculated result
System.out.println(this.result.data);
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree.maxLeftChild();
}
}
Output
11
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find maximum among all left nodes in Binary Tree
class TreeNode
{
public:
// Data value
int data;
// Indicates left and right subtree
TreeNode *left;
TreeNode *right;
TreeNode(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree
{
public: TreeNode *root;
TreeNode *result;
BinaryTree()
{
this->root = NULL;
this->result = NULL;
}
void findMaxLeftNode(TreeNode *node)
{
if (node != NULL)
{
if (node->left != NULL)
{
if (this->result == NULL ||
this->result->data < node->left->data)
{
// Get new max left child node
this->result = node->left;
}
}
// Visit left subtree
this->findMaxLeftNode(node->left);
// Visit right subtree
this->findMaxLeftNode(node->right);
}
}
void maxLeftChild()
{
this->result = NULL;
this->findMaxLeftNode(this->root);
if (this->result == NULL)
{
// When no left child node
cout << "None" << endl;
}
else
{
// Display calculated result
cout << this->result->data << endl;
}
}
};
int main()
{
BinaryTree *tree = new BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree->root = new TreeNode(1);
tree->root->left = new TreeNode(11);
tree->root->right = new TreeNode(3);
tree->root->right->right = new TreeNode(6);
tree->root->right->right->right = new TreeNode(-4);
tree->root->right->left = new TreeNode(5);
tree->root->left->left = new TreeNode(4);
tree->root->left->left->left = new TreeNode(8);
tree->root->left->left->right = new TreeNode(7);
tree->root->left->left->right->left = new TreeNode(-2);
tree->root->left->left->right->right = new TreeNode(9);
tree->root->right->left->right = new TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree->maxLeftChild();
return 0;
}
Output
11
package main
import "fmt"
// Go program for
// Find maximum among all left nodes in Binary Tree
type TreeNode struct {
// Data value
data int
// Indicates left and right subtree
left * TreeNode
right * TreeNode
}
func getTreeNode(data int) * TreeNode {
var me *TreeNode = &TreeNode {}
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 {}
me.root = nil
me.result = nil
return me
}
func(this *BinaryTree) findMaxLeftNode(node * TreeNode) {
if node != nil {
if node.left != nil {
if this.result == nil || this.result.data < node.left.data {
// Get new max left child node
this.result = node.left
}
}
// Visit left subtree
this.findMaxLeftNode(node.left)
// Visit right subtree
this.findMaxLeftNode(node.right)
}
}
func(this BinaryTree) maxLeftChild() {
this.result = nil
this.findMaxLeftNode(this.root)
if this.result == nil {
// When no left child node
fmt.Println("None")
} else {
// Display calculated result
fmt.Println(this.result.data)
}
}
func main() {
var tree * BinaryTree = getBinaryTree()
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = getTreeNode(1)
tree.root.left = getTreeNode(11)
tree.root.right = getTreeNode(3)
tree.root.right.right = getTreeNode(6)
tree.root.right.right.right = getTreeNode(-4)
tree.root.right.left = getTreeNode(5)
tree.root.left.left = getTreeNode(4)
tree.root.left.left.left = getTreeNode(8)
tree.root.left.left.right = getTreeNode(7)
tree.root.left.left.right.left = getTreeNode(-2)
tree.root.left.left.right.right = getTreeNode(9)
tree.root.right.left.right = getTreeNode(12)
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree.maxLeftChild()
}
Output
11
// Include namespace system
using System;
// Csharp program for
// Find maximum among all left nodes in Binary Tree
public class TreeNode
{
// Data value
public int data;
// Indicates left and right subtree
public TreeNode left;
public TreeNode right;
public TreeNode(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public TreeNode root;
public TreeNode result;
public BinaryTree()
{
this.root = null;
this.result = null;
}
public void findMaxLeftNode(TreeNode node)
{
if (node != null)
{
if (node.left != null)
{
if (this.result == null ||
this.result.data < node.left.data)
{
// Get new max left child node
this.result = node.left;
}
}
// Visit left subtree
this.findMaxLeftNode(node.left);
// Visit right subtree
this.findMaxLeftNode(node.right);
}
}
public void maxLeftChild()
{
this.result = null;
this.findMaxLeftNode(this.root);
if (this.result == null)
{
// When no left child node
Console.WriteLine("None");
}
else
{
// Display calculated result
Console.WriteLine(this.result.data);
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree.maxLeftChild();
}
}
Output
11
<?php
// Php program for
// Find maximum among all left nodes in Binary Tree
class TreeNode
{
// Data value
public $data;
// Indicates left and right subtree
public $left;
public $right;
public function __construct($data)
{
$this->data = $data;
$this->left = NULL;
$this->right = NULL;
}
}
class BinaryTree
{
public $root;
public $result;
public function __construct()
{
$this->root = NULL;
$this->result = NULL;
}
public function findMaxLeftNode($node)
{
if ($node != NULL)
{
if ($node->left != NULL)
{
if ($this->result == NULL ||
$this->result->data < $node->left->data)
{
// Get new max left child node
$this->result = $node->left;
}
}
// Visit left subtree
$this->findMaxLeftNode($node->left);
// Visit right subtree
$this->findMaxLeftNode($node->right);
}
}
public function maxLeftChild()
{
$this->result = NULL;
$this->findMaxLeftNode($this->root);
if ($this->result == NULL)
{
// When no left child node
echo("None\n");
}
else
{
// Display calculated result
echo($this->result->data.
"\n");
}
}
}
function main()
{
$tree = new BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
$tree->root = new TreeNode(1);
$tree->root->left = new TreeNode(11);
$tree->root->right = new TreeNode(3);
$tree->root->right->right = new TreeNode(6);
$tree->root->right->right->right = new TreeNode(-4);
$tree->root->right->left = new TreeNode(5);
$tree->root->left->left = new TreeNode(4);
$tree->root->left->left->left = new TreeNode(8);
$tree->root->left->left->right = new TreeNode(7);
$tree->root->left->left->right->left = new TreeNode(-2);
$tree->root->left->left->right->right = new TreeNode(9);
$tree->root->right->left->right = new TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
$tree->maxLeftChild();
}
main();
Output
11
// Node JS program for
// Find maximum among all left nodes in Binary Tree
class TreeNode
{
constructor(data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
this.root = null;
this.result = null;
}
findMaxLeftNode(node)
{
if (node != null)
{
if (node.left != null)
{
if (this.result == null ||
this.result.data < node.left.data)
{
// Get new max left child node
this.result = node.left;
}
}
// Visit left subtree
this.findMaxLeftNode(node.left);
// Visit right subtree
this.findMaxLeftNode(node.right);
}
}
maxLeftChild()
{
this.result = null;
this.findMaxLeftNode(this.root);
if (this.result == null)
{
// When no left child node
console.log("None");
}
else
{
// Display calculated result
console.log(this.result.data);
}
}
}
function main()
{
var tree = new BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree.maxLeftChild();
}
main();
Output
11
# Python 3 program for
# Find maximum among all left nodes in Binary Tree
class TreeNode :
# Data value
# Indicates left and right subtree
def __init__(self, data) :
self.data = data
self.left = None
self.right = None
class BinaryTree :
def __init__(self) :
self.root = None
self.result = None
def findMaxLeftNode(self, node) :
if (node != None) :
if (node.left != None) :
if (self.result == None or
self.result.data < node.left.data) :
# Get new max left child node
self.result = node.left
# Visit left subtree
self.findMaxLeftNode(node.left)
# Visit right subtree
self.findMaxLeftNode(node.right)
def maxLeftChild(self) :
self.result = None
self.findMaxLeftNode(self.root)
if (self.result == None) :
# When no left child node
print("None")
else :
# Display calculated result
print(self.result.data)
def main() :
tree = BinaryTree()
# Binary Tree
# ---------------
# 1
# / \
# 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \
# -2 9
# Add Binary tree nodes
tree.root = TreeNode(1)
tree.root.left = TreeNode(11)
tree.root.right = TreeNode(3)
tree.root.right.right = TreeNode(6)
tree.root.right.right.right = TreeNode(-4)
tree.root.right.left = TreeNode(5)
tree.root.left.left = TreeNode(4)
tree.root.left.left.left = TreeNode(8)
tree.root.left.left.right = TreeNode(7)
tree.root.left.left.right.left = TreeNode(-2)
tree.root.left.left.right.right = TreeNode(9)
tree.root.right.left.right = TreeNode(12)
# 1
# / \
# → 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \
# -2 9
# --------------------
# Max left node is : 11
tree.maxLeftChild()
if __name__ == "__main__": main()
Output
11
# Ruby program for
# Find maximum among all left nodes in Binary Tree
class TreeNode
# Define the accessor and reader of class TreeNode
attr_reader :data, :left, :right
attr_accessor :data, :left, :right
# Data value
# Indicates left and right subtree
def initialize(data)
self.data = data
self.left = nil
self.right = nil
end
end
class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_reader :root, :result
attr_accessor :root, :result
def initialize()
self.root = nil
self.result = nil
end
def findMaxLeftNode(node)
if (node != nil)
if (node.left != nil)
if (self.result == nil ||
self.result.data < node.left.data)
# Get new max left child node
self.result = node.left
end
end
# Visit left subtree
self.findMaxLeftNode(node.left)
# Visit right subtree
self.findMaxLeftNode(node.right)
end
end
def maxLeftChild()
self.result = nil
self.findMaxLeftNode(self.root)
if (self.result == nil)
# When no left child node
print("None", "\n")
else
# Display calculated result
print(self.result.data, "\n")
end
end
end
def main()
tree = BinaryTree.new()
# Binary Tree
# ---------------
# 1
# / \
# 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \
# -2 9
# Add Binary tree nodes
tree.root = TreeNode.new(1)
tree.root.left = TreeNode.new(11)
tree.root.right = TreeNode.new(3)
tree.root.right.right = TreeNode.new(6)
tree.root.right.right.right = TreeNode.new(-4)
tree.root.right.left = TreeNode.new(5)
tree.root.left.left = TreeNode.new(4)
tree.root.left.left.left = TreeNode.new(8)
tree.root.left.left.right = TreeNode.new(7)
tree.root.left.left.right.left = TreeNode.new(-2)
tree.root.left.left.right.right = TreeNode.new(9)
tree.root.right.left.right = TreeNode.new(12)
# 1
# / \
# → 11 3
# / / \
# 4 5 6
# / \ \ \
# 8 7 12 -4
# / \
# -2 9
# --------------------
# Max left node is : 11
tree.maxLeftChild()
end
main()
Output
11
// Scala program for
// Find maximum among all left nodes in Binary Tree
class TreeNode(
// Data value
var data: Int,
// Indicates left and right subtree
var left: TreeNode,
var right: TreeNode)
{
def this(data: Int)
{
this(data, null, null);
}
}
class BinaryTree(var root: TreeNode,
var result: TreeNode)
{
def this()
{
this(null, null);
}
def findMaxLeftNode(node: TreeNode): Unit = {
if (node != null)
{
if (node.left != null)
{
if (this.result == null ||
this.result.data < node.left.data)
{
// Get new max left child node
this.result = node.left;
}
}
// Visit left subtree
findMaxLeftNode(node.left);
// Visit right subtree
findMaxLeftNode(node.right);
}
}
def maxLeftChild(): Unit = {
this.result = null;
this.findMaxLeftNode(this.root);
if (this.result == null)
{
// When no left child node
println("None");
}
else
{
// Display calculated result
println(this.result.data);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var tree: BinaryTree = new BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = new TreeNode(1);
tree.root.left = new TreeNode(11);
tree.root.right = new TreeNode(3);
tree.root.right.right = new TreeNode(6);
tree.root.right.right.right = new TreeNode(-4);
tree.root.right.left = new TreeNode(5);
tree.root.left.left = new TreeNode(4);
tree.root.left.left.left = new TreeNode(8);
tree.root.left.left.right = new TreeNode(7);
tree.root.left.left.right.left = new TreeNode(-2);
tree.root.left.left.right.right = new TreeNode(9);
tree.root.right.left.right = new TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree.maxLeftChild();
}
}
Output
11
// Swift 4 program for
// Find maximum among all left nodes in Binary Tree
class TreeNode
{
// Data value
var data: Int;
// Indicates left and right subtree
var left: TreeNode? ;
var right: TreeNode? ;
init(_ data: Int)
{
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinaryTree
{
var root: TreeNode? ;
var result: TreeNode? ;
init()
{
self.root = nil;
self.result = nil;
}
func findMaxLeftNode(_ node: TreeNode? )
{
if (node != nil)
{
if (node!.left != nil)
{
if (self.result == nil ||
self.result!.data < node!.left!.data)
{
// Get new max left child node
self.result = node!.left;
}
}
// Visit left subtree
self.findMaxLeftNode(node!.left);
// Visit right subtree
self.findMaxLeftNode(node!.right);
}
}
func maxLeftChild()
{
self.result = nil;
self.findMaxLeftNode(self.root);
if (self.result == nil)
{
// When no left child node
print("None");
}
else
{
// Display calculated result
print(self.result!.data);
}
}
}
func main()
{
let tree: BinaryTree = BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = TreeNode(1);
tree.root!.left = TreeNode(11);
tree.root!.right = TreeNode(3);
tree.root!.right!.right = TreeNode(6);
tree.root!.right!.right!.right = TreeNode(-4);
tree.root!.right!.left = TreeNode(5);
tree.root!.left!.left = TreeNode(4);
tree.root!.left!.left!.left = TreeNode(8);
tree.root!.left!.left!.right = TreeNode(7);
tree.root!.left!.left!.right!.left = TreeNode(-2);
tree.root!.left!.left!.right!.right = TreeNode(9);
tree.root!.right!.left!.right = TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree.maxLeftChild();
}
main();
Output
11
// Kotlin program for
// Find maximum among all left nodes in Binary Tree
class TreeNode
{
// Data value
var data: Int;
// Indicates left and right subtree
var left: TreeNode ? ;
var right: TreeNode ? ;
constructor(data: Int)
{
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
var root: TreeNode ? ;
var result: TreeNode ? ;
constructor()
{
this.root = null;
this.result = null;
}
fun findMaxLeftNode(node: TreeNode ? ): Unit
{
if (node != null)
{
if (node.left != null)
{
if (this.result == null ||
this.result!!.data < node.left!!.data)
{
// Get new max left child node
this.result = node.left;
}
}
// Visit left subtree
this.findMaxLeftNode(node.left);
// Visit right subtree
this.findMaxLeftNode(node.right);
}
}
fun maxLeftChild(): Unit
{
this.result = null;
this.findMaxLeftNode(this.root);
if (this.result == null)
{
// When no left child node
println("None");
}
else
{
// Display calculated result
println(this.result?.data);
}
}
}
fun main(args: Array < String > ): Unit
{
val tree: BinaryTree = BinaryTree();
/*
Binary Tree
---------------
1
/ \
11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
*/
// Add Binary tree nodes
tree.root = TreeNode(1);
tree.root?.left = TreeNode(11);
tree.root?.right = TreeNode(3);
tree.root?.right?.right = TreeNode(6);
tree.root?.right?.right?.right = TreeNode(-4);
tree.root?.right?.left = TreeNode(5);
tree.root?.left?.left = TreeNode(4);
tree.root?.left?.left?.left = TreeNode(8);
tree.root?.left?.left?.right = TreeNode(7);
tree.root?.left?.left?.right?.left = TreeNode(-2);
tree.root?.left?.left?.right?.right = TreeNode(9);
tree.root?.right?.left?.right = TreeNode(12);
/*
1
/ \
→ 11 3
/ / \
4 5 6
/ \ \ \
8 7 12 -4
/ \
-2 9
--------------------
Max left node is : 11
*/
tree.maxLeftChild();
}
Output
11
The time complexity in the above program is O(n), And this problem of solved by using of recursion. Your task is to solve this problem using iteration.
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