Skip to content

Commit

Permalink
Minimum Depth of Binary Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
dksifoua committed Sep 3, 2024
1 parent a0c5865 commit 094a515
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
| 0076 | Hard | Minimum Window Substring | Hash Table, String, Sliding Window | [solution](./docs/0076-Mininum-Window-Substring.md) |
| 0084 | Hard | Largest Rectangle in Histogram | Array, Stack, Monotonic Stack | [solution](./docs/0084-Largest-Rectangle-In-Histogram.md) |
| 0104 | Easy | Maximum Depth of Binary Tree | Tree, Depth-First Search, Breadth-First Search, Binary Tree | [solution](./docs/0104-Maximum-Depth-of-Binary-Tree.md) |
| 0111 | Easy | Minimum Depth of Binary Tree | Tree, Depth-First Search, Breadth-First Search, Binary Tree | [solution](./docs/0111-Minimum-Depth-of-Binary-Tree.md) |
| 0121 | Easy | Best Time to Buy and Sell Stock | Array | [solution](./docs/0121-Best-Time-to-Buy-and-Sell-Stock.md) |
| 0125 | Easy | Valid Palindrome | String, Two Pointers | [solution](./docs/0125-Valid-Palindrome.md) |
| 0128 | Medium | Longest Consecutive Sequence | Array, HashSet | [solution](./docs/0128-Longest-Consecutive-Sequence.md) |
Expand Down
36 changes: 36 additions & 0 deletions docs/0111-Minimum-Depth-of-Binary-Tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)

## Intuition

The minimum depth of a binary tree is the length of the shortest path from the root to a leaf. Unlike the maximum depth,
which considers all paths, the minimum depth stops at the first leaf node it encounters. A recursive approach allows us
to explore each node’s depth until we reach the closest leaf, ensuring the minimum depth calculation.

## Approach

1. **Base Case:** If the root is null, the depth is `0` because there are no nodes.
2. **Handle Nodes with One Child:** If either the left or right child is null, it means that the node has only one
child. In this case, the minimum depth must come from the non-null child, so we use `Math.max()` to find the depth of
the non-null side.
3. **Recursive Minimum Depth Calculation:**
- If both left and right children exist, compute the minimum depth for both subtrees and take the smaller one
with `Math.min()`.
- Add `1` to account for the current node.
4. **Return the Result:** The recursion will return the minimum depth by following the shortest path to a leaf node.

## Complexity

- **Time Complexity: `O(n)**, where `n` is the number of nodes in the tree. Each node is visited once to calculate its
depth.
- **Space Complexity: `O(h)`, where `h` is the height of the tree. This represents the space used by the call stack
during recursion. In the worst case (a skewed tree), `h = n` , and in the best case (a balanced tree), `h = log(n)`.

## Code

- [Java](../src/main/java/io/dksifoua/leetcode/minimumdepthofbinarytree/Solution.java)

## Summary

This recursive solution calculates the minimum depth by prioritizing the shortest path to a leaf node. By handling cases
where nodes have only one child separately, we ensure the correct minimum depth calculation, making this approach
efficient with O(n) time complexity.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.dksifoua.leetcode.minimumdepthofbinarytree;

import io.dksifoua.leetcode.utils.TreeNode;

public class Solution {

public int minDepth(TreeNode root) {
if (root == null) return 0;

if (root.getLeft() == null || root.getRight() == null) {
return 1 + Math.max(this.minDepth(root.getLeft()), this.minDepth(root.getRight()));
}

return 1 + Math.min(this.minDepth(root.getLeft()), this.minDepth(root.getRight()));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.dksifoua.leetcode.maximumdepthofbinarytree;

import io.dksifoua.leetcode.utils.TreeNode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.dksifoua.leetcode.minimumdepthofbinarytree;

import io.dksifoua.leetcode.utils.TreeNode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class SolutionTest {

static final Solution solution = new Solution();

@Test
void test1() {
assertEquals(2, solution.minDepth(TreeNode.build(new Integer[] { 3, 9, 20, null, null, 15, 7 })));
}

@Test
void test2() {
assertEquals(5, solution.minDepth(TreeNode.build(new Integer[] { 2, null, 3, null, 4, null, 5, null, 6 })));
}
}

0 comments on commit 094a515

Please sign in to comment.