File tree Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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 ] ) )
Original file line number Diff line number Diff line change @@ -145,4 +145,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
145
145
146
146
| Day 63| [ 131. Palindrome Partitioning] ( https://leetcode.com/problems/palindrome-partitioning/ ) | [ javascript] ( ) | [ :memo : ] ( https://leetcode.com/problems/palindrome-partitioning/ ) | Medium|
147
147
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|
You can’t perform that action at this time.
0 commit comments