forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone-binary-tree-with-random-pointer.cpp
174 lines (166 loc) · 5.96 KB
/
clone-binary-tree-with-random-pointer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Time: O(n)
// Space: O(h)
/**
* Definition for a Node.
* struct Node {
* int val;
* Node *left;
* Node *right;
* Node *random;
* Node() : val(0), left(nullptr), right(nullptr), random(nullptr) {}
* Node(int x) : val(x), left(nullptr), right(nullptr), random(nullptr) {}
* Node(int x, Node *left, Node *right, Node *random) : val(x), left(left), right(right), random(random) {}
* };
*/
class Solution {
public:
NodeCopy* copyRandomBinaryTree(Node* root) {
const auto& merge = [](Node *node) {
auto copy = new NodeCopy(node->val);
tie(node->left, copy->left) = pair(reinterpret_cast<Node*>(copy),
reinterpret_cast<NodeCopy*>(node->left));
return pair{reinterpret_cast<Node*>(copy->left), copy};
};
const auto& clone = [](Node *node) {
auto copy = reinterpret_cast<NodeCopy*>(node->left);
node->left->random = node->random ? node->random->left : nullptr;
node->left->right = node->right ? node->right->left : nullptr;
return pair{reinterpret_cast<Node*>(copy->left), copy};
};
const auto& split = [](Node *node) {
auto copy = reinterpret_cast<NodeCopy*>(node->left);
tie(node->left, copy->left) = pair(copy->left ? reinterpret_cast<Node*>(copy->left) : nullptr,
copy->left ? copy->left->left : nullptr);
return pair{reinterpret_cast<Node*>(node->left), copy};
};
iter_dfs(root, merge);
iter_dfs(root, clone);
return iter_dfs(root, split);
}
private:
NodeCopy *iter_dfs(Node *root, const function<pair<Node*, NodeCopy*>(Node*)>& callback) {
NodeCopy *result = nullptr;
vector<Node *> stk = {root};
while (!stk.empty()) {
auto node = stk.back(); stk.pop_back();
if (!node) {
continue;
}
const auto& [left_node, copy] = callback(node);
if (!result) {
result = copy;
}
stk.emplace_back(node->right);
stk.emplace_back(left_node);
}
return result;
}
};
// Time: O(n)
// Space: O(h)
class Solution_Recu {
public:
NodeCopy* copyRandomBinaryTree(Node* root) {
const auto& merge = [](Node *node) {
auto copy = new NodeCopy(node->val);
tie(node->left, copy->left) = pair(reinterpret_cast<Node*>(copy),
reinterpret_cast<NodeCopy*>(node->left));
return pair{reinterpret_cast<Node*>(copy->left), copy};
};
const auto& clone = [](Node *node) {
auto copy = reinterpret_cast<NodeCopy*>(node->left);
node->left->random = node->random ? node->random->left : nullptr;
node->left->right = node->right ? node->right->left : nullptr;
return pair{reinterpret_cast<Node*>(copy->left), copy};
};
const auto& split = [](Node *node) {
auto copy = reinterpret_cast<NodeCopy*>(node->left);
tie(node->left, copy->left) = pair(copy->left ? reinterpret_cast<Node*>(copy->left) : nullptr,
copy->left ? copy->left->left : nullptr);
return pair{reinterpret_cast<Node*>(node->left), copy};
};
dfs(root, merge);
dfs(root, clone);
return dfs(root, split);
}
private:
NodeCopy *dfs(Node *node, const function<pair<Node*, NodeCopy*>(Node*)>& callback) {
if (!node) {
return nullptr;
}
const auto& [left_node, copy] = callback(node);
dfs(left_node, callback);
dfs(node->right, callback);
return copy;
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
NodeCopy* copyRandomBinaryTree(Node* root) {
unordered_map<Node*, NodeCopy*> lookup;
lookup[nullptr] = nullptr;
vector<Node *> stk = {root};
while (!stk.empty()) {
auto node = stk.back(); stk.pop_back();
if (!node) {
continue;
}
if (!lookup.count(node)) {
lookup[node] = new NodeCopy();
}
if (!lookup.count(node->left)) {
lookup[node->left] = new NodeCopy();
}
if (!lookup.count(node->right)) {
lookup[node->right] = new NodeCopy();
}
if (!lookup.count(node->random)) {
lookup[node->random] = new NodeCopy();
}
lookup[node]->val = node->val;
lookup[node]->left = lookup[node->left];
lookup[node]->right = lookup[node->right];
lookup[node]->random = lookup[node->random];
stk.emplace_back(node->right);
stk.emplace_back(node->left);
}
return lookup[root];
}
};
// Time: O(n)
// Space: O(n)
class Solution2_Recu {
public:
NodeCopy* copyRandomBinaryTree(Node* root) {
unordered_map<Node*, NodeCopy*> lookup;
lookup[nullptr] = nullptr;
dfs(root, &lookup);
return lookup[root];
}
private:
void dfs(Node *node, unordered_map<Node*, NodeCopy*> *lookup) {
if (!node) {
return;
}
if (!lookup->count(node)) {
(*lookup)[node] = new NodeCopy();
}
if (!lookup->count(node->left)) {
(*lookup)[node->left] = new NodeCopy();
}
if (!lookup->count(node->right)) {
(*lookup)[node->right] = new NodeCopy();
}
if (!lookup->count(node->random)) {
(*lookup)[node->random] = new NodeCopy();
}
(*lookup)[node]->val = node->val;
(*lookup)[node]->left = (*lookup)[node->left];
(*lookup)[node]->right = (*lookup)[node->right];
(*lookup)[node]->random = (*lookup)[node->random];
dfs(node->left, lookup);
dfs(node->right, lookup);
}
};