-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
height-of-binary-tree-after-subtree-removal-queries.cpp
76 lines (72 loc) · 2.59 KB
/
height-of-binary-tree-after-subtree-removal-queries.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
// Time: O(n)
// Space: O(n)
// iterative dfs
class Solution {
public:
vector<int> treeQueries(TreeNode* root, vector<int>& queries) {
unordered_map<int, pair<int, int>> top;
unordered_map<int, int> depth, height;
const auto& iter_dfs = [&]() {
vector<tuple<int, TreeNode *, int>> stk = {{1, root, 0}};
while (!empty(stk)) {
const auto [step, curr, d] = stk.back(); stk.pop_back();
if (step == 1) {
if (!curr) {
continue;
}
stk.emplace_back(2, curr, d);
stk.emplace_back(1, curr->right, d + 1);
stk.emplace_back(1, curr->left, d + 1);
} else if (step == 2) {
const int h = 1 + max(curr->left ? height[curr->left->val] : 0,
curr->right ? height[curr->right->val] : 0);
if (h > top[d].first) {
top[d].second = top[d].first;
top[d].first = h;
} else if (h > top[d].second) {
top[d].second = h;
}
depth[curr->val] = d;
height[curr->val] = h;
}
}
};
iter_dfs();
vector<int> result;
for (const auto& q: queries) {
result.emplace_back((depth[q] - 1) + (height[q] != top[depth[q]].first ? top[depth[q]].first : top[depth[q]].second));
}
return result;
}
};
// Time: O(n)
// Space: O(n)
// dfs
class Solution2 {
public:
vector<int> treeQueries(TreeNode* root, vector<int>& queries) {
unordered_map<int, pair<int, int>> top;
unordered_map<int, int> depth, height;
const function<int(TreeNode *curr, int d)> dfs = [&](const auto& curr, const auto& d) {
if (!curr) {
return 0;
}
const int h = 1 + max(dfs(curr->left, d + 1), dfs(curr->right, d + 1));
if (h > top[d].first) {
top[d].second = top[d].first;
top[d].first = h;
} else if (h > top[d].second) {
top[d].second = h;
}
depth[curr->val] = d;
height[curr->val] = h;
return h;
};
dfs(root, 0);
vector<int> result;
for (const auto& q: queries) {
result.emplace_back((depth[q] - 1) + (height[q] != top[depth[q]].first ? top[depth[q]].first : top[depth[q]].second));
}
return result;
}
};