Skip to content

Commit bed3aac

Browse files
🎋 Day 22
1 parent f1f5079 commit bed3aac

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
## Week 4 🚧
3939

40-
Coming Soon...
40+
1. [Minimum Depth of Binary Tree](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/562/week-4-october-22nd-october-28th/3504/) ➡️ [CPP Solution](Week4/minDepth.cpp)
4141

4242
## Week 5 🚧
4343

Week4/minDepth.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int minDepth(TreeNode* root) {
15+
if(root == NULL) return 0;
16+
queue<TreeNode*> q;
17+
18+
q.push(root);
19+
int level = 1;
20+
21+
while(!q.empty()) {
22+
int n = q.size();
23+
24+
while(n--) {
25+
TreeNode* current = q.front();
26+
q.pop();
27+
28+
if(current->left == NULL && current->right == NULL)
29+
return level;
30+
31+
if(current->left)
32+
q.push(current->left);
33+
34+
if(current->right)
35+
q.push(current->right);
36+
}
37+
38+
level++;
39+
}
40+
41+
return level;
42+
}
43+
};

0 commit comments

Comments
 (0)