Skip to content
LC-1104 Medium LeetCode

1104. Path In Zigzag Labelled Binary Tree

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 75% Topics: Math, Tree, Binary Tree
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(logn)
# Space: O(logn)

class Solution(object):
    def pathInZigZagTree(self, label):
        """
        :type label: int
        :rtype: List[int]
        """
        count = 2**label.bit_length()
        result = []
        while label >= 1:
            result.append(label)
            label = ((count//2) + ((count-1)-label)) // 2
            count //= 2
        result.reverse()
        return result

Solution from kamyu104/LeetCode-Solutions · MIT