-
Notifications
You must be signed in to change notification settings - Fork 0
/
LC111_minDepth.cpp
43 lines (42 loc) · 1.25 KB
/
LC111_minDepth.cpp
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
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) {}
};
class Solution {
public:
// RC
int minDepth(TreeNode* root) {
// Postorder
if (!root) return 0;
int left = minDepth(root -> left);
int right = minDepth(root -> right);
if (left == 0 && right == 0) return 1;
if (left == 0) return right + 1;
if (right == 0) return left + 1;
return min(left, right) + 1;
}
// IT
int minDepth(TreeNode* root) {
// Levelorder
queue<TreeNode*> que;
if(root == nullptr) return 0;
que.push(root);
int min = 0;
while(!que.empty()){
int size = que.size();
while (size --){
TreeNode* node = que.front();
que.pop();
if(!node -> left && !node -> right) return min + 1;
if(node -> left) que.push(node -> left);
if(node -> right) que.push(node -> right);
}
++ min;
}
return min;
}
};