-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathInZigzagLabelledBinaryTree.java
More file actions
77 lines (67 loc) · 2.13 KB
/
PathInZigzagLabelledBinaryTree.java
File metadata and controls
77 lines (67 loc) · 2.13 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package leetcode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* PathInZigzagLabelledBinaryTree
* https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/
* 1104. 二叉树寻路
* https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/solution/man-su-jie-ti-li-jie-si-lu-by-oshdyr-7ix0/
*
* @author tobin
* @since 2021-07-29
*/
public class PathInZigzagLabelledBinaryTree {
public static void main(String[] args) {
PathInZigzagLabelledBinaryTree sol = new PathInZigzagLabelledBinaryTree();
System.out.println(sol.pathInZigZagTree(26));
System.out.println(sol.pathInZigZagTree(14));
System.out.println(sol.pathInZigZagTree(1));
System.out.println(sol.pathInZigZagTree(1000000));
}
private List<Integer> powers = new ArrayList<>();
public List<Integer> pathInZigZagTree(int label) {
int base = 2;
for (int i = 0; i < 20; i++) {
powers.add(base);
base = base << 1;
}
int pos = labelRealPos(label);
Stack<Integer> stack = new Stack<>();
while (pos > 1) {
stack.push(posRefLabel(pos));
pos = pos >> 1;
}
List<Integer> result = new LinkedList<>();
result.add(1);
while (!stack.isEmpty()) {
result.add(stack.pop());
}
return result;
}
private int labelRealPos(int label) {
for (int i = 0; i < powers.size(); i++) {
if (label < powers.get(i)) {
if (i % 2 == 0) {
return label;
} else {
return powers.get(i) - label + powers.get(i - 1) - 1;
}
}
}
return -1;
}
private int posRefLabel(int pos) {
for (int i = 0; i < powers.size(); i++) {
if (pos < powers.get(i)) {
if (i % 2 == 0) {
return pos;
} else {
return powers.get(i) - (pos - powers.get(i - 1) + 1);
}
}
}
return 0;
}
}