-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path1485-clone-binary-tree-with-random-pointer.js
More file actions
53 lines (47 loc) · 1.64 KB
/
1485-clone-binary-tree-with-random-pointer.js
File metadata and controls
53 lines (47 loc) · 1.64 KB
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
/**
* 1485. Clone Binary Tree With Random Pointer
* https://leetcode.com/problems/clone-binary-tree-with-random-pointer/
* Difficulty: Medium
*
* A binary tree is given such that each node contains an additional random pointer which
* could point to any node in the tree or null.
*
* Return a deep copy of the tree.
*
* The tree is represented in the same input/output way as normal binary trees where each
* node is represented as a pair of [val, random_index] where:
* - val: an integer representing Node.val
* - random_index: the index of the node (in the input) where the random pointer points to,
* or null if it does not point to any node.
*
* You will be given the tree in class Node and you should return the cloned tree in class
* NodeCopy. NodeCopy class is just a clone of Node class with the same attributes and constructors.
*/
/**
* // Definition for a _Node.
* function _Node(val, left, right, random) {
* this.val = val === undefined ? null : val;
* this.left = left === undefined ? null : left;
* this.right = right === undefined ? null : right;
* this.random = random === undefined ? null : random;
* };
*/
/**
* @param {_Node} root
* @return {NodeCopy}
*/
var copyRandomBinaryTree = function(root) {
if (!root) return null;
const nodeMap = new Map();
return createCopy(root);
function createCopy(node) {
if (!node) return null;
if (nodeMap.has(node)) return nodeMap.get(node);
const copy = new NodeCopy(node.val);
nodeMap.set(node, copy);
copy.left = createCopy(node.left);
copy.right = createCopy(node.right);
copy.random = createCopy(node.random);
return copy;
}
};