-
Notifications
You must be signed in to change notification settings - Fork 3
/
swapNodes.js
41 lines (38 loc) · 1022 Bytes
/
swapNodes.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
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
function swapNodes(indexes, queries) {
const inOrder = (node, track = []) => {
if (node.left)
inOrder(node.left, track);
track.push(node.value);
if (node.right)
inOrder(node.right, track);
return track;
};
const node = value => ({ value, left: null, right: null });
const root = node(1);
let nextLevel = [];
let currentLevel = [root];
let levels = [currentLevel];
let i = 0;
for (const [left, right] of indexes) {
const parent = currentLevel[i++];
if (left !== -1)
nextLevel.push(parent.left = node(left));
if (right !== -1)
nextLevel.push(parent.right = node(right));
if (i === currentLevel.length && nextLevel.length) {
levels.push(currentLevel = nextLevel);
nextLevel = [];
i = 0;
}
}
const r = [];
for (const query of queries) {
for (let i = query - 1; i < levels.length; i += query)
for (const node of levels[i])
[node.left, node.right] = [node.right, node.left];
r.push(inOrder(root));
}
return r;
}