Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
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.
Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label.
Example 1:
Input: label = 14 Output: [1,3,4,14]
Example 2:
Input: label = 26 Output: [1,2,6,10,26]
Constraints:
1 <= label <= 10^6Problem summary: 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. Given the label of a node in this tree, return the labels in the path from the root of the tree to the node with that label.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Tree
14
26
cycle-length-queries-in-a-tree)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1104: Path In Zigzag Labelled Binary Tree
class Solution {
public List<Integer> pathInZigZagTree(int label) {
int x = 1, i = 1;
while ((x << 1) <= label) {
x <<= 1;
++i;
}
List<Integer> ans = new ArrayList<>();
for (; i > 0; --i) {
ans.add(label);
label = ((1 << (i - 1)) + (1 << i) - 1 - label) >> 1;
}
Collections.reverse(ans);
return ans;
}
}
// Accepted solution for LeetCode #1104: Path In Zigzag Labelled Binary Tree
func pathInZigZagTree(label int) (ans []int) {
x, i := 1, 1
for x<<1 <= label {
x <<= 1
i++
}
for ; i > 0; i-- {
ans = append(ans, label)
label = ((1 << (i - 1)) + (1 << i) - 1 - label) >> 1
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return
}
# Accepted solution for LeetCode #1104: Path In Zigzag Labelled Binary Tree
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
// Accepted solution for LeetCode #1104: Path In Zigzag Labelled Binary Tree
struct Solution;
impl Solution {
fn path_in_zig_zag_tree(mut label: i32) -> Vec<i32> {
let mut level = 0;
while label >= 1 << level {
level += 1;
}
let mut res = vec![0; level];
for i in (0..level).rev() {
res[i] = label;
let r = (1 << i) - 1;
let l = r / 2 + 1;
label = l + r - label / 2;
}
res
}
}
#[test]
fn test() {
let label = 14;
let res = vec![1, 3, 4, 14];
assert_eq!(Solution::path_in_zig_zag_tree(label), res);
let label = 26;
let res = vec![1, 2, 6, 10, 26];
assert_eq!(Solution::path_in_zig_zag_tree(label), res);
let label = 16;
let res = vec![1, 3, 4, 15, 16];
assert_eq!(Solution::path_in_zig_zag_tree(label), res);
}
// Accepted solution for LeetCode #1104: Path In Zigzag Labelled Binary Tree
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1104: Path In Zigzag Labelled Binary Tree
// class Solution {
// public List<Integer> pathInZigZagTree(int label) {
// int x = 1, i = 1;
// while ((x << 1) <= label) {
// x <<= 1;
// ++i;
// }
// List<Integer> ans = new ArrayList<>();
// for (; i > 0; --i) {
// ans.add(label);
// label = ((1 << (i - 1)) + (1 << i) - 1 - label) >> 1;
// }
// Collections.reverse(ans);
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
BFS with a queue visits every node exactly once — O(n) time. The queue may hold an entire level of the tree, which for a complete binary tree is up to n/2 nodes = O(n) space. This is optimal in time but costly in space for wide trees.
Every node is visited exactly once, giving O(n) time. Space depends on tree shape: O(h) for recursive DFS (stack depth = height h), or O(w) for BFS (queue width = widest level). For balanced trees h = log n; for skewed trees h = n.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.