Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Path In Zigzag Labelled Binary Tree in Python
In an infinite binary tree where every node has two children, nodes are labeled in a zigzag pattern. Odd-numbered rows (1st, 3rd, 5th...) are labeled left to right, while even-numbered rows (2nd, 4th, 6th...) are labeled right to left.
Given a node's label in this zigzag tree, we need to find the path from the root to that node. For example, if label = 14, the output should be [1, 3, 4, 14].
Algorithm Approach
The solution involves building the tree level by level until we find the target label, then tracing back to find the path ?
Steps
- Build tree array representing node positions
- Alternate between left-to-right and right-to-left labeling
- Stop when target label is found
- Trace back using parent-child relationships (index // 2)
Implementation
class Solution:
def pathInZigZagTree(self, label):
tree = []
res = []
tree.append(0) # Placeholder for index 0
tree.append(1) # Root node
odd = True
current = 1
two = 2
if label == 1:
return [1]
while True:
if odd:
# Odd rows: right to left (reverse order)
max_val = current + two
temp = max_val
while temp > current:
tree.append(temp)
if temp == label:
break
temp -= 1
if tree[-1] == label:
break
current = max_val
else:
# Even rows: left to right
temp = two
while temp:
temp -= 1
current += 1
tree.append(current)
if current == label:
break
if tree[-1] == label:
break
two *= 2
odd = not odd
# Build path by tracing parent relationships
index = len(tree) - 1
while index:
res.append(tree[index])
index //= 2
res = res[::-1]
return res
# Test the solution
solution = Solution()
print(solution.pathInZigZagTree(14))
[1, 3, 4, 14]
How It Works
The algorithm builds the tree structure by alternating labeling directions:
- Odd levels: Labels decrease from right to left
- Even levels: Labels increase from left to right
- Parent relationship: Node at index i has parent at index i//2
- Path construction: Trace backwards from target to root using parent indices
Example Walkthrough
For label = 14:
# Tree construction process
solution = Solution()
# Level 1: [0, 1] - Root
# Level 2: [0, 1, 3, 2] - Right to left: 3, 2
# Level 3: [0, 1, 3, 2, 4, 5, 6, 7] - Left to right: 4, 5, 6, 7
# Level 4: [0, 1, 3, 2, 4, 5, 6, 7, 15, 14, ...] - Right to left: 15, 14, ...
# Path tracing for label 14 (at index 9):
# index 9 ? tree[9] = 14
# index 4 ? tree[4] = 4 (parent of 14)
# index 2 ? tree[2] = 3 (parent of 4)
# index 1 ? tree[1] = 1 (parent of 3, root)
print("Path for label 14:", solution.pathInZigZagTree(14))
print("Path for label 26:", solution.pathInZigZagTree(26))
Path for label 14: [1, 3, 4, 14] Path for label 26: [1, 2, 6, 26]
Conclusion
The zigzag binary tree path problem is solved by building the tree structure level by level and tracing parent relationships. The key insight is alternating labeling directions between levels and using integer division to find parent nodes.
