Skip to content

Commit 299cf99

Browse files
Added solution for lt problem 111
1 parent 80f3ecd commit 299cf99

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
id: minimum-depth-of-binary-tree
3+
title: Minimum Depth of Binary Tree
4+
sidebar_label: 0111-Minimum Depth
5+
tags:
6+
- Java
7+
- Python
8+
- C++
9+
description: "Find the minimum depth of a binary tree, which is the number of nodes along the shortest path from the root node down to the nearest leaf node."
10+
---
11+
12+
## Problem Description
13+
14+
Given a binary tree, find its minimum depth.
15+
16+
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
![LeetCode Problem - Binary Tree](https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg)
23+
```
24+
Input: root = [3,9,20,null,null,15,7]
25+
Output: 2
26+
```
27+
28+
29+
### Constraints
30+
31+
- `The number of nodes in the tree is in the range [0, 105].`
32+
- `-100 <= Node.val <= 100`
33+
34+
---
35+
36+
## Solution for Binary Tree Problem
37+
38+
### Intuition And Approach
39+
40+
To find the minimum depth of a binary tree, we can perform a depth-first search (DFS) or a breadth-first search (BFS). Here, we will use BFS for simplicity.
41+
42+
Breadth-First Search (BFS): Traverse the tree level by level, stopping as soon as we encounter a leaf node. The depth of the first leaf node encountered will be the minimum depth of the tree.
43+
44+
<Tabs>
45+
<tabItem value="Recursive" label="Recursive">
46+
47+
48+
#### Code in Different Languages
49+
50+
<Tabs>
51+
<TabItem value="Java" label="Java" default>
52+
<SolutionAuthor name="@Vipullakum007"/>
53+
```java
54+
class Solution {
55+
public int minDepth(TreeNode root) {
56+
if (root == null) return 0;
57+
Queue<TreeNode> queue = new LinkedList<>();
58+
queue.offer(root);
59+
int depth = 1;
60+
while (!queue.isEmpty()) {
61+
int levelSize = queue.size();
62+
for (int i = 0; i < levelSize; i++) {
63+
TreeNode node = queue.poll();
64+
if (node.left == null && node.right == null) return depth;
65+
if (node.left != null) queue.offer(node.left);
66+
if (node.right != null) queue.offer(node.right);
67+
}
68+
depth++;
69+
}
70+
return depth;
71+
}
72+
}
73+
```
74+
75+
</TabItem>
76+
<TabItem value="Python" label="Python">
77+
<SolutionAuthor name="@Vipullakum007"/>
78+
```python
79+
class Solution:
80+
def minDepth(self, root):
81+
if not root:
82+
return 0
83+
queue = collections.deque([(root, 1)])
84+
while queue:
85+
node, depth = queue.popleft()
86+
if not node.left and not node.right:
87+
return depth
88+
if node.left:
89+
queue.append((node.left, depth + 1))
90+
if node.right:
91+
queue.append((node.right, depth + 1))
92+
```
93+
94+
</TabItem>
95+
<TabItem value="C++" label="C++">
96+
<SolutionAuthor name="@Vipullakum007"/>
97+
```cpp
98+
class Solution {
99+
public:
100+
int minDepth(TreeNode* root) {
101+
if (!root) return 0;
102+
queue<TreeNode*> q;
103+
q.push(root);
104+
int depth = 1;
105+
while (!q.empty()) {
106+
int levelSize = q.size();
107+
for (int i = 0; i < levelSize; ++i) {
108+
TreeNode* node = q.front();
109+
q.pop();
110+
if (!node->left && !node->right) return depth; // Leaf node found
111+
if (node->left) q.push(node->left);
112+
if (node->right) q.push(node->right);
113+
}
114+
++depth;
115+
}
116+
return depth;
117+
}
118+
};
119+
```
120+
121+
</TabItem>
122+
</Tabs>
123+
124+
#### Complexity Analysis
125+
126+
- Time Complexity: O(n) where n is the number of nodes in the binary tree.
127+
- Space Complexity: O(h) where h is the height of the binary tree.
128+
129+
</tabItem>
130+
</Tabs>
131+
132+
133+
---

0 commit comments

Comments
 (0)