Skip to content

Commit d1eaffa

Browse files
authored
Update 1104.path-in-zigzag-labelled-binary-tree.js
1 parent 17d4382 commit d1eaffa

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1104.path-in-zigzag-labelled-binary-tree.js

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/**
2+
* @param {number} label
3+
* @return {number[]}
4+
*/
5+
const pathInZigZagTree = function(label) {
6+
const res = [], { log2, floor, ceil } = Math
7+
const level = floor(log2(label))
8+
let compl = 2 ** (level + 1) - 1 + 2 ** level - label
9+
10+
while(label) {
11+
res.push(label)
12+
label = floor(label / 2)
13+
compl = floor(compl / 2)
14+
;[label, compl] = [compl, label]
15+
}
16+
17+
res.reverse()
18+
19+
return res
20+
};
21+
22+
// another
23+
24+
125
/**
226
* @param {number} label
327
* @return {number[]}

0 commit comments

Comments
 (0)