-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-968-Binary-Tree-Cameras.java
46 lines (38 loc) · 1.23 KB
/
LeetCode-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
38
39
40
41
42
43
44
45
46
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/*
https://leetcode.com/problems/binary-tree-cameras/discuss/211966/Super-Clean-Java-solution-beat-100-DFS-O(n)-time-complexity
Time complexity is O(n), space complexity is O(logn) considering system stack space.
https://leetcode.com/problems/binary-tree-cameras/discuss/211180/JavaC%2B%2BPython-Greedy-DFS
*/
private int NONE = 0;
private int COVERED_NO_CAMERA = 1;
private int COVERED_CAMERA = 2;
int res = 0;
public int minCameraCover(TreeNode root) {
int top = dfs(root);
return res + (top == NONE ? 1 : 0);
}
private int dfs(TreeNode node) {
if (node == null) return COVERED_NO_CAMERA;
int left = dfs(node.left);
int right = dfs(node.right);
if (left == COVERED_NO_CAMERA && right == COVERED_NO_CAMERA) {
return NONE;
}
if (left == NONE || right == NONE) {
// means leaf
res++;
return COVERED_CAMERA;
}
return COVERED_NO_CAMERA;
}
}