Path in ZigZag Tree

https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/

记住上一个parent的值等于 parent=(currMax+currMincurrLabel)/2parent = (currMax + currMin - currLabel)/2

currMax 是当前level的最大值,currMin是最小

class Solution:
    def pathInZigZagTree(self, label: int) -> List[int]:
        curr =1
        res = []
        while curr <= label:
            curr *=2
        nxt = label
        while nxt != 1:
            currMax = curr-1
            currMin = curr//2
            res.append(nxt)
            nxt = (currMax+currMin- nxt)//2
            curr//=2
        res.append(nxt)
        return res[::-1]
            

Last updated

Was this helpful?