grah

Zigzag Traversal of a Binary Tree

January 14, 2024
medium
BFS, grah

Problem # Given a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e., from left to right, then right to left for the next level and alternate between). Example: Input: 3 / \ 9 20 / \ 15 7 Output: [[3], [20, 9], [15, 7]] Solution Approach # The solution uses a breadth-first search (BFS) approach with a queue, alternating the direction of traversal with each level of the tree. ...