forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-flips-in-binary-tree-to-get-result.cpp
91 lines (85 loc) · 3.32 KB
/
minimum-flips-in-binary-tree-to-get-result.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Time: O(n)
// Space: O(h)
// tree dp with stack
class Solution {
public:
int minimumFlips(TreeNode* root, bool result) {
static const int INF = numeric_limits<int>::max();
static unordered_map<int, function<int(int, int)>> OP = {
{2, [](int x, int y) { return x | y; }},
{3, [](int x, int y) { return x & y; }},
{4, [](int x, int y) { return x ^ y; }},
{5, [](int x, int y) { return x != -1 ? !x : !y; }},
};
const auto& iter_dfs = [&]() {
using RET = unordered_map<int, int>;
RET ret;
vector<tuple<int, TreeNode *, shared_ptr<RET>, shared_ptr<RET>, RET *>> stk;
stk.emplace_back(1, root, nullptr, nullptr, &ret);
while (!empty(stk)) {
auto [step, node, ret1, ret2, ret] = stk.back(); stk.pop_back();
if (step == 1) {
if (!node) {
(*ret)[-1] = 0; // null object pattern
continue;
}
if (node->left == node->right) {
(*ret)[true] = node->val ^ 1;
(*ret)[false] = node->val ^ 0;
continue;
}
auto ret1 = make_shared<RET>(), ret2 = make_shared<RET>();
stk.emplace_back(2, node, ret1, ret2, ret);
stk.emplace_back(1, node->right, nullptr, nullptr, ret2.get());
stk.emplace_back(1, node->left, nullptr, nullptr, ret1.get());
} else if (step == 2) {
for (const auto& [k1, v1] : *ret1) {
for (const auto& [k2, v2] : *ret2) {
const int nk = OP[node->val](k1, k2);
(*ret)[nk] = min(ret->count(nk) ? (*ret)[nk] : INF, v1 + v2);
}
}
}
}
return ret[result];
};
return iter_dfs();
}
};
// Time: O(n)
// Space: O(h)
// tree dp with recursion
class Solution2 {
public:
int minimumFlips(TreeNode* root, bool result) {
static const int INF = numeric_limits<int>::max();
static unordered_map<int, function<int(int, int)>> OP = {
{2, [](int x, int y) { return x | y; }},
{3, [](int x, int y) { return x & y; }},
{4, [](int x, int y) { return x ^ y; }},
{5, [](int x, int y) { return x != -1 ? !x : !y; }},
};
function<unordered_map<int, int> (TreeNode*)> dfs = [&](TreeNode *node) {
unordered_map<int, int> dp;
if (!node) {
dp[-1] = 0; // null object pattern
return dp;
}
if (node->left == node->right) {
dp[true] = node->val ^ 1;
dp[false] = node->val ^ 0;
return dp;
}
const auto& left = dfs(node->left);
const auto& right = dfs(node->right);
for (const auto& [k1, v1] : left) {
for (const auto& [k2, v2] : right) {
const int nk = OP[node->val](k1, k2);
dp[nk] = min(dp.count(nk) ? dp[nk] : INF, v1 + v2);
}
}
return dp;
};
return dfs(root)[result];
}
};