1
var pathInZigZagTree = function (label) {2
//store highest and lowest value for each level3
let levels = [[1, 1]]; //to reduce space complexity we will fill the levels array with out output as we go5
let nodesInLastRow = 1;7
//Calculate which level the label lies in8
while (totalNodes < label) {9
let lowest = totalNodes + 1;11
nodesInLastRow = nodesInLastRow * 2;12
totalNodes += nodesInLastRow;14
let highest = totalNodes;16
levels.push([lowest, highest]);19
let index = levels.length;20
let childBoundaries = levels[levels.length - 1];21
levels[levels.length - 1] = label;23
//Work bottom up, for each level, calculate the value based on the child and the child boundaries boundaries24
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 level26
let childValue = levels[i + 1];28
let inversionCalculation = Math.abs(29
childBoundaries[0] + childBoundaries[1] - childValue32
childBoundaries = levels[i];34
levels[i] = Math.floor(inversionCalculation / 2);