-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111_minDepth.cpp
More file actions
35 lines (35 loc) · 829 Bytes
/
111_minDepth.cpp
File metadata and controls
35 lines (35 loc) · 829 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
/**
* 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) {
if(root == NULL)
return 0;
queue<TreeNode*> q;
q.push(root);
TreeNode *last = q.back(),*p;
int lev = 1;
while(!q.empty()){
p = q.front();
q.pop();
if(p->left == NULL&&p->right == NULL)
return lev;
if(p->left!=NULL)
q.push(p->left);
if(p->right !=NULL)
q.push(p->right);
if(p == last){
lev++;
last = q.back();
}
}
return lev;
}
};