1104. 二叉树寻路
为保证权益,题目请参考 1104. 二叉树寻路(From LeetCode).
解决方案1
Python
python
# 1104. 二叉树寻路
# https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/
from typing import List
import math
class Solution:
def pathInZigZagTree(self, label: int) -> List[int]:
ans = []
while label != 1:
ans.insert(0, label)
par = label // 2
recordPar = self.getReversedPar(par)
label = recordPar
ans.insert(0, 1)
return ans
def getReversedPar(self, par: int) -> int:
s = math.floor(math.log2(par))
start = 2 ** s
end = 2 ** (s + 1) - 1
res = (end - par) + start
return res
if __name__ == "__main__":
solution = Solution()
print(solution.pathInZigZagTree(14))
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
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