-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1104.java
More file actions
40 lines (35 loc) · 1003 Bytes
/
Copy pathSolution1104.java
File metadata and controls
40 lines (35 loc) · 1003 Bytes
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
package medium;
import java.util.LinkedList;
import java.util.List;
public class Solution1104 {
public static void main(String[] args) {
int[] cases = {
14,
26
};
Solution1104 solution = new Solution1104();
for (int a : cases) {
System.out.println(solution.pathInZigZagTree(a));
}
}
public List<Integer> pathInZigZagTree(int label) {
int depth = getDepth(label);
List<Integer> result = new LinkedList<>();
while (depth > 0) {
result.add(0, label);
if (--depth % 2 == 0) {
label /= 2;
label = 3 * (1 << (depth - 1)) - 1 - label;
} else {
label = 3 * (1 << depth) - 1 - label;
label /= 2;
}
}
return result;
}
private int getDepth(int label) {
int depth = 0;
while ((label >>= 1) > 0) ++depth;
return depth + 1;
}
}