Skip to content

Commit 8ed0e6f

Browse files
committed
Day65
1 parent 9c6cd83 commit 8ed0e6f

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Day65/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val, left, right, random) {
4+
* this.val = val === undefined ? null : val;
5+
* this.left = left === undefined ? null : left;
6+
* this.right = right === undefined ? null : right;
7+
* this.random = random === undefined ? null : random;
8+
* };
9+
*/
10+
11+
/**
12+
* @param {Node} root
13+
* @return {NodeCopy}
14+
*/
15+
var copyRandomBinaryTree = function (root) {
16+
let map = new Map();
17+
18+
function dfs(node) {
19+
if (!node) return null;
20+
if (map.has(node)) return map.get(node);
21+
22+
let d = new NodeCopy(node.val);
23+
map.set(node, d);
24+
d.left = dfs(node.left)
25+
d.right = dfs(node.right)
26+
d.random = dfs(node.random)
27+
return d;
28+
29+
}
30+
31+
return dfs(root)
32+
};

Extra/mergeSort.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const merge = (left, right) => {
2+
let res = [];
3+
while (left.length && right.length) {
4+
if (left[0] < right[0]) {
5+
res.push(left.shift())
6+
} else {
7+
res.push(right.shift())
8+
}
9+
}
10+
11+
return [...res, ...left, ...right]
12+
}
13+
14+
15+
const mergeSort = arr => {
16+
if (arr.length <= 1) return arr;
17+
let mid = Math.floor(arr.length / 2),
18+
left = mergeSort(arr.slice(0, mid)),
19+
right = mergeSort(arr.slice(mid));
20+
21+
return merge(left, right);
22+
};
23+
24+
25+
console.log(mergeSort([15, 13, 2, 1]))

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
145145

146146
|Day 63| [131. Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/) | [javascript]()|[:memo:](https://leetcode.com/problems/palindrome-partitioning/)|Medium|
147147

148-
|Day 64| [131. Palindrome Partitioning](https://leetcode.com/problems/squares-of-a-sorted-array/) | [javascript]()|[:memo:](https://leetcode.com/problems/squares-of-a-sorted-array/)|Easy|
148+
|Day 64| [131. Palindrome Partitioning](https://leetcode.com/problems/squares-of-a-sorted-array/) | [javascript]()|[:memo:](https://leetcode.com/problems/squares-of-a-sorted-array/)|Easy|
149+
150+
|Day 65| [1485. Clone Binary Tree With Random Pointer](https://leetcode.com/problems/clone-binary-tree-with-random-pointer/) | [javascript]()|[:memo:](https://leetcode.com/problems/clone-binary-tree-with-random-pointer/)|Medium|

0 commit comments

Comments
 (0)