Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1104: Path In Zigzag Labelled Binary Tree

In this guide, we solve Leetcode #1104 Path In Zigzag Labelled Binary Tree in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

In an infinite binary tree where every node has two children, the nodes are labelled in row order. In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Tree, Math, Binary Tree

Intuition

The input is a tree, so recursive decomposition is a natural fit.

We can compute the answer by combining results from left and right subtrees.

Approach

Use DFS and pass the required state through recursive calls.

Combine child results to compute the answer for each node.

Steps:

  • Pick traversal order.
  • Recurse with state.
  • Combine results from children.

Example

Input: label = 14 Output: [1,3,4,14]

Python Solution

class Solution: def pathInZigZagTree(self, label: int) -> List[int]: x = i = 1 while (x << 1) <= label: x <<= 1 i += 1 ans = [0] * i while i: ans[i - 1] = label label = ((1 << (i - 1)) + (1 << i) - 1 - label) >> 1 i -= 1 return ans

Complexity

The time complexity is O(log⁡n)O(\log n)O(logn), where nnn is the label of the node. The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy