1
var pathInZigZagTree = function (label) {
2
//store highest and lowest value for each level
3
let levels = [[1, 1]]; //to reduce space complexity we will fill the levels array with out output as we go
4
let totalNodes = 1;
5
let nodesInLastRow = 1;
6

7
//Calculate which level the label lies in
8
while (totalNodes < label) {
9
let lowest = totalNodes + 1;
10

11
nodesInLastRow = nodesInLastRow * 2;
12
totalNodes += nodesInLastRow;
13

14
let highest = totalNodes;
15

16
levels.push([lowest, highest]);
17
}
18

19
let index = levels.length;
20
let childBoundaries = levels[levels.length - 1];
21
levels[levels.length - 1] = label;
22

23
//Work bottom up, for each level, calculate the value based on the child and the child boundaries boundaries
24
for (let i = levels.length - 2; i >= 0; i--) {
25
let childLevel = i + 2; //2 because i is index of 0, so 1 is to preset it to 1...n and then and second one is parent level
26
let childValue = levels[i + 1];
27

28
let inversionCalculation = Math.abs(
29
childBoundaries[0] + childBoundaries[1] - childValue
30
);
31

32
childBoundaries = levels[i];
33

34
levels[i] = Math.floor(inversionCalculation / 2);
35
}
36

37
return levels;
38
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0