-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111.minimum-depth-of-binary-tree.cpp
More file actions
52 lines (49 loc) · 1.23 KB
/
111.minimum-depth-of-binary-tree.cpp
File metadata and controls
52 lines (49 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode id=111 lang=cpp
*
* [111] Minimum Depth of Binary Tree
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
queue<TreeNode*> Q;
Q.push(root);
int i = 0;
while (!Q.empty()) {
i++;
int k = Q.size();
for (int j = 0; j < k; ++j) {
TreeNode* rt = Q.front();
Q.pop();
if (rt->left) Q.push(rt->left);
if (rt->right) Q.push(rt->right);
if (rt->left == NULL && rt->right == NULL) return i;
}
}
return -1; //For the compiler thing. The code never runs here.
}
};
// Accepted
// 52/52 cases passed (237 ms)
// Your runtime beats 99.27 % of cpp submissions
// Your memory usage beats 99.31 % of cpp submissions (144.4 MB)
// @lc code=end