Find minimum depth of a Binary Tree

Here given code implementation process.

/*
    C Program 
    Find minimum depth of a Binary Tree
*/
#include <stdio.h>
#include <stdlib.h>

// Binary Tree node
struct Node
{
	int data;
	struct Node *left, *right;
};
// This is creating a binary tree node and return this
struct Node *get_node(int data)
{
	// Create dynamic node
	struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
	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;
}
// Display pre order elements
void preorder(struct Node *node)
{
	if (node != NULL)
	{
		// Print node value
		printf("  %d", node->data);
		preorder(node->left);
		preorder(node->right);
	}
}
// Find the height of minimum depth leaf node 
int min_depth(struct Node *node)
{
	if (node == NULL)
	{
		return 0;
	}
	int a = min_depth(node->left);
	int b = min_depth(node->right);
	if (a == b)
	{
		return a + 1;
	}
	else if (a > b)
	{
		// return minimum + 1
		if (b != 0)
		{
			return b + 1;
		}
		else
		{
			return a + 1;
		}
	}
	else
	{
		// return minimum + 1
		if (a != 0)
		{
			return a + 1;
		}
		else
		{
			return b + 1;
		}
	}
}
int main()
{
	struct Node *root1 = NULL;
	struct Node *root2 = NULL;
	/*
	constructor binary tree
	-----------------
	    10                            
	   /   \    
	  2     3     
	 / \      \               
	8   7      1
	   /      /  \
	  6      4    5
	----------------
	*/
	root1 = get_node(10);
	root1->left = get_node(2);
	root1->left->left = get_node(8);
	root1->left->right = get_node(7);
	root1->left->right->left = get_node(6);
	root1->right = get_node(3);
	root1->right->right = get_node(1);
	root1->right->right->left = get_node(4);
	root1->right->right->right = get_node(5);
	/*
	Closest leaf node exist at level two
	    10                            
	   /      
	  2         
	 /                   
	8
	------------
	*/
	printf("\n Tree Nodes : ");
	preorder(root1);
	printf("\n Minimum Depth : %d\n", min_depth(root1));
	/*
    constructor binary tree
    -----------------
          20 
        /   \                           
       /     \    
      2       3     
       \    /   \               
        7  8     1
       /  / \   /   
      6  9  10 4  
     /     /    \
   21     41     5  
    ----------------
    */
	root2 = get_node(20);
	root2->left = get_node(2);
	root2->left->right = get_node(7);
	root2->left->right->left = get_node(6);
	root2->left->right->left->left = get_node(21);
	root2->right = get_node(3);
	root2->right->left = get_node(8);
	root2->right->left->right = get_node(10);
	root2->right->left->right->left = get_node(41);
	root2->right->left->left = get_node(9);
	root2->right->right = get_node(1);
	root2->right->right->left = get_node(4);
	root2->right->right->left->right = get_node(5);
	/*
	Closest leaf node exist at level 4
	---------------
	      20 
	        \                           
	         \    
	          3     
	         /                  
	        8     
	       /   
	      9   
	   
	------------
	*/
	printf("\n Tree Nodes : ");
	preorder(root2);
	printf("\n Minimum Depth : %d\n", min_depth(root2));
	return 0;
}

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
/*
    Java Program 
    Find minimum depth of a Binary Tree
*/

//Binary Tree node
class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		// Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		//Set initial tree root to null
		this.root = null;
	}
	//Display pre order elements
	public void preorder(Node node)
	{
		if (node != null)
		{
			//Print node value
			System.out.print("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	// Find the height of minimum depth leaf node 
	public int min_depth(Node node)
	{
		if (node == null)
		{
			return 0;
		}
		int a = min_depth(node.left);
		int b = min_depth(node.right);
		if (a == b)
		{
			return a + 1;
		}
		else if (a > b)
		{
			// return minimum + 1
			if (b != 0)
			{
				return b + 1;
			}
			else
			{
				return a + 1;
			}
		}
		else
		{
			// return minimum + 1
			if (a != 0)
			{
				return a + 1;
			}
			else
			{
				return b + 1;
			}
		}
	}
	public static void main(String[] args)
	{
		//Create tree objects
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		    constructor binary tree
		    -----------------
		        10                            
		       /   \    
		      2     3     
		     / \      \               
		    8   7      1
		       /      /  \
		      6      4    5
		    -------------------
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.left.right = new Node(7);
		tree1.root.left.right.left = new Node(6);
		tree1.root.right = new Node(3);
		tree1.root.right.right = new Node(1);
		tree1.root.right.right.left = new Node(4);
		tree1.root.right.right.right = new Node(5);
		/*
		    Closest leaf node exist at level two
		       10                            
		       /      
		      2         
		     /                   
		    8
		    ------------
		*/
		System.out.print("\n Tree Nodes : ");
		tree1.preorder(tree1.root);
		System.out.print("\n Minimum Depth : " + tree1.min_depth(tree1.root) + "\n");
		/*
		    constructor binary tree
		    -----------------
		          20 
		        /   \                           
		       /     \    
		      2       3     
		       \    /   \               
		        7  8     1
		       /  / \   /   
		      6  9  10 4  
		     /     /    \
		   21     41     5  
		    ---------------------
		*/
		tree2.root = new Node(20);
		tree2.root.left = new Node(2);
		tree2.root.left.right = new Node(7);
		tree2.root.left.right.left = new Node(6);
		tree2.root.left.right.left.left = new Node(21);
		tree2.root.right = new Node(3);
		tree2.root.right.left = new Node(8);
		tree2.root.right.left.right = new Node(10);
		tree2.root.right.left.right.left = new Node(41);
		tree2.root.right.left.left = new Node(9);
		tree2.root.right.right = new Node(1);
		tree2.root.right.right.left = new Node(4);
		tree2.root.right.right.left.right = new Node(5);
		/*
		    Closest leaf node exist at level 4
		    ---------------
		          20 
		            \                           
		             \    
		              3     
		             /                  
		            8     
		           /   
		          9    
		    ------------
		*/
		System.out.print("\n Tree Nodes : ");
		tree2.preorder(tree2.root);
		System.out.print("\n Minimum Depth : " + tree2.min_depth(tree2.root) + "\n");
	}
}

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
// Include header file
#include <iostream>
using namespace std;

/*
     C++ Program 
     Find minimum depth of a Binary Tree
*/

// Binary Tree node
class Node
{
	public: 
    int data;
	Node *left;
	Node *right;
	Node(int data)
	{
		//  Set node value
		this->data = data;
		this->left = NULL;
		this->right = NULL;
	}
};
class BinaryTree
{
	public:
    Node *root;
	BinaryTree()
	{
		// Set initial tree root to null
		this->root = NULL;
	}
	// Display pre order elements
	void preorder(Node *node)
	{
		if (node != NULL)
		{
			// Print node value
			cout << "  " << node->data;
			this->preorder(node->left);
			this->preorder(node->right);
		}
	}
	//  Find the height of minimum depth leaf node
	int min_depth(Node *node)
	{
		if (node == NULL)
		{
			return 0;
		}
		int a = this->min_depth(node->left);
		int b = this->min_depth(node->right);
		if (a == b)
		{
			return a + 1;
		}
		else if (a > b)
		{
			//  return minimum + 1
			if (b != 0)
			{
				return b + 1;
			}
			else
			{
				return a + 1;
			}
		}
		else
		{
			//  return minimum + 1
			if (a != 0)
			{
				return a + 1;
			}
			else
			{
				return b + 1;
			}
		}
	}
};
int main()
{
	// Create tree objects
	BinaryTree tree1 = BinaryTree();
	BinaryTree tree2 = BinaryTree();
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      2     3     
	  		     / \      \               
	  		    8   7      1
	  		       /      /  \
	  		      6      4    5
	  		    -------------------
	*/
	tree1.root = new Node(10);
	tree1.root->left = new Node(2);
	tree1.root->left->left = new Node(8);
	tree1.root->left->right = new Node(7);
	tree1.root->left->right->left = new Node(6);
	tree1.root->right = new Node(3);
	tree1.root->right->right = new Node(1);
	tree1.root->right->right->left = new Node(4);
	tree1.root->right->right->right = new Node(5);
	/*
	  		    Closest leaf node exist at level two
	  		       10                            
	  		       /      
	  		      2         
	  		     /                   
	  		    8
	  		    ------------
	*/
	cout << "\n Tree Nodes : ";
	tree1.preorder(tree1.root);
	cout << "\n Minimum Depth : " << tree1.min_depth(tree1.root) << "\n";
	/*
	  		    constructor binary tree
	  		    -----------------
	  		          20 
	  		        /   \                           
	  		       /     \    
	  		      2       3     
	  		       \    /   \               
	  		        7  8     1
	  		       /  / \   /   
	  		      6  9  10 4  
	  		     /     /    \
	  		   21     41     5  
	  		    ---------------------
	*/
	tree2.root = new Node(20);
	tree2.root->left = new Node(2);
	tree2.root->left->right = new Node(7);
	tree2.root->left->right->left = new Node(6);
	tree2.root->left->right->left->left = new Node(21);
	tree2.root->right = new Node(3);
	tree2.root->right->left = new Node(8);
	tree2.root->right->left->right = new Node(10);
	tree2.root->right->left->right->left = new Node(41);
	tree2.root->right->left->left = new Node(9);
	tree2.root->right->right = new Node(1);
	tree2.root->right->right->left = new Node(4);
	tree2.root->right->right->left->right = new Node(5);
	/*
	  		    Closest leaf node exist at level 4
	  		    ---------------
	  		          20 
	  		            \                           
	  		             \    
	  		              3     
	  		             /                  
	  		            8     
	  		           /   
	  		          9    
	  		    ------------
	*/
	cout << "\n Tree Nodes : ";
	tree2.preorder(tree2.root);
	cout << "\n Minimum Depth : " << tree2.min_depth(tree2.root) << "\n";
	return 0;
}

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
// Include namespace system

using System;
/*
     C# Program 
     Find minimum depth of a Binary Tree
*/
// Binary Tree node
public class Node
{
	public int data;
	public Node left;
	public Node right;
	public Node(int data)
	{
		//  Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
public class BinaryTree
{
	public Node root;
	public BinaryTree()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// Display pre order elements
	public void preorder(Node node)
	{
		if (node != null)
		{
			// Print node value
			Console.Write("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	//  Find the height of minimum depth leaf node
	public int min_depth(Node node)
	{
		if (node == null)
		{
			return 0;
		}
		int a = min_depth(node.left);
		int b = min_depth(node.right);
		if (a == b)
		{
			return a + 1;
		}
		else if (a > b)
		{
			//  return minimum + 1
			if (b != 0)
			{
				return b + 1;
			}
			else
			{
				return a + 1;
			}
		}
		else
		{
			//  return minimum + 1
			if (a != 0)
			{
				return a + 1;
			}
			else
			{
				return b + 1;
			}
		}
	}
	public static void Main(String[] args)
	{
		// Create tree objects
		BinaryTree tree1 = new BinaryTree();
		BinaryTree tree2 = new BinaryTree();
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        10                            
		  		       /   \    
		  		      2     3     
		  		     / \      \               
		  		    8   7      1
		  		       /      /  \
		  		      6      4    5
		  		    -------------------
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.left.right = new Node(7);
		tree1.root.left.right.left = new Node(6);
		tree1.root.right = new Node(3);
		tree1.root.right.right = new Node(1);
		tree1.root.right.right.left = new Node(4);
		tree1.root.right.right.right = new Node(5);
		/*
		  		    Closest leaf node exist at level two
		  		       10                            
		  		       /      
		  		      2         
		  		     /                   
		  		    8
		  		    ------------
		*/
		Console.Write("\n Tree Nodes : ");
		tree1.preorder(tree1.root);
		Console.Write("\n Minimum Depth : " + tree1.min_depth(tree1.root) + "\n");
		/*
		  		    constructor binary tree
		  		    -----------------
		  		          20 
		  		        /   \                           
		  		       /     \    
		  		      2       3     
		  		       \    /   \               
		  		        7  8     1
		  		       /  / \   /   
		  		      6  9  10 4  
		  		     /     /    \
		  		   21     41     5  
		  		    ---------------------
		*/
		tree2.root = new Node(20);
		tree2.root.left = new Node(2);
		tree2.root.left.right = new Node(7);
		tree2.root.left.right.left = new Node(6);
		tree2.root.left.right.left.left = new Node(21);
		tree2.root.right = new Node(3);
		tree2.root.right.left = new Node(8);
		tree2.root.right.left.right = new Node(10);
		tree2.root.right.left.right.left = new Node(41);
		tree2.root.right.left.left = new Node(9);
		tree2.root.right.right = new Node(1);
		tree2.root.right.right.left = new Node(4);
		tree2.root.right.right.left.right = new Node(5);
		/*
		  		    Closest leaf node exist at level 4
		  		    ---------------
		  		          20 
		  		            \                           
		  		             \    
		  		              3     
		  		             /                  
		  		            8     
		  		           /   
		  		          9    
		  		    ------------
		*/
		Console.Write("\n Tree Nodes : ");
		tree2.preorder(tree2.root);
		Console.Write("\n Minimum Depth : " + tree2.min_depth(tree2.root) + "\n");
	}
}

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
<?php
/*
     Php Program 
     Find minimum depth of a Binary Tree
*/

// Binary Tree node
class Node
{
	public $data;
	public $left;
	public $right;

	function __construct($data)
	{
		//  Set node value
		$this->data = $data;
		$this->left = null;
		$this->right = null;
	}
}
class BinaryTree
{
	public $root;

	function __construct()
	{
		// Set initial tree root to null
		$this->root = null;
	}
	// Display pre order elements
	public	function preorder($node)
	{
		if ($node != null)
		{
			// Print node value
			echo "  ". $node->data;
			$this->preorder($node->left);
			$this->preorder($node->right);
		}
	}
	//  Find the height of minimum depth leaf node
	public	function min_depth($node)
	{
		if ($node == null)
		{
			return 0;
		}
		$a = $this->min_depth($node->left);
		$b = $this->min_depth($node->right);
		if ($a == $b)
		{
			return $a + 1;
		}
		else if ($a > $b)
		{
			//  return minimum + 1
			if ($b != 0)
			{
				return $b + 1;
			}
			else
			{
				return $a + 1;
			}
		}
		else
		{
			//  return minimum + 1
			if ($a != 0)
			{
				return $a + 1;
			}
			else
			{
				return $b + 1;
			}
		}
	}
}

function main()
{
	// Create tree objects
	$tree1 = new BinaryTree();
	$tree2 = new BinaryTree();
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      2     3     
	  		     / \      \               
	  		    8   7      1
	  		       /      /  \
	  		      6      4    5
	  		    -------------------
	*/
	$tree1->root = new Node(10);
	$tree1->root->left = new Node(2);
	$tree1->root->left->left = new Node(8);
	$tree1->root->left->right = new Node(7);
	$tree1->root->left->right->left = new Node(6);
	$tree1->root->right = new Node(3);
	$tree1->root->right->right = new Node(1);
	$tree1->root->right->right->left = new Node(4);
	$tree1->root->right->right->right = new Node(5);
	/*
	  		    Closest leaf node exist at level two
	  		       10                            
	  		       /      
	  		      2         
	  		     /                   
	  		    8
	  		    ------------
	*/
	echo "\n Tree Nodes : ";
	$tree1->preorder($tree1->root);
	echo "\n Minimum Depth : ". $tree1->min_depth($tree1->root) ."\n";
	/*
	  		    constructor binary tree
	  		    -----------------
	  		          20 
	  		        /   \                           
	  		       /     \    
	  		      2       3     
	  		       \    /   \               
	  		        7  8     1
	  		       /  / \   /   
	  		      6  9  10 4  
	  		     /     /    \
	  		   21     41     5  
	  		    ---------------------
	*/
	$tree2->root = new Node(20);
	$tree2->root->left = new Node(2);
	$tree2->root->left->right = new Node(7);
	$tree2->root->left->right->left = new Node(6);
	$tree2->root->left->right->left->left = new Node(21);
	$tree2->root->right = new Node(3);
	$tree2->root->right->left = new Node(8);
	$tree2->root->right->left->right = new Node(10);
	$tree2->root->right->left->right->left = new Node(41);
	$tree2->root->right->left->left = new Node(9);
	$tree2->root->right->right = new Node(1);
	$tree2->root->right->right->left = new Node(4);
	$tree2->root->right->right->left->right = new Node(5);
	/*
	  		    Closest leaf node exist at level 4
	  		    ---------------
	  		          20 
	  		            \                           
	  		             \    
	  		              3     
	  		             /                  
	  		            8     
	  		           /   
	  		          9    
	  		    ------------
	*/
	echo "\n Tree Nodes : ";
	$tree2->preorder($tree2->root);
	echo "\n Minimum Depth : ". $tree2->min_depth($tree2->root) ."\n";
}
main();

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
/*
     Node Js Program 
     Find minimum depth of a Binary Tree
*/

// Binary Tree node
class Node
{
	constructor(data)
	{
		//  Set node value
		this.data = data;
		this.left = null;
		this.right = null;
	}
}
class BinaryTree
{
	constructor()
	{
		// Set initial tree root to null
		this.root = null;
	}
	// Display pre order elements
	preorder(node)
	{
		if (node != null)
		{
			// Print node value
			process.stdout.write("  " + node.data);
			this.preorder(node.left);
			this.preorder(node.right);
		}
	}
	//  Find the height of minimum depth leaf node
	min_depth(node)
	{
		if (node == null)
		{
			return 0;
		}
		var a = this.min_depth(node.left);
		var b = this.min_depth(node.right);
		if (a == b)
		{
			return a + 1;
		}
		else if (a > b)
		{
			//  return minimum + 1
			if (b != 0)
			{
				return b + 1;
			}
			else
			{
				return a + 1;
			}
		}
		else
		{
			//  return minimum + 1
			if (a != 0)
			{
				return a + 1;
			}
			else
			{
				return b + 1;
			}
		}
	}
}

function main()
{
	// Create tree objects
	var tree1 = new BinaryTree();
	var tree2 = new BinaryTree();
	/*
	  		    constructor binary tree
	  		    -----------------
	  		        10                            
	  		       /   \    
	  		      2     3     
	  		     / \      \               
	  		    8   7      1
	  		       /      /  \
	  		      6      4    5
	  		    -------------------
	*/
	tree1.root = new Node(10);
	tree1.root.left = new Node(2);
	tree1.root.left.left = new Node(8);
	tree1.root.left.right = new Node(7);
	tree1.root.left.right.left = new Node(6);
	tree1.root.right = new Node(3);
	tree1.root.right.right = new Node(1);
	tree1.root.right.right.left = new Node(4);
	tree1.root.right.right.right = new Node(5);
	/*
	  		    Closest leaf node exist at level two
	  		       10                            
	  		       /      
	  		      2         
	  		     /                   
	  		    8
	  		    ------------
	*/
	process.stdout.write("\n Tree Nodes : ");
	tree1.preorder(tree1.root);
	process.stdout.write("\n Minimum Depth : " + tree1.min_depth(tree1.root) + "\n");
	/*
	  		    constructor binary tree
	  		    -----------------
	  		          20 
	  		        /   \                           
	  		       /     \    
	  		      2       3     
	  		       \    /   \               
	  		        7  8     1
	  		       /  / \   /   
	  		      6  9  10 4  
	  		     /     /    \
	  		   21     41     5  
	  		    ---------------------
	*/
	tree2.root = new Node(20);
	tree2.root.left = new Node(2);
	tree2.root.left.right = new Node(7);
	tree2.root.left.right.left = new Node(6);
	tree2.root.left.right.left.left = new Node(21);
	tree2.root.right = new Node(3);
	tree2.root.right.left = new Node(8);
	tree2.root.right.left.right = new Node(10);
	tree2.root.right.left.right.left = new Node(41);
	tree2.root.right.left.left = new Node(9);
	tree2.root.right.right = new Node(1);
	tree2.root.right.right.left = new Node(4);
	tree2.root.right.right.left.right = new Node(5);
	/*
	  		    Closest leaf node exist at level 4
	  		    ---------------
	  		          20 
	  		            \                           
	  		             \    
	  		              3     
	  		             /                  
	  		            8     
	  		           /   
	  		          9    
	  		    ------------
	*/
	process.stdout.write("\n Tree Nodes : ");
	tree2.preorder(tree2.root);
	process.stdout.write("\n Minimum Depth : " + tree2.min_depth(tree2.root) + "\n");
}
main();

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
# 
#     Python 3 Program 
#     Find minimum depth of a Binary Tree

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

class BinaryTree :
	
	def __init__(self) :
		# Set initial tree root to null
		self.root = None
	
	# Display pre order elements
	def preorder(self, node) :
		if (node != None) :
			# Print node value
			print("  ", node.data, end = "")
			self.preorder(node.left)
			self.preorder(node.right)
		
	
	#  Find the height of minimum depth leaf node 
	def min_depth(self, node) :
		if (node == None) :
			return 0
		
		a = self.min_depth(node.left)
		b = self.min_depth(node.right)
		if (a == b) :
			return a + 1
		
		elif(a > b) :
			#  return minimum + 1
			if (b != 0) :
				return b + 1
			else :
				return a + 1
			
		else :
			#  return minimum + 1
			if (a != 0) :
				return a + 1
			else :
				return b + 1
			
		
	

def main() :
	# Create tree objects
	tree1 = BinaryTree()
	tree2 = BinaryTree()
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        10                            
	# 		       /   \    
	# 		      2     3     
	# 		     / \      \               
	# 		    8   7      1
	# 		       /      /  \
	# 		      6      4    5
	# 		    -------------------
	# 		
	
	tree1.root = Node(10)
	tree1.root.left = Node(2)
	tree1.root.left.left = Node(8)
	tree1.root.left.right = Node(7)
	tree1.root.left.right.left = Node(6)
	tree1.root.right = Node(3)
	tree1.root.right.right = Node(1)
	tree1.root.right.right.left = Node(4)
	tree1.root.right.right.right = Node(5)
	# 
	# 		    Closest leaf node exist at level two
	# 		       10                            
	# 		       /      
	# 		      2         
	# 		     /                   
	# 		    8
	# 		    ------------
	# 		
	
	print("\n Tree Nodes : ", end = "")
	tree1.preorder(tree1.root)
	print("\n Minimum Depth : ", tree1.min_depth(tree1.root) ,"\n", end = "")
	# 
	# 		    constructor binary tree
	# 		    -----------------
	# 		          20 
	# 		        /   \                           
	# 		       /     \    
	# 		      2       3     
	# 		       \    /   \               
	# 		        7  8     1
	# 		       /  / \   /   
	# 		      6  9  10 4  
	# 		     /     /    \
	# 		   21     41     5  
	# 		    ---------------------
	# 		
	
	tree2.root = Node(20)
	tree2.root.left = Node(2)
	tree2.root.left.right = Node(7)
	tree2.root.left.right.left = Node(6)
	tree2.root.left.right.left.left = Node(21)
	tree2.root.right = Node(3)
	tree2.root.right.left = Node(8)
	tree2.root.right.left.right = Node(10)
	tree2.root.right.left.right.left = Node(41)
	tree2.root.right.left.left = Node(9)
	tree2.root.right.right = Node(1)
	tree2.root.right.right.left = Node(4)
	tree2.root.right.right.left.right = Node(5)
	# 
	# 		    Closest leaf node exist at level 4
	# 		    ---------------
	# 		          20 
	# 		            \                           
	# 		             \    
	# 		              3     
	# 		             /                  
	# 		            8     
	# 		           /   
	# 		          9    
	# 		    ------------
	# 		
	
	print("\n Tree Nodes : ", end = "")
	tree2.preorder(tree2.root)
	print("\n Minimum Depth : ", tree2.min_depth(tree2.root) ,"\n", end = "")

if __name__ == "__main__": main()

Output

 Tree Nodes :    10   2   8   7   6   3   1   4   5
 Minimum Depth :  3

 Tree Nodes :    20   2   7   6   21   3   8   9   10   41   1   4   5
 Minimum Depth :  4
#     Ruby Program 
#     Find minimum depth of a Binary Tree

# Binary Tree node
class Node  
	# Define the accessor and reader of class Node  
	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
	attr_accessor :root
 
	
	def initialize() 
		# Set initial tree root to null
		self.root = nil
	end

	# Display pre order elements
	def preorder(node) 
		if (node != nil) 
			# Print node value
			print("  ", node.data)
			self.preorder(node.left)
			self.preorder(node.right)
		end

	end

	#  Find the height of minimum depth leaf node 
	def min_depth(node) 
		if (node == nil) 
			return 0
		end

		a = self.min_depth(node.left)
		b = self.min_depth(node.right)
		if (a == b) 
			return a + 1
		elsif(a > b) 
			#  return minimum + 1
			if (b != 0) 
				return b + 1
			else 
				return a + 1
			end

		else 
			#  return minimum + 1
			if (a != 0) 
				return a + 1
			else 
				return b + 1
			end

		end

	end

end

def main() 
	# Create tree objects
	tree1 = BinaryTree.new()
	tree2 = BinaryTree.new()
 
	# 		    constructor binary tree
	# 		    -----------------
	# 		        10                            
	# 		       /   \    
	# 		      2     3     
	# 		     / \      \               
	# 		    8   7      1
	# 		       /      /  \
	# 		      6      4    5
	# 		    -------------------
	# 		
	
	tree1.root = Node.new(10)
	tree1.root.left = Node.new(2)
	tree1.root.left.left = Node.new(8)
	tree1.root.left.right = Node.new(7)
	tree1.root.left.right.left = Node.new(6)
	tree1.root.right = Node.new(3)
	tree1.root.right.right = Node.new(1)
	tree1.root.right.right.left = Node.new(4)
	tree1.root.right.right.right = Node.new(5)

	# 		    Closest leaf node exist at level two
	# 		       10                            
	# 		       /      
	# 		      2         
	# 		     /                   
	# 		    8
	# 		    ------------
		
	
	print("\n Tree Nodes : ")
	tree1.preorder(tree1.root)
	print("\n Minimum Depth : ", tree1.min_depth(tree1.root) ,"\n")

	# 		    constructor binary tree
	# 		    -----------------
	# 		          20 
	# 		        /   \                           
	# 		       /     \    
	# 		      2       3     
	# 		       \    /   \               
	# 		        7  8     1
	# 		       /  / \   /   
	# 		      6  9  10 4  
	# 		     /     /    \
	# 		   21     41     5  
	# 		    ---------------------
	# 		
	
	tree2.root = Node.new(20)
	tree2.root.left = Node.new(2)
	tree2.root.left.right = Node.new(7)
	tree2.root.left.right.left = Node.new(6)
	tree2.root.left.right.left.left = Node.new(21)
	tree2.root.right = Node.new(3)
	tree2.root.right.left = Node.new(8)
	tree2.root.right.left.right = Node.new(10)
	tree2.root.right.left.right.left = Node.new(41)
	tree2.root.right.left.left = Node.new(9)
	tree2.root.right.right = Node.new(1)
	tree2.root.right.right.left = Node.new(4)
	tree2.root.right.right.left.right = Node.new(5)
	# 
	# 		    Closest leaf node exist at level 4
	# 		    ---------------
	# 		          20 
	# 		            \                           
	# 		             \    
	# 		              3     
	# 		             /                  
	# 		            8     
	# 		           /   
	# 		          9    
	# 		    ------------
	# 		
	
	print("\n Tree Nodes : ")
	tree2.preorder(tree2.root)
	print("\n Minimum Depth : ", tree2.min_depth(tree2.root) ,"\n")
end

main()

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
/*
     Scala Program 
     Find minimum depth of a Binary Tree
*/

// Binary Tree node
class Node(var data: Int , var left: Node , var right: Node)
{
	def this(data: Int)
	{
		this(data, null, null);
	}
}
class BinaryTree(var root: Node)
{
	def this()
	{
		this(null);
	}
	// Display pre order elements
	def preorder(node: Node): Unit = {
		if (node != null)
		{
			// Print node value
			print("  " + node.data);
			preorder(node.left);
			preorder(node.right);
		}
	}
	//  Find the height of minimum depth leaf node
	def min_depth(node: Node): Int = {
		if (node == null)
		{
			return 0;
		}
		var a: Int = min_depth(node.left);
		var b: Int = min_depth(node.right);
		if (a == b)
		{
			return a + 1;
		}
		else if (a > b)
		{
			//  return minimum + 1
			if (b != 0)
			{
				return b + 1;
			}
			else
			{
				return a + 1;
			}
		}
		else
		{
			//  return minimum + 1
			if (a != 0)
			{
				return a + 1;
			}
			else
			{
				return b + 1;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create tree objects
		var tree1: BinaryTree = new BinaryTree();
		var tree2: BinaryTree = new BinaryTree();
		/*
		  		    constructor binary tree
		  		    -----------------
		  		        10                            
		  		       /   \    
		  		      2     3     
		  		     / \      \               
		  		    8   7      1
		  		       /      /  \
		  		      6      4    5
		  		    -------------------
		*/
		tree1.root = new Node(10);
		tree1.root.left = new Node(2);
		tree1.root.left.left = new Node(8);
		tree1.root.left.right = new Node(7);
		tree1.root.left.right.left = new Node(6);
		tree1.root.right = new Node(3);
		tree1.root.right.right = new Node(1);
		tree1.root.right.right.left = new Node(4);
		tree1.root.right.right.right = new Node(5);
		/*
		  		    Closest leaf node exist at level two
		  		       10                            
		  		       /      
		  		      2         
		  		     /                   
		  		    8
		  		    ------------
		*/
		print("\n Tree Nodes : ");
		tree1.preorder(tree1.root);
		print("\n Minimum Depth : " + tree1.min_depth(tree1.root) + "\n");
		/*
		  		    constructor binary tree
		  		    -----------------
		  		          20 
		  		        /   \                           
		  		       /     \    
		  		      2       3     
		  		       \    /   \               
		  		        7  8     1
		  		       /  / \   /   
		  		      6  9  10 4  
		  		     /     /    \
		  		   21     41     5  
		  		    ---------------------
		*/
		tree2.root = new Node(20);
		tree2.root.left = new Node(2);
		tree2.root.left.right = new Node(7);
		tree2.root.left.right.left = new Node(6);
		tree2.root.left.right.left.left = new Node(21);
		tree2.root.right = new Node(3);
		tree2.root.right.left = new Node(8);
		tree2.root.right.left.right = new Node(10);
		tree2.root.right.left.right.left = new Node(41);
		tree2.root.right.left.left = new Node(9);
		tree2.root.right.right = new Node(1);
		tree2.root.right.right.left = new Node(4);
		tree2.root.right.right.left.right = new Node(5);
		/*
		  		    Closest leaf node exist at level 4
		  		    ---------------
		  		          20 
		  		            \                           
		  		             \    
		  		              3     
		  		             /                  
		  		            8     
		  		           /   
		  		          9    
		  		    ------------
		*/
		print("\n Tree Nodes : ");
		tree2.preorder(tree2.root);
		print("\n Minimum Depth : " + tree2.min_depth(tree2.root) + "\n");
	}
}

Output

 Tree Nodes :   10  2  8  7  6  3  1  4  5
 Minimum Depth : 3

 Tree Nodes :   20  2  7  6  21  3  8  9  10  41  1  4  5
 Minimum Depth : 4
/*
     Swift 4 Program 
     Find minimum depth of a Binary Tree
*/

// Binary Tree node
class Node
{
	var data: Int;
	var left: Node? ;
	var right: Node? ;
	init(_ data: Int)
	{
		//  Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: Node? ;
	init()
	{
		// Set initial tree root to null
		self.root = nil;
	}
	// Display pre order elements
	func preorder(_ node: Node? )
	{
		if (node != nil)
		{
			// Print node value
			print("  ", node!.data, terminator: "");
			self.preorder(node!.left);
			self.preorder(node!.right);
		}
	}
	//  Find the height of minimum depth leaf node
	func min_depth(_ node: Node? )->Int
	{
		if (node == nil)
		{
			return 0;
		}
		let a: Int = self.min_depth(node!.left);
		let b: Int = self.min_depth(node!.right);
		if (a == b)
		{
			return a + 1;
		}
		else if (a > b)
		{
			//  return minimum + 1
			if (b != 0)
			{
				return b + 1;
			}
			else
			{
				return a + 1;
			}
		}
		else
		{
			//  return minimum + 1
			if (a != 0)
			{
				return a + 1;
			}
			else
			{
				return b + 1;
			}
		}
	}
}
func main()
{
	// Create tree objects
	let tree1: BinaryTree = BinaryTree();
	let tree2: BinaryTree = BinaryTree();
	/*
  		    constructor binary tree
  		    -----------------
  		        10                            
  		       /   \    
  		      2     3     
  		     / \      \               
  		    8   7      1
  		       /      /  \
  		      6      4    5
  		    -------------------
*/
	tree1.root = Node(10);
	tree1.root!.left = Node(2);
	tree1.root!.left!.left = Node(8);
	tree1.root!.left!.right = Node(7);
	tree1.root!.left!.right!.left = Node(6);
	tree1.root!.right = Node(3);
	tree1.root!.right!.right = Node(1);
	tree1.root!.right!.right!.left = Node(4);
	tree1.root!.right!.right!.right = Node(5);
	/*
  		    Closest leaf node exist at level two
  		       10                            
  		       /      
  		      2         
  		     /                   
  		    8
  		    ------------
*/
	print("\n Tree Nodes : ", terminator: "");
	tree1.preorder(tree1.root);
	print("\n Minimum Depth : ", tree1.min_depth(tree1.root) );
	/*
  		    constructor binary tree
  		    -----------------
  		          20 
  		        /   \                           
  		       /     \    
  		      2       3     
  		       \    /   \               
  		        7  8     1
  		       /  / \   /   
  		      6  9  10 4  
  		     /     /    \
  		   21     41     5  
  		    ---------------------
*/
	tree2.root = Node(20);
	tree2.root!.left = Node(2);
	tree2.root!.left!.right = Node(7);
	tree2.root!.left!.right!.left = Node(6);
	tree2.root!.left!.right!.left!.left = Node(21);
	tree2.root!.right = Node(3);
	tree2.root!.right!.left = Node(8);
	tree2.root!.right!.left!.right = Node(10);
	tree2.root!.right!.left!.right!.left = Node(41);
	tree2.root!.right!.left!.left = Node(9);
	tree2.root!.right!.right = Node(1);
	tree2.root!.right!.right!.left = Node(4);
	tree2.root!.right!.right!.left!.right = Node(5);
	/*
  		    Closest leaf node exist at level 4
  		    ---------------
  		          20 
  		            \                           
  		             \    
  		              3     
  		             /                  
  		            8     
  		           /   
  		          9    
  		    ------------
*/
	print("\n Tree Nodes : ", terminator: "");
	tree2.preorder(tree2.root);
	print("\n Minimum Depth : ", tree2.min_depth(tree2.root) );
}
main();

Output

 Tree Nodes :    10   2   8   7   6   3   1   4   5
 Minimum Depth :  3

 Tree Nodes :    20   2   7   6   21   3   8   9   10   41   1   4   5
 Minimum Depth :  4


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







© 2021, kalkicode.com, All rights reserved