Solved LeetCode Problem 111: Minimum Depth of Binary Tree with Java

✅ Day 34 of #100DaysOfCode Challenge 📘 LeetCode Problem 111: Minimum Depth of Binary Tree 🧩 Problem Statement:  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. Example: Input: root = [3,9,20,null,null,15,7] Output: 2 Explanation:  The shortest path is 3 → 9, so the minimum depth = 2. 💡 Approach (Simple Recursive): If the tree is empty → return 0. Recursively find the left and right subtree depths. If one side is missing, the depth = 1 + depth of the other side. Otherwise, take 1 + min(leftDepth, rightDepth). 💻 Java Code (Easy Version): class Solution {   public int minDepth(TreeNode root) {     if (root == null)       return 0;     int left = minDepth(root.left);     int right = minDepth(root.right);     // If one child is missing, take the non-null side     if (left == 0 || right == 0)       return 1 + left + right;     return 1 + Math.min(left, right);   } } ⚙ Complexity: ⏱ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Growing one problem at a time 🌱  #Day34 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge #ProblemSolving

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories