-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path968.cpp
More file actions
37 lines (31 loc) · 692 Bytes
/
Copy path968.cpp
File metadata and controls
37 lines (31 loc) · 692 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
#include <bits/stdc++.h>
using namespace std;
struct Status
{
int a, b, c;
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
int minCameraCover(TreeNode* root) {
auto [a, b, c] = dfs(root);
return b;
}
Status dfs(TreeNode* root) {
if (root == nullptr) {
return {INT_MAX/2, 0, 0};
}
auto [la, lb, lc] = dfs(root->left);
auto [ra, rb, rc] = dfs(root->right);
int a = lc + rc + 1;
int b = min(a, min(la+rb, ra+lb)); //
int c = min(a, lb+rb);
return {a, b, c};
}
};