-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
44 lines (42 loc) · 985 Bytes
/
solution1.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
/**
* https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/
*
* 1104. 二叉树寻路
*
* Medium
*
* 80ms 50.00%
* 34mb 100.00
*/
const pathInZigZagTree = label => {
const res = [];
// 注意层级的计算
let level = Math.ceil(Math.log2(label + 1));
while (level) {
res.push(label);
// 得到当前层级和上一级层级的边界
const [left, right] = computeEdge(level);
const [preLeft, preRight] = computeEdge(--level);
let index = 0;
if (left > right) {
index = left - label;
} else {
index = label - left;
}
if (preLeft > preRight) {
label = preLeft - Math.floor(index / 2);
} else {
label = preLeft + Math.floor(index / 2);
}
}
return res.reverse();
}
function computeEdge(level) {
let left = 2 ** level - 1;
let right = 2 ** (level - 1);
if (level % 2 !== 0) {
[left, right] = [right, left];
}
return [left, right];
}
console.log(pathInZigZagTree(26))