From 8488f466082f3f80508607612fdcde769250c896 Mon Sep 17 00:00:00 2001 From: Ayushmaanagarwal1121 Date: Fri, 7 Jun 2024 14:17:49 +0530 Subject: [PATCH 1/2] Create 0103-zigzag-order-traversal.md --- .../0100-0199/0103-zigzag-order-traversal.md | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md diff --git a/dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md b/dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md new file mode 100644 index 000000000..346e7911d --- /dev/null +++ b/dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md @@ -0,0 +1,133 @@ +--- +id: binary-tree-zigzag-level-order-traversal +title: Binary Tree Zigzag Level Order Traversal +sidebar_label: 0103-Zigzag Level Order +tags: + - Tree + - Breadth-First-Search + - C++ +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)." +--- + +## Problem 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). + + +### Examples + +**Example 1:** + +![LeetCode Problem - Binary Tree](https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg) +``` +Input: root = [3,9,20,null,null,15,7] +Output: [[3],[20,9],[15,7]] +``` + + +### Constraints + +- `The number of nodes in the tree is in the range $[0, 2000]$.` +- `$-100 \leq \text{Node.val} \leq 100$` + +--- + +## Solution for Binary Tree Problem + +### Intuition And Approach + +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. + +1. **Breadth-First Search (BFS):** Traverse the tree level by level. +2. **Direction Toggle:** Use a flag to toggle the direction of traversal for each level (left-to-right or right-to-left). + + + + + +#### Code in Different Languages + + + + + ```java + class Solution { + public int minDepth(TreeNode root) { + if (root == null) return 0; + Queue queue = new LinkedList<>(); + queue.offer(root); + int depth = 1; + while (!queue.isEmpty()) { + int levelSize = queue.size(); + for (int i = 0; i < levelSize; i++) { + TreeNode node = queue.poll(); + if (node.left == null && node.right == null) return depth; + if (node.left != null) queue.offer(node.left); + if (node.right != null) queue.offer(node.right); + } + depth++; + } + return depth; + } +} + ``` + + + + + ```python + class Solution: + def minDepth(self, root): + if not root: + return 0 + queue = collections.deque([(root, 1)]) + while queue: + node, depth = queue.popleft() + if not node.left and not node.right: + return depth + if node.left: + queue.append((node.left, depth + 1)) + if node.right: + queue.append((node.right, depth + 1)) + ``` + + + + + ```cpp + class Solution { +public: + int minDepth(TreeNode* root) { + if (!root) return 0; + queue q; + q.push(root); + int depth = 1; + while (!q.empty()) { + int levelSize = q.size(); + for (int i = 0; i < levelSize; ++i) { + TreeNode* node = q.front(); + q.pop(); + if (!node->left && !node->right) return depth; // Leaf node found + if (node->left) q.push(node->left); + if (node->right) q.push(node->right); + } + ++depth; + } + return depth; + } +}; + ``` + + + + +#### Complexity Analysis + +- Time Complexity: O(n) where n is the number of nodes in the binary tree. +- Space Complexity: O(h) where h is the height of the binary tree. + + + + + +--- \ No newline at end of file From 427f075a4ebcec934248c2c5df1356f2aba2d96b Mon Sep 17 00:00:00 2001 From: Ayushmaanagarwal1121 Date: Fri, 7 Jun 2024 19:50:04 +0530 Subject: [PATCH 2/2] Update 0103-zigzag-order-traversal.md --- .../lc-solutions/0100-0199/0103-zigzag-order-traversal.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md b/dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md index 346e7911d..4a0eff5a0 100644 --- a/dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md +++ b/dsa-solutions/lc-solutions/0100-0199/0103-zigzag-order-traversal.md @@ -27,8 +27,8 @@ Output: [[3],[20,9],[15,7]] ### Constraints -- `The number of nodes in the tree is in the range $[0, 2000]$.` -- `$-100 \leq \text{Node.val} \leq 100$` +- The number of nodes in the tree is in the range $[0, 2000]$. +- $-100 \leq \text{Node.val} \leq 100$ --- @@ -123,8 +123,8 @@ public: #### Complexity Analysis -- Time Complexity: O(n) where n is the number of nodes in the binary tree. -- Space Complexity: O(h) where h is the height of the binary tree. +- Time Complexity: $O(n)$ where n is the number of nodes in the binary tree. +- Space Complexity: $O(h)$ where h is the height of the binary tree.