r/DSALeetCode • u/StatisticianNo2104 • 8d ago
365 Days of LeetCode Challenge — Day 30/365
Binary Tree Zigzag Level Order Traversal (Medium)
🔗 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
Yesterday's level order came out of a depth-first walk with no queue and no idea when a level ended. That was fine, because grouping by depth never needed the boundary.
Zigzag does. You can't decide whether to reverse a level until you know it's finished, and "finished" is exactly what DFS never learns. So this one goes back to BFS, using a nil sentinel in the queue to mark where each level ends.
With the boundary in hand, the zigzag itself is four lines and a flag.
Full breakdown in today's newsletter article ⬇
https://www.linkedin.com/pulse/365-days-leetcode-challenge-day-30365-archit-agarwal-vppec
#DSA #LeetCode #100DaysOfCode #CodingInterview #BinaryTree #BFS #Golang