forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lowest-common-ancestor-of-a-binary-tree-ii.cpp
71 lines (67 loc) · 2.14 KB
/
lowest-common-ancestor-of-a-binary-tree-ii.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
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
return iter_dfs(root, p, q);
}
private:
TreeNode *iter_dfs(TreeNode* node, TreeNode* p, TreeNode* q) {
TreeNode *result = nullptr;
int ret = 0;
vector<tuple<int, TreeNode *, unique_ptr<int>, unique_ptr<int>, int*>> stk;
stk.emplace_back(1, node, nullptr, nullptr, &ret);
while (!empty(stk)) {
const auto [step, node, ret1, ret2, ret] = move(stk.back()); stk.pop_back();
if (step == 1) {
if (!node) {
continue;
}
auto ret1 = make_unique<int>(), ret2 = make_unique<int>();
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) {
auto curr = int(node == p || node == q);
if (curr + *ret1 + *ret2 == 2 && !result) {
result = node;
}
*ret = curr + *ret1 + *ret2;
}
}
return result;
}
};
// Time: O(n)
// Space: O(h)
class Solution2 {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode *result = nullptr;
dfs(root, p, q, &result);
return result;
}
private:
int dfs(TreeNode* node, TreeNode* p, TreeNode* q, TreeNode** result) {
if (!node) {
return 0;
}
auto left = dfs(node->left, p, q, result);
auto right = dfs(node->right, p, q, result);
auto curr = int(node == p || node == q);
if (curr + left + right == 2 && !(*result)) {
*result = node;
}
return curr + left + right;
}
};