-
-
Notifications
You must be signed in to change notification settings - Fork 155
Added solution for leetcode problem 0111 #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
80f3ecd
Merge pull request #3 from CodeHarborHub/main
Ayushmaanagarwal1211 299cf99
Added solution for lt problem 111
Ayushmaanagarwal1211 d12904c
update
Ayushmaanagarwal1211 b121a92
Update 0111-minimum-depth.md
Ayushmaanagarwal1211 78ddf89
Update 0111-minimum-depth.md
Ayushmaanagarwal1211 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md
This file contains hidden or 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,133 @@ | ||
--- | ||
id: minimum-depth-of-binary-tree | ||
title: Minimum Depth of Binary Tree | ||
sidebar_label: 0111-Minimum Depth Of Binary tree | ||
tags: | ||
- Java | ||
- Python | ||
- C++ | ||
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." | ||
--- | ||
|
||
## Problem Description | ||
|
||
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. | ||
|
||
### Examples | ||
|
||
**Example 1:** | ||
|
||
 | ||
``` | ||
Input: root = [3,9,20,null,null,15,7] | ||
Output: 2 | ||
``` | ||
|
||
|
||
### Constraints | ||
|
||
- The number of nodes in the tree is in the range [0, 105]. | ||
- -100 <= Node.val <= 100 | ||
|
||
--- | ||
|
||
## Solution for Binary Tree Problem | ||
|
||
### Intuition And Approach | ||
|
||
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. | ||
|
||
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. | ||
|
||
<Tabs> | ||
<tabItem value="Recursive" label="Recursive"> | ||
|
||
|
||
#### Code in Different Languages | ||
|
||
<Tabs> | ||
<TabItem value="Java" label="Java" default> | ||
<SolutionAuthor name="@Vipullakum007"/> | ||
```java | ||
class Solution { | ||
public int minDepth(TreeNode root) { | ||
if (root == null) return 0; | ||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.offer(root); | ||
int depth = 1; | ||
while (!queue.isEmpty()) { | ||
int levelSize = queue.size(); | ||
for (int i = 0; i < levelSize; i++) { | ||
TreeNode node = queue.poll(); | ||
if (node.left == null && node.right == null) return depth; | ||
if (node.left != null) queue.offer(node.left); | ||
if (node.right != null) queue.offer(node.right); | ||
} | ||
depth++; | ||
} | ||
return depth; | ||
} | ||
} | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="Python" label="Python"> | ||
<SolutionAuthor name="@Vipullakum007"/> | ||
```python | ||
class Solution: | ||
def minDepth(self, root): | ||
if not root: | ||
return 0 | ||
queue = collections.deque([(root, 1)]) | ||
while queue: | ||
node, depth = queue.popleft() | ||
if not node.left and not node.right: | ||
return depth | ||
if node.left: | ||
queue.append((node.left, depth + 1)) | ||
if node.right: | ||
queue.append((node.right, depth + 1)) | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="C++" label="C++"> | ||
<SolutionAuthor name="@Vipullakum007"/> | ||
```cpp | ||
class Solution { | ||
public: | ||
int minDepth(TreeNode* root) { | ||
if (!root) return 0; | ||
queue<TreeNode*> q; | ||
q.push(root); | ||
int depth = 1; | ||
while (!q.empty()) { | ||
int levelSize = q.size(); | ||
for (int i = 0; i < levelSize; ++i) { | ||
TreeNode* node = q.front(); | ||
q.pop(); | ||
if (!node->left && !node->right) return depth; // Leaf node found | ||
if (node->left) q.push(node->left); | ||
if (node->right) q.push(node->right); | ||
} | ||
++depth; | ||
} | ||
return depth; | ||
} | ||
}; | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
#### Complexity Analysis | ||
|
||
- Time Complexity: $O(n)$ where n is the number of nodes in the binary tree. | ||
- Space Complexity: $O(h)$ where h is the height of the binary tree. | ||
|
||
</tabItem> | ||
</Tabs> | ||
|
||
|
||
--- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.