Path In Zigzag Labelled Binary Tree - Problem

Imagine a special infinite binary tree where nodes are labeled in a unique zigzag pattern! 🌲

Here's how the labeling works:

  • Odd-numbered rows (1st, 3rd, 5th...): Labels go left to right β†’ β†’ β†’
  • Even-numbered rows (2nd, 4th, 6th...): Labels go right to left ← ← ←

For example:

       1           (Row 1: left→right)
     /   \
    3     2         (Row 2: right←left)
   / \   / \
  4   5 6   7       (Row 3: left→right)

Your mission: Given a node's label, find the complete path from the root (node 1) to that node!

Input: An integer label representing a node in the zigzag binary tree

Output: An array containing all labels in the path from root to the target node

Input & Output

example_1.py β€” Basic Path Finding
$ Input: label = 14
β€Ί Output: [1, 3, 6, 14]
πŸ’‘ Note: Node 14 is in level 4. Tracing back: 14 β†’ 6 (level 3) β†’ 3 (level 2) β†’ 1 (level 1, root). The complete path from root to node 14 is [1, 3, 6, 14].
example_2.py β€” Early Level Node
$ Input: label = 26
β€Ί Output: [1, 2, 6, 10, 26]
πŸ’‘ Note: Node 26 is in level 5. Working backwards through the zigzag pattern: 26 β†’ 10 β†’ 6 β†’ 2 β†’ 1, giving us the root-to-target path [1, 2, 6, 10, 26].
example_3.py β€” Root Node Edge Case
$ Input: label = 1
β€Ί Output: [1]
πŸ’‘ Note: The root node 1 has no parent, so the path is simply [1]. This is the simplest case where the target is the root itself.

Constraints

  • 1 ≀ label ≀ 106
  • The given label will always exist in the zigzag binary tree
  • Tree property: Every node has exactly two children (except leaf nodes)

Visualization

Tap to expand
Zigzag Binary Tree: Finding Path to Node 141Level 1 (Lβ†’R)32Level 2 (R←L)4567Level 3 (Lβ†’R)15141312Level 4 (R←L)...more nodesMathematical Calculation:Node 14 β†’ Level 4 β†’ Normal pos: (15-14+8) = 9 β†’ Parent: 9Γ·2 = 4 β†’ Convert to Level 3: Node 6Node 6 β†’ Level 3 β†’ Normal pos: 6 β†’ Parent: 6Γ·2 = 3 β†’ Convert to Level 2: Node 3Node 3 β†’ Level 2 β†’ Normal pos: (3-3+2) = 2 β†’ Parent: 2Γ·2 = 1 β†’ Level 1: Node 1 (ROOT)Final Path: [1, 3, 6, 14] βœ“
Understanding the Visualization
1
Identify the Level
First, determine which level (floor) contains our target node using powers of 2
2
Convert to Normal Position
Transform the zigzag position to what it would be in a normal left-to-right binary tree
3
Find the Parent
In a normal binary tree, parent is at position divided by 2 in the previous level
4
Convert Back to Zigzag
Transform the parent's normal position back to its actual zigzag position
5
Repeat Until Root
Continue this process until we reach the root node (level 1)
Key Takeaway
🎯 Key Insight: By understanding the mathematical relationship between zigzag positions and normal binary tree positions, we can calculate any path in O(log n) time without building the tree!
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen