Skip to content

Commit f623a93

Browse files
authored
Create path-in-zigzag-labelled-binary-tree.py
1 parent a79884e commit f623a93

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(logn)
2+
# Space: O(logn)
3+
4+
class Solution(object):
5+
def pathInZigZagTree(self, label):
6+
"""
7+
:type label: int
8+
:rtype: List[int]
9+
"""
10+
count = 2**label.bit_length()
11+
result = []
12+
while label >= 1:
13+
result.append(label)
14+
label = ((count//2) + ((count-1)-label)) // 2
15+
count //= 2
16+
result.reverse()
17+
return result

0 commit comments

Comments
 (0)