Skip to main content

Find min and max element in a binary tree

Minimum and maximum node in binary tree

Here given code implementation process.

// C program for 
// Find min and max element 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 and max node recursively
void findNode(struct TreeNode *node, 
              struct TreeNode **min, 
              struct TreeNode **max)
{
	if (node == NULL)
	{
		return;
	}
	if (( *min)->data > node->data)
	{
		// Change 
		*min = node;
	}
	if (( *max)->data < node->data)
	{
		*max = node;
	}
	// Visit left and right subtree
	findNode(node->left, min, max);
	findNode(node->right, min, max);
}
void minAndMax(struct TreeNode *root)
{
	if (root == NULL)
	{
		return;
	}
	// Assign first root of tree
	struct TreeNode *min = root;
	struct TreeNode *max = root;
	// Find node value
	findNode(root, & min, & max);
	// Display min and max node value
	printf(" Minimum : %d", min->data);
	printf("\n Maximum : %d\n", max->data);
	// Note that in case only one node then both 
	// printing the value of root node
}
int main(int argc, char
	const *argv[])
{
	struct BinaryTree *tree = newTree();
	/*
	             4                            
	           /   \    
	         -4     7    
	         / \     \               
	        2   3     12
	       /   / \    / 
	      1   10  8  15
	     /    /       \
	    3    9         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->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);
	minAndMax(tree->root);
	return 0;
}

Output

 Minimum : -4
 Maximum : 15
/*
  Java program for
  Find min and max element 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 int min;
	public int max;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
		this.min = 0;
		this.max = 0;
	}
	// Find the value of min and max node recursively
	public void findNode(TreeNode node)
	{
		if (node == null)
		{
			return;
		}
		if (this.min > node.data)
		{
			// Change 
			this.min = node.data;
		}
		if (this.max < node.data)
		{
			max = node.data;
		}
		// Visit left and right subtree
		this.findNode(node.left);
		this.findNode(node.right);
	}
	public void minAndMax()
	{
		if (this.root == null)
		{
			return;
		}
		// Assign root value to min max
		this.min = this.root.data;
		this.max = this.root.data;
		// Find node value
		this.findNode(this.root);
		// Display min and max node value
		System.out.print(" Minimum : " + this.min);
		System.out.print("\n Maximum : " +  this.max + "\n");
	}
	public static void main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		         4                            
		       /   \    
		     -4     7    
		     / \     \               
		    2   3     12
		   /   / \    / 
		  1   10  8  15
		 /    /       \
		3    9         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.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.minAndMax();
	}
}

Output

 Minimum : -4
 Maximum : 15
// Include header file
#include <iostream>
using namespace std;
/*
  C++ program for
  Find min and max element 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;
	int min;
	int max;
	BinaryTree()
	{
		this->root = NULL;
		this->min = 0;
		this->max = 0;
	}
	// Find the value of min and max node recursively
	void findNode(TreeNode *node)
	{
		if (node == NULL)
		{
			return;
		}
		if (this->min > node->data)
		{
			// Change
			this->min = node->data;
		}
		if (this->max < node->data)
		{
			this->max = node->data;
		}
		// Visit left and right subtree
		this->findNode(node->left);
		this->findNode(node->right);
	}
	void minAndMax()
	{
		if (this->root == NULL)
		{
			return;
		}
		// Assign root value to min max
		this->min = this->root->data;
		this->max = this->root->data;
		// Find node value
		this->findNode(this->root);
		// Display min and max node value
		cout << " Minimum : " << this->min;
		cout << "\n Maximum : " << this->max << "\n";
	}
};
int main()
{
	BinaryTree *tree = new BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \     \               
	    2   3     12
	   /   / \    / 
	  1   10  8  15
	 /    /       \
	3    9         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->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->minAndMax();
	return 0;
}

Output

 Minimum : -4
 Maximum : 15
// Include namespace system
using System;
/*
  Csharp program for
  Find min and max element 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 int min;
	public int max;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
		this.min = 0;
		this.max = 0;
	}
	// Find the value of min and max node recursively
	public void findNode(TreeNode node)
	{
		if (node == null)
		{
			return;
		}
		if (this.min > node.data)
		{
			// Change
			this.min = node.data;
		}
		if (this.max < node.data)
		{
			this.max = node.data;
		}
		// Visit left and right subtree
		this.findNode(node.left);
		this.findNode(node.right);
	}
	public void minAndMax()
	{
		if (this.root == null)
		{
			return;
		}
		// Assign root value to min max
		this.min = this.root.data;
		this.max = this.root.data;
		// Find node value
		this.findNode(this.root);
		// Display min and max node value
		Console.Write(" Minimum : " + this.min);
		Console.Write("\n Maximum : " + this.max + "\n");
	}
	public static void Main(String[] args)
	{
		BinaryTree tree = new BinaryTree();
		/*
		         4                            
		       /   \    
		     -4     7    
		     / \     \               
		    2   3     12
		   /   / \    / 
		  1   10  8  15
		 /    /       \
		3    9         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.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.minAndMax();
	}
}

Output

 Minimum : -4
 Maximum : 15
package main
import "fmt"
/*
  Go program for
  Find min and max element 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
	min int
	max int
}
func getBinaryTree() * BinaryTree {
	var me *BinaryTree = &BinaryTree {}
	// Set initial tree root to null
	me.root = nil
	me.min = 0
	me.max = 0
	return me
}
// Find the value of min and max node recursively
func(this *BinaryTree) findNode(node * TreeNode) {
	if node == nil {
		return
	}
	if this.min > node.data {
		// Change
		this.min = node.data
	}
	if this.max < node.data {
		this.max = node.data
	}
	// Visit left and right subtree
	this.findNode(node.left)
	this.findNode(node.right)
}
func(this BinaryTree) minAndMax() {
	if this.root == nil {
		return
	}
	// Assign root value to min max
	this.min = this.root.data
	this.max = this.root.data
	// Find node value
	this.findNode(this.root)
	// Display min and max node value
	fmt.Print(" Minimum : ", this.min)
	fmt.Print("\n Maximum : ", this.max, "\n")
}
func main() {
	var tree * BinaryTree = getBinaryTree()
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \     \               
	    2   3     12
	   /   / \    / 
	  1   10  8  15
	 /    /       \
	3    9         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.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.minAndMax()
}

Output

 Minimum : -4
 Maximum : 15
<?php
/*
  Php program for
  Find min and max element 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 $min;
	public $max;
	public	function __construct()
	{
		$this->root = NULL;
		$this->min = 0;
		$this->max = 0;
	}
	// Find the value of min and max node recursively
	public	function findNode($node)
	{
		if ($node == NULL)
		{
			return;
		}
		if ($this->min > $node->data)
		{
			// Change
			$this->min = $node->data;
		}
		if ($this->max < $node->data)
		{
			$this->max = $node->data;
		}
		// Visit left and right subtree
		$this->findNode($node->left);
		$this->findNode($node->right);
	}
	public	function minAndMax()
	{
		if ($this->root == NULL)
		{
			return;
		}
		// Assign root value to min max
		$this->min = $this->root->data;
		$this->max = $this->root->data;
		// Find node value
		$this->findNode($this->root);
		// Display min and max node value
		echo(" Minimum : ".$this->min);
		echo("\n Maximum : ".$this->max."\n");
	}
}

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

Output

 Minimum : -4
 Maximum : 15
/*
  Node JS program for
  Find min and max element 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.min = 0;
		this.max = 0;
	}
	// Find the value of min and max node recursively
	findNode(node)
	{
		if (node == null)
		{
			return;
		}
		if (this.min > node.data)
		{
			// Change
			this.min = node.data;
		}
		if (this.max < node.data)
		{
			this.max = node.data;
		}
		// Visit left and right subtree
		this.findNode(node.left);
		this.findNode(node.right);
	}
	minAndMax()
	{
		if (this.root == null)
		{
			return;
		}
		// Assign root value to min max
		this.min = this.root.data;
		this.max = this.root.data;
		// Find node value
		this.findNode(this.root);
		// Display min and max node value
		process.stdout.write(" Minimum : " + this.min);
		process.stdout.write("\n Maximum : " + this.max + "\n");
	}
}

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

Output

 Minimum : -4
 Maximum : 15
#  Python 3 program for
#  Find min and max element 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.min = 0
		self.max = 0
	
	#  Find the value of min and max node recursively
	def findNode(self, node) :
		if (node == None) :
			return
		
		if (self.min > node.data) :
			#  Change
			self.min = node.data
		
		if (self.max < node.data) :
			self.max = node.data
		
		#  Visit left and right subtree
		self.findNode(node.left)
		self.findNode(node.right)
	
	def minAndMax(self) :
		if (self.root == None) :
			return
		
		#  Assign root value to min max
		self.min = self.root.data
		self.max = self.root.data
		#  Find node value
		self.findNode(self.root)
		#  Display min and max node value
		print(" Minimum : ", self.min, end = "")
		print("\n Maximum : ", self.max )
	

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

if __name__ == "__main__": main()

Output

 Minimum :  -4
 Maximum :  15
#  Ruby program for
#  Find min and max element in a 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

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

	#  Find the value of min and max node recursively
	def findNode(node) 
		if (node == nil) 
			return
		end

		if (self.min > node.data) 
			#  Change
			self.min = node.data
		end

		if (self.max < node.data) 
			self.max = node.data
		end

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

	def minAndMax() 
		if (self.root == nil) 
			return
		end

		#  Assign root value to min max
		self.min = self.root.data
		self.max = self.root.data
		#  Find node value
		self.findNode(self.root)
		#  Display min and max node value
		print(" Minimum : ", self.min)
		print("\n Maximum : ", self.max ,"\n")
	end

end

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

main()

Output

 Minimum : -4
 Maximum : 15
/*
  Scala program for
  Find min and max element 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 min: Int,
		var max: Int)
{
	def this()
	{
		this(null,0,0);
	}
	// Find the value of min and max node recursively
	def findNode(node: TreeNode): Unit = {
		if (node == null)
		{
			return;
		}
		if (this.min > node.data)
		{
			// Change
			this.min = node.data;
		}
		if (this.max < node.data)
		{
			max = node.data;
		}
		// Visit left and right subtree
		this.findNode(node.left);
		this.findNode(node.right);
	}
	def minAndMax(): Unit = {
		if (this.root == null)
		{
			return;
		}
		// Assign root value to min max
		this.min = this.root.data;
		this.max = this.root.data;
		// Find node value
		this.findNode(this.root);
		// Display min and max node value
		print(" Minimum : " + this.min);
		print("\n Maximum : " + this.max + "\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         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.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.minAndMax();
	}
}

Output

 Minimum : -4
 Maximum : 15
/*
  Swift 4 program for
  Find min and max element 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 min: Int;
	var max: Int;
	init()
	{
		self.root = nil;
		self.min = 0;
		self.max = 0;
	}
	// Find the value of min and max node recursively
	func findNode(_ node: TreeNode? )
	{
		if (node == nil)
		{
			return;
		}
		if (self.min > node!.data)
		{
			// Change
			self.min = node!.data;
		}
		if (self.max < node!.data)
		{
			self.max = node!.data;
		}
		// Visit left and right subtree
		self.findNode(node!.left);
		self.findNode(node!.right);
	}
	func minAndMax()
	{
		if (self.root == nil)
		{
			return;
		}
		// Assign root value to min max
		self.min = self.root!.data;
		self.max = self.root!.data;
		// Find node value
		self.findNode(self.root);
		// Display min and max node value
		print(" Minimum : ", self.min, terminator: "");
		print("\n Maximum : ", self.max );
	}
}
func main()
{
	let tree: BinaryTree = BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \     \               
	    2   3     12
	   /   / \    / 
	  1   10  8  15
	 /    /       \
	3    9         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!.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.minAndMax();
}
main();

Output

 Minimum :  -4
 Maximum :  15
/*
  Kotlin program for
  Find min and max element 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 min: Int;
	var max: Int;
	constructor()
	{
		this.root = null;
		this.min = 0;
		this.max = 0;
	}
	// Find the value of min and max node recursively
	fun findNode(node: TreeNode?): Unit
	{
		if (node == null)
		{
			return;
		}
		if (this.min > node.data)
		{
			// Change
			this.min = node.data;
		}
		if (this.max < node.data)
		{
			this.max = node.data;
		}
		// Visit left and right subtree
		this.findNode(node.left);
		this.findNode(node.right);
	}
	fun minAndMax(): Unit
	{
		if (this.root == null)
		{
			return;
		}
		// Assign root value to min max
		this.min = this.root!!.data;
		this.max = this.root!!.data;
		// Find node value
		this.findNode(this.root);
		// Display min and max node value
		print(" Minimum : " + this.min);
		print("\n Maximum : " + this.max + "\n");
	}
}
fun main(args: Array < String > ): Unit
{
	val tree: BinaryTree = BinaryTree();
	/*
	         4                            
	       /   \    
	     -4     7    
	     / \     \               
	    2   3     12
	   /   / \    / 
	  1   10  8  15
	 /    /       \
	3    9         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?.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.minAndMax();
}

Output

 Minimum : -4
 Maximum : 15




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.

New Comment