forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlowest-common-ancestor-of-a-binary-tree-iv.cpp
69 lines (65 loc) · 2.29 KB
/
lowest-common-ancestor-of-a-binary-tree-iv.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
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) {
return iter_dfs(root, unordered_set<TreeNode*>(cbegin(nodes), cend(nodes)));
}
private:
TreeNode* iter_dfs(TreeNode* node, const unordered_set<TreeNode*>& lookup) {
TreeNode *result = nullptr;
vector<tuple<int, TreeNode *, unique_ptr<TreeNode *>, unique_ptr<TreeNode *>, TreeNode **>> stk;
stk.emplace_back(1, node, nullptr, nullptr, &result);
while (!empty(stk)) {
const auto [step, node, ret1, ret2, ret] = move(stk.back()); stk.pop_back();
if (step == 1) {
if (!node || lookup.count(node)) {
*ret = node;
continue;
}
auto ret1 = make_unique<TreeNode *>(), ret2 = make_unique<TreeNode *>();
auto p1 = ret1.get(), p2 = ret2.get();
stk.emplace_back(2, node, move(ret1), move(ret2), ret);
stk.emplace_back(1, node->right, nullptr, nullptr, p2);
stk.emplace_back(1, node->left, nullptr, nullptr, p1);
} else if (step == 2) {
if (*ret1 && *ret2) {
*ret = node;
} else {
*ret = *ret1 ? *ret1 : *ret2;
}
}
}
return result;
}
};
// Time: O(n)
// Space: O(h)
class Solution2 {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) {
return dfs(root, unordered_set<TreeNode*>(cbegin(nodes), cend(nodes)));
}
private:
TreeNode* dfs(TreeNode* node, const unordered_set<TreeNode*>& lookup) {
if (!node || lookup.count(node)) {
return node;
}
auto left = dfs(node->left, lookup), right = dfs(node->right, lookup);
if (left && right) {
return node;
}
return left ? left : right;
}
};