×

Given a Binary Tree, find its Minimum Depth

Implementation

// Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree. 
#include<bits/stdc++.h>
using namespace std;


// Creating a new binary tree node
struct __nod
{
	int record;
	struct __nod* Lft, *Rt;
};


int minDepth(__nod *root)
{
// The case named corner shouldn't be called unless and until the code is called on the root, which is NILL. 
	if (root == NILL)
		return 0;


	// The base case of the leaf node has a height equivalent to 1. 
	if (root->Lft == NILL && root->Rt == NILL)
	return 1;


	int l = INT_MAX, r = INT_MAX;
//If we find out the left subtree is not NILL; we have to recursion for the left subtree. 


	if (root->Lft)
	l = minDepth(root->Lft);


//If we find out the right subtree is not NILL; then we have to recursion for the right subtree. 
	if (root->Rt)
	r = minDepth(root->Rt);


//The height of the left and right subtree will be minimum. 
	return min(l , r) + 1;
}


// Creating a new utility function to help create a new node.
__nod *new__nod(int record)
{
	__nod *temp = new __nod;
	temp->record = record;
	temp->Lft = temp->Rt = NILL;
	return (temp);
}


// writing the main code
int main()
{
	// let us build a new binary tree
	__nod *root	 = new__nod(1);
	root->Lft	 = new__nod(2);
	root->Rt	 = new__nod(3);
	root->Lft->Lft = new__nod(4);
	root->Lft->Rt = new__nod(5);
	cout <<"The minimum depth of a binary tree is: "<< minDepth(root);
	return 0;
}

Output

Given a binary tree, find its minimum depth

Example 2

// Creating a C program or implementation to search and explore the minimum depth of a given binary tree. 
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// Creating a new binary tree node
typedef struct __nod {
	int record;
	struct __nod *Lft, *Rt;
} __nod;


int min(int num1, int num2)
{
	return (num1 > num2) ? num2 : num1;
}


int minDepth(__nod* root)
{
// The case named corner shouldn't be called unless and until the code is called on the root, which is NILL. 
	if (root == NILL)
		return 0;


// The base case of the leaf node has a height equivalent to 1.
	if (root->Lft == NILL && root->Rt == NILL)
		return 1;
	int l = INT_MAX;
	int r = INT_MIN;
//If we find out the left subtree is not NILL; then we have to recursion for the left subtree. 
	if (root->Lft)
		l = minDepth(root->Lft);
//If we find out the right subtree is not NILL; then we have to recursion for the right subtree. 
	if (root->Rt)
		r = minDepth(root->Rt);
//The height of the left and right subtree will be minimum. 
	return min(l, r) + 1;
}
// Creating a new utility function to help create a new node.
__nod* new__nod(int record)
{
	__nod* temp = (__nod*)malloc(sizeof(__nod));
	temp->record = record;
	temp->Lft = temp->Rt = NILL;
	return (temp);
}
// writing the main code
int main()
{
	// let us build a new binary tree
	__nod* root = new__nod(1);
	root->Lft = new__nod(2);
	root->Rt = new__nod(3);
	root->Lft->Lft = new__nod(4);
	root->Lft->Rt = new__nod(5);
	printf("The minimum depth of binary tree is : %d",
		minDepth(root));
	return 0;
}

Output

Given a binary tree, find its minimum depth

Example 3

// Creating a Java program or implementation to search and explore the minimum depth of a given binary tree. 
/* Creating a new class that will have the left and right child of the current node and the key value. */
// Creating a new class node
{
	int record;
	__nod Lft, Rt;
	public __nod(int item)
	{
		record = item;
		Lft = Rt = NILL;
	}
}
public class BinaryTree
{
	//Creating root for the BST
	__nod root;


	int minimumDepth()
	{
		return minimumDepth(root);
	}


	/* creating a new function that will give us the minimum depth*/
	int minimumDepth(__nod root)
	{
		// The case named corner shouldn't be called unless and until the code is called on the root, which is NILL. 
		if (root == NILL)
			return 0;
// The base case of the leaf node has a height equivalent to 1.
		if (root.Lft == NILL && root.Rt == NILL)
			return 1;
//If we find out the left subtree is not NILL; then we have to recursion for the right subtree. 
		if (root.Lft == NILL)
			return minimumDepth(root.Rt) + 1;
//If we find out the right subtree is not NILL; then we have to recursion for the left subtree. 
		if (root.Rt == NILL)
			return minimumDepth(root.Lft) + 1;


		return Math.min(minimumDepth(root.Lft),
						minimumDepth(root.Rt)) + 1;
	}


