-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path114.js
48 lines (44 loc) · 1.14 KB
/
114.js
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
const {
getArrayFromList,
getListFromArray,
ListNode,
} = require("./utils/list");
const { TreeNode } = require("./utils/tree");
var mergeList = function (leftList, midValue, rightList) {
const resultList = new ListNode(0);
let currentNode = resultList;
while (leftList) {
currentNode.next = leftList;
currentNode = currentNode.next;
leftList = leftList.next;
}
currentNode.next = new ListNode(midValue);
currentNode = currentNode.next;
while (rightList) {
currentNode.next = rightList;
currentNode = currentNode.next;
rightList = rightList.next;
}
return resultList.next;
};
/**
* @param {TreeNode} root
* @return {void} Do not return anything, modify root in-place instead.
*/
var flatten = function (root) {
if (!root) return null;
flatten(root.left);
flatten(root.right);
let left = root.left;
let right = root.right;
root.left = null;
root.right = left;
while (root.right) {
root = root.right;
}
root.right = right;
};
const leftList = null;
const midValue = 5;
const rightList = getListFromArray([1, 2, 3]);
console.log(getArrayFromList(mergeList(leftList, midValue, rightList)));