-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path968.cpp
More file actions
40 lines (40 loc) · 1.12 KB
/
968.cpp
File metadata and controls
40 lines (40 loc) · 1.12 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
/**
* 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) {}
* };
*/
class Solution {
public:
int res = 0;
int dfs(TreeNode* node) {
// 0: is a leave
// 1: set camera
// 2: is not leave but is covered
if (node == nullptr) return 2;
int left = dfs(node->left);
int right = dfs(node->right);
// if any of its child is a leave => set
if (left == 0 || right == 0) {
res++;
return 1;
}
// if any of its child has camera => not set
if (left == 1 || right == 1) {
return 2;
}
// left == right == 2
// both of its children have been cover or do not exist
return 0;
}
int minCameraCover(TreeNode* root) {
int state = dfs(root);
if (state == 0) res++;
return res;
}
};