|
| 1 | +--- |
| 2 | +id: binary-tree-zigzag-level-order-traversal |
| 3 | +title: Binary Tree Zigzag Level Order Traversal |
| 4 | +sidebar_label: 0103-Zigzag Level Order |
| 5 | +tags: |
| 6 | + - Tree |
| 7 | + - Breadth-First-Search |
| 8 | + - C++ |
| 9 | +description: "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)." |
| 10 | +--- |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +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). |
| 15 | + |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | + |
| 22 | +``` |
| 23 | +Input: root = [3,9,20,null,null,15,7] |
| 24 | +Output: [[3],[20,9],[15,7]] |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +### Constraints |
| 29 | + |
| 30 | +- `The number of nodes in the tree is in the range $[0, 2000]$.` |
| 31 | +- `$-100 \leq \text{Node.val} \leq 100$` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Solution for Binary Tree Problem |
| 36 | + |
| 37 | +### Intuition And Approach |
| 38 | + |
| 39 | +To perform a zigzag level order traversal, we can use a breadth-first search (BFS) with a queue. We'll use a boolean flag to determine the direction of traversal at each level. |
| 40 | + |
| 41 | +1. **Breadth-First Search (BFS):** Traverse the tree level by level. |
| 42 | +2. **Direction Toggle:** Use a flag to toggle the direction of traversal for each level (left-to-right or right-to-left). |
| 43 | + |
| 44 | +<Tabs> |
| 45 | + <tabItem value="Iterative Simulation" label="Iterative Simulation"> |
| 46 | + |
| 47 | + |
| 48 | +#### Code in Different Languages |
| 49 | + |
| 50 | +<Tabs> |
| 51 | + <TabItem value="Java" label="Java" default> |
| 52 | + <SolutionAuthor name="@Vipullakum007"/> |
| 53 | + ```java |
| 54 | + class Solution { |
| 55 | + public int minDepth(TreeNode root) { |
| 56 | + if (root == null) return 0; |
| 57 | + Queue<TreeNode> queue = new LinkedList<>(); |
| 58 | + queue.offer(root); |
| 59 | + int depth = 1; |
| 60 | + while (!queue.isEmpty()) { |
| 61 | + int levelSize = queue.size(); |
| 62 | + for (int i = 0; i < levelSize; i++) { |
| 63 | + TreeNode node = queue.poll(); |
| 64 | + if (node.left == null && node.right == null) return depth; |
| 65 | + if (node.left != null) queue.offer(node.left); |
| 66 | + if (node.right != null) queue.offer(node.right); |
| 67 | + } |
| 68 | + depth++; |
| 69 | + } |
| 70 | + return depth; |
| 71 | + } |
| 72 | +} |
| 73 | + ``` |
| 74 | + |
| 75 | + </TabItem> |
| 76 | + <TabItem value="Python" label="Python"> |
| 77 | + <SolutionAuthor name="@Vipullakum007"/> |
| 78 | + ```python |
| 79 | + class Solution: |
| 80 | + def minDepth(self, root): |
| 81 | + if not root: |
| 82 | + return 0 |
| 83 | + queue = collections.deque([(root, 1)]) |
| 84 | + while queue: |
| 85 | + node, depth = queue.popleft() |
| 86 | + if not node.left and not node.right: |
| 87 | + return depth |
| 88 | + if node.left: |
| 89 | + queue.append((node.left, depth + 1)) |
| 90 | + if node.right: |
| 91 | + queue.append((node.right, depth + 1)) |
| 92 | + ``` |
| 93 | + |
| 94 | + </TabItem> |
| 95 | + <TabItem value="C++" label="C++"> |
| 96 | + <SolutionAuthor name="@Vipullakum007"/> |
| 97 | + ```cpp |
| 98 | + class Solution { |
| 99 | +public: |
| 100 | + int minDepth(TreeNode* root) { |
| 101 | + if (!root) return 0; |
| 102 | + queue<TreeNode*> q; |
| 103 | + q.push(root); |
| 104 | + int depth = 1; |
| 105 | + while (!q.empty()) { |
| 106 | + int levelSize = q.size(); |
| 107 | + for (int i = 0; i < levelSize; ++i) { |
| 108 | + TreeNode* node = q.front(); |
| 109 | + q.pop(); |
| 110 | + if (!node->left && !node->right) return depth; // Leaf node found |
| 111 | + if (node->left) q.push(node->left); |
| 112 | + if (node->right) q.push(node->right); |
| 113 | + } |
| 114 | + ++depth; |
| 115 | + } |
| 116 | + return depth; |
| 117 | + } |
| 118 | +}; |
| 119 | + ``` |
| 120 | + |
| 121 | + </TabItem> |
| 122 | +</Tabs> |
| 123 | + |
| 124 | +#### Complexity Analysis |
| 125 | + |
| 126 | +- Time Complexity: O(n) where n is the number of nodes in the binary tree. |
| 127 | +- Space Complexity: O(h) where h is the height of the binary tree. |
| 128 | + |
| 129 | +</tabItem> |
| 130 | +</Tabs> |
| 131 | + |
| 132 | + |
| 133 | +--- |
0 commit comments