×

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

Linear Search

Searching: In the data structure, searching is the process in which an element is searched in a list that satisfies one or more than one condition. Types of searching There are two...

4 minutes read.

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Delete the Middle element of the Linked List in C

Delete the Middle element of the Linked List in C This article has given a singly linked list and will delete the middle element of the given linked list. Example:  The given...

3 minutes read.

Deletion Operation of the binary search tree in C++ language

A typical binary search tree implements some order to carry out the arrangements. As the name suggests, each parent node should have at most two children. The main rule in...

4 minutes read.

B+ Tree Program in Q language

A B+ tree is just an improvised version of a self-balancing and well-maintained tree in which all the key values that hold valuable information is present at the bottom, which...

9 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

5 minutes read.

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.

LCA of binary tree

Implementation //Writing a program to find the lowest common factor in a given binary search tree. #include <iostream> #include <vector> using namespace std; // the very first step is to create a binary tree. struct __nod { int...

8 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.

Array vs Linked List: Data Structure

Data structure: Difference Between Array and Linked List What is Array? An array is a linear data structure that can store similar data items for further processing. The similar data items...

3 minutes read.

What is the difference between Tree and Graph

We usually use a diverse range of data structure to store our data and information. To store them in a more sequential manner and to access them easily, we use...

4 minutes read.

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2). Example:  *+EF-GH (Infix:...

2 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Buffer overflow attack with examples

You have undoubtedly faced the term buffer overflow in your programming journey. Many times it occurs when we try to run a piece of code with user input, but it...

4 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.

Binary Tree Uses

A binary tree is a tree data structure containing hubs with at most two children for instance a right and left child. The node at the top is insinuated as the...

3 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.