-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
47 lines (46 loc) · 1.3 KB
/
Solution.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
47
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minimumFlips(TreeNode root, boolean result) {
return dfs(root)[result ? 1 : 0];
}
private int[] dfs(TreeNode root) {
if (root == null) {
return new int[] {1 << 30, 1 << 30};
}
int x = root.val;
if (x < 2) {
return new int[] {x, x ^ 1};
}
var l = dfs(root.left);
var r = dfs(root.right);
int a = 0, b = 0;
if (x == 2) {
a = l[0] + r[0];
b = Math.min(l[0] + r[1], Math.min(l[1] + r[0], l[1] + r[1]));
} else if (x == 3) {
a = Math.min(l[0] + r[0], Math.min(l[0] + r[1], l[1] + r[0]));
b = l[1] + r[1];
} else if (x == 4) {
a = Math.min(l[0] + r[0], l[1] + r[1]);
b = Math.min(l[0] + r[1], l[1] + r[0]);
} else {
a = Math.min(l[1], r[1]);
b = Math.min(l[0], r[0]);
}
return new int[] {a, b};
}
}