-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1484_copy_random_binary_tree.cc
52 lines (44 loc) · 1.13 KB
/
1484_copy_random_binary_tree.cc
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
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <iostream>
#include <tuple>
#include <queue>
#include <stack>
#include <unordered_map>
using namespace::std;
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* dfs(Node* root) {
if (root == nullptr)
return nullptr;
if (dict.find(root) != dict.end())
return dict[root];
NodeCopy* ptr = new NodeCopy(root->val);
dict[root] = ptr;
ptr->random = dfs(root->random);
ptr->left = dfs(root->left);
ptr->right = dfs(root->right);
return dict[root];
}
NodeCopy* copyRandomBinaryTree(Node* root) {
return dfs(root);
}
private:
unordered_map<Node*, NodeCopy*> dict;
};
int main()
{
return 0;
}