-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path968. Binary Tree Cameras.java
37 lines (37 loc) · 1.14 KB
/
968. Binary Tree Cameras.java
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
class Solution {
int minimumCameras = 0;
Set<TreeNode> covered = new HashSet<>(); // store the temp visited
// Binary Tree Cameras | Live Coding with Explanation | Leetcode - 968
// https://www.youtube.com/watch?v=2Gh5WPjAgJk
public int minCameraCover(TreeNode root) {
minimumCameras = 0;
covered.add(null);
minCameraCoverHelper(root, null);
return minimumCameras;
}
public void minCameraCoverHelper(TreeNode root, TreeNode parent) {
if (root == null) {
return;
}
// faith
minCameraCoverHelper(root.left, root);
minCameraCoverHelper(root.right, root);
// my work
if (parent == null && !covered.contains(root)
|| !covered.contains(root.left)
|| !covered.contains(root.right)) {
minimumCameras++;
covered.add(parent);
covered.add(root);
covered.add(root.left);
covered.add(root.right);
}
}
}