Skip to content

Commit a79884e

Browse files
authored
Create path-in-zigzag-labelled-binary-tree.cpp
1 parent 279e3d8 commit a79884e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(logn)
2+
// Space: O(logn)
3+
4+
class Solution {
5+
public:
6+
vector<int> pathInZigZagTree(int label) {
7+
int count = 1;
8+
while (count <= label) {
9+
count *= 2;
10+
}
11+
vector<int> result;
12+
for (; label >= 1; label /= 2, count /= 2) {
13+
result.emplace_back(label);
14+
label = (count / 2) + ((count - 1) - label);
15+
}
16+
reverse(result.begin(), result.end());
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)