-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
16 changes: 16 additions & 0 deletions
16
src/main/java/io/dksifoua/leetcode/minimumdepthofbinarytree/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/test/java/io/dksifoua/leetcode/maximumdepthofbinarytree/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/test/java/io/dksifoua/leetcode/minimumdepthofbinarytree/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }))); | ||
} | ||
} |