-
Notifications
You must be signed in to change notification settings - Fork 0
/
0510.cpp
44 lines (38 loc) · 831 Bytes
/
0510.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
// Runhe Tian Coding Practice
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std;
struct Node {
Node* ptr1;
Node* ptr2;
};
typedef map<Node*, Node*> NodeMap;
Node* CopyRecursive(Node* cur, NodeMap& nodeMap) {
if(cur == NULL) {
cout << "1" << endl;
return cur;
}
NodeMap::iterator it = nodeMap.find(cur);
if (it != nodeMap.end()) {
cout << "2" << endl;
return it->second;
} else {
cout << "3" << endl;
Node* node = new Node;
nodeMap[cur] = node;
node->ptr1 = CopyRecursive (cur->ptr1, nodeMap);
node->ptr2 = CopyRecursive (cur->ptr2, nodeMap);
return node;
}
}
Node* CopyStructure(Node* root) {
NodeMap nodeMap;
return CopyRecursive(root, nodeMap);
}
int main() {
Node *ptr = new Node;
CopyStructure(ptr);
return 0;
}