forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0968.cpp
More file actions
49 lines (37 loc) · 1001 Bytes
/
0968.cpp
File metadata and controls
49 lines (37 loc) · 1001 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 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 {
int cams;
int dfs(TreeNode* root) {
if (!root)
return 2;
int l = dfs(root -> left);
int r = dfs(root -> right);
if (l == 0 || r == 0) {
cams++;
return 1;
}
if (l == 1 || r == 1)
return 2;
else
return 0;
}
public:
int minCameraCover(TreeNode* root) {
cams = 0;
int check = dfs(root);
if (check == 0)
return cams + 1;
else
return cams;
}
};