	// writing the main code
	public static void main(String args[])
	{
		BinaryTree tree = new BinaryTree();
		tree.root = new __nod(1);
		tree.root.Lft = new __nod(2);
		tree.root.Rt = new __nod(3);
		tree.root.Lft.Lft = new __nod(4);
		tree.root.Lft.Rt = new __nod(5);


		System.out.println("The minimum depth of "+
		"binary tree is : " + tree.minimumDepth());
	}
}

Output

Given a binary tree, find its minimum depth

Example 4

using System;
// Creating a C# program or implementation to search and explore the minimum depth of a given binary tree. 
/* Creating a new class that will have the left and right child of the current node and the key value. */
public class __nod
{
	public int record;
	public __nod Lft, Rt;
	public __nod(int item)
	{
		record = item;
		Lft = Rt = NILL;
	}
}
public class BinaryTree
{
	//Creating root for the BST
	public __nod root;


	public virtual int minimumDepth()
	{
		return minimumDepth(root);
	}
/* creating a new function that will give us the minimum depth*/
	public virtual int minimumDepth(__nod root)
	{
	// The case named corner shouldn't be called unless and until the code is called on the root, which is NILL. 
		if (root == NILL)
		{
			return 0;
		}
// The base case of the leaf node has a height equivalent to 1.
		if (root.Lft == NILL && root.Rt == NILL)
		{
			return 1;
		}
//If we find out the left subtree is not NILL; then we have to recursion for the right subtree. 


		if (root.Lft == NILL)
		{
			return minimumDepth(root.Rt) + 1;
		}
//If we find out the right subtree is not NILL; then we have to recursion for the left subtree. 
		if (root.Rt == NILL)
		{
			return minimumDepth(root.Lft) + 1;
		}


		return Math.Min(minimumDepth(root.Lft), minimumDepth(root.Rt)) + 1;
	}


	// writing the main code
	public static void Main(string[] args)
	{
		BinaryTree tree = new BinaryTree();
		tree.root = new __nod(1);
		tree.root.Lft = new __nod(2);
		tree.root.Rt = new __nod(3);
		tree.root.Lft.Lft = new __nod(4);
		tree.root.Lft.Rt = new __nod(5);


		Console.WriteLine("The minimum depth of binary tree is : " + tree.minimumDepth());
	}
}

Output

Given a binary tree, find its minimum depth

Related Topics

Polish Notation in Data Structures

Arithmetic Expression: An arithmetic expression is defined as several operands or data items combined using several operators. For example; a+b*(c-d) is an expression. Operands: Operands represent the data in an expression...

2 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Difference Between Linear and Non Linear Data Structures

Data Structure A data structure is a data object together with the relationships between the instances and the individual elements that compose an instance. These relationships are defined by the operations...

5 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

Operations of B++ tree

Insertion When we discuss the insertion operation in the B++ tree, this operation helps us in pushing a new element in the tree at any given place. In this case, the...

17 minutes read.

Lowest common ancestor in a binary search tree

Suppose you have given two values of nodes in a binary search tree. You have to find out the lowest common ancestor between the nodes. Let’s take an example tree- For the...

4 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 minutes read.

Fundamental of Algorithms

An algorithm is a part of any programming solution or coding. If we have to make a solution then first we have to think of a clear idea about the...

13 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Construction of B tree in Data Structure

A B-tree is a type of balanced tree data structure that is commonly used in file systems and databases to improve the efficiency of search, insert, and delete operations. The structure...

4 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

Merge Sort

Merge Sort is one of the most widely used sorting algorithms, and it is based on the Divide and Conquer principle. A problem is subdivided into multiple sub-problems in this method....

8 minutes read.

Count pairs from two linked lists whose sum is equal to a given value

Count pairs from two linked lists whose sum is equal to a given value In this problem, we have given two linked lists of size n1 and n2 with distinct elements...

4 minutes read.

Deletion in B+ Tree

Make a search for the leaf node that containing the key value by taking the value in a key value. If the required key value is found, then it will remove...

6 minutes read.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.