// This function copies random node by using the hashmap built by // copyLeftRightNode() void copyRandom(Node* treeNode, Node* cloneNode, map<Node *, Node *> *mymap) { if (cloneNode == NULL) return; cloneNode->random = (*mymap)[treeNode->random]; copyRandom(treeNode->left, cloneNode->left, mymap); copyRandom(treeNode->right, cloneNode->right, mymap); }
// This function makes the clone of given tree. It mainly uses // copyLeftRightNode() and copyRandom() Node* cloneTree(Node* tree) { if (tree == NULL) return NULL; map<Node *, Node *> *mymap = new map<Node *, Node *>; Node* newTree = copyLeftRightNode(tree, mymap); copyRandom(tree, newTree, mymap); return newTree; }
struct RandomListNode *copyRandomList(struct RandomListNode *head) { copyList(head); copyRandom(head); return spiltList(head); }