forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum_Depth_of_Binary_Tree.cc
43 lines (43 loc) · 1.16 KB
/
Minimum_Depth_of_Binary_Tree.cc
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (root == NULL) {
return 0;
}
queue<TreeNode *> que;
queue<int> dep_que;
que.push(root);
dep_que.push(1);
TreeNode *cntNode;
int cntDepth = -1;
while (!que.empty()) {
cntNode = que.front();
que.pop();
cntDepth = dep_que.front();
dep_que.pop();
if (cntNode->left == NULL && cntNode->right == NULL) {
return cntDepth;
}
if (cntNode->left) {
que.push(cntNode->left);
dep_que.push(cntDepth + 1);
}
if (cntNode->right) {
que.push(cntNode->right);
dep_que.push(cntDepth + 1);
}
}
return cntDepth;
}
};