-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path111.minDepth.cpp
More file actions
44 lines (38 loc) · 946 Bytes
/
111.minDepth.cpp
File metadata and controls
44 lines (38 loc) · 946 Bytes
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> q;
queue<int> l;
int level;
if (root == NULL) return 0;
q.push(root);
l.push(1);
while(!q.empty()) {
TreeNode* root = q.front();
level = l.front();
q.pop();
l.pop();
if (root->left == NULL && root->right == NULL) {
return level;
}
if (root->left != NULL) {
q.push(root->left);
l.push(level+1);
}
if (root->right != NULL) {
q.push(root->right);
l.push(level+1);
}
}
return level;
}
};