-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeCameras.cpp
More file actions
31 lines (26 loc) · 843 Bytes
/
BinaryTreeCameras.cpp
File metadata and controls
31 lines (26 loc) · 843 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
#include <bits/stdc++.h>
#include "TreeNode.h"
using namespace std;
void solve(TreeNode* root, TreeNode* parent, unordered_set<TreeNode*>& visited, int& ans) {
if (!root) return;
solve(root->left, root, visited, ans);
solve(root->right, root, visited, ans);
if ((parent == NULL && visited.count(root) == 0) || visited.count(root->left) == 0 || visited.count(root->right) == 0) {
ans++;
visited.insert(root);
visited.insert(parent);
visited.insert(root->left);
visited.insert(root->right);
}
}
int minCameraCover(TreeNode* root) {
unordered_set<TreeNode*> visited;
visited.insert(NULL);
int ans = 0;
solve(root, NULL, visited, ans);
return ans;
}
int main() {
TreeNode* root = takeInput();
cout << minCameraCover(root);
}