Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Minimum Depth of Binary Tree problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Minimum Depth of Binary Tree problem solution, we have given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Leetcode Minimum Depth of Binary Tree problem solution

Leetcode Minimum Depth of Binary Tree problem solution in Python.

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        if (root == None):
            return 0
        self.height = 1<<30
        def depth(node,height):
            if (node):
                if (node.left == None) and (node.right == None):
                    self.height = min(self.height,height)
                if (node.left):
                    depth(node.left,height+1)
                if (node.right):
                    depth(node.right,height + 1)
        depth(root,1)
        return self.height

Minimum Depth of Binary Tree problem solution in Java.

public int minDepth(TreeNode root) {
	if (root == null) return 0;
	if (root.left == null) return 1 + minDepth(root.right);
	if (root.right == null) return 1 + minDepth(root.left);
	return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}

Problem solution in C++.

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root) return 0;
        int left_depth  = minDepth(root->left);
        int right_depth = minDepth(root->right);
        if(left_depth == 0 || right_depth == 0)
            return left_depth + right_depth + 1;
        return min(left_depth, right_depth)+1;
    }

};

Problem solution in C.

int minDepth(struct TreeNode* root){
   if(root==NULL) return 0;
   struct TreeNode* arr[100000];
    int head=1, tail=-1;
    arr[0] = root;
    root->val=1;
    while (head!=tail){
		tail++;
        if(arr[tail]->left ==NULL && arr[tail]->right ==NULL) break;
        if(arr[tail]->left !=NULL){
            arr[head++] = arr[tail]->left;
            arr[tail]->left->val = arr[tail]->val+1;
        }
        if(arr[tail]->right !=NULL){
             arr[head++] = arr[tail]->right;
            arr[tail]->right->val = arr[tail]->val+1;
        }
    }
    return arr[tail]->val;
    
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes