From 299cf991df0438de3af1f419b4b5793e0c007a86 Mon Sep 17 00:00:00 2001 From: Ayushmaanagarwal1121 Date: Fri, 7 Jun 2024 13:58:24 +0530 Subject: [PATCH 1/4] Added solution for lt problem 111 --- .../0100-0199/0111-minimum-depth.md | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md diff --git a/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md new file mode 100644 index 000000000..aa52f6dfb --- /dev/null +++ b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md @@ -0,0 +1,133 @@ +--- +id: minimum-depth-of-binary-tree +title: Minimum Depth of Binary Tree +sidebar_label: 0111-Minimum Depth +tags: + - Java + - Python + - C++ +description: "Find the minimum depth of a binary tree, which is the number of nodes along the shortest path from the root node down to the nearest leaf node." +--- + +## Problem Description + +Given a binary tree, find its minimum depth. + +The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. + +### Examples + +**Example 1:** + +![LeetCode Problem - Binary Tree](https://assets.leetcode.com/uploads/2020/10/12/ex_depth.jpg) +``` +Input: root = [3,9,20,null,null,15,7] +Output: 2 +``` + + +### Constraints + +- `The number of nodes in the tree is in the range [0, 105].` +- `-100 <= Node.val <= 100` + +--- + +## Solution for Binary Tree Problem + +### Intuition And Approach + +To find the minimum depth of a binary tree, we can perform a depth-first search (DFS) or a breadth-first search (BFS). Here, we will use BFS for simplicity. + +Breadth-First Search (BFS): Traverse the tree level by level, stopping as soon as we encounter a leaf node. The depth of the first leaf node encountered will be the minimum depth of the tree. + + + + + +#### 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 d12904c76a26380e626518bee0cff955da9d8549 Mon Sep 17 00:00:00 2001 From: Ayushmaanagarwal1121 Date: Fri, 7 Jun 2024 14:01:12 +0530 Subject: [PATCH 2/4] update --- dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md index aa52f6dfb..8bec02675 100644 --- a/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md +++ b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md @@ -1,7 +1,7 @@ --- id: minimum-depth-of-binary-tree title: Minimum Depth of Binary Tree -sidebar_label: 0111-Minimum Depth +sidebar_label: 0111-Minimum Depth Of Binary tree tags: - Java - Python From b121a92e6b3fa6e883c5f306b679581d3c5af8c8 Mon Sep 17 00:00:00 2001 From: Ayushmaan Agarwal <118350936+Ayushmaanagarwal1211@users.noreply.github.com> Date: Fri, 7 Jun 2024 19:53:23 +0530 Subject: [PATCH 3/4] Update 0111-minimum-depth.md --- .../lc-solutions/0100-0199/0111-minimum-depth.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md index 8bec02675..5529d6634 100644 --- a/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md +++ b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md @@ -28,8 +28,8 @@ Output: 2 ### Constraints -- `The number of nodes in the tree is in the range [0, 105].` -- `-100 <= Node.val <= 100` +- The number of nodes in the tree is in the range [0, 105]. +- -100 <= Node.val <= 100 --- @@ -123,11 +123,11 @@ 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. ---- \ No newline at end of file +--- From 78ddf8926a94608f715021ed80e9371ca82471d1 Mon Sep 17 00:00:00 2001 From: Ayushmaan Agarwal <118350936+Ayushmaanagarwal1211@users.noreply.github.com> Date: Fri, 7 Jun 2024 23:26:59 +0530 Subject: [PATCH 4/4] Update 0111-minimum-depth.md --- dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md index 5529d6634..1911a32b6 100644 --- a/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md +++ b/dsa-solutions/lc-solutions/0100-0199/0111-minimum-depth.md @@ -29,7 +29,7 @@ Output: 2 ### Constraints - The number of nodes in the tree is in the range [0, 105]. -- -100 <= Node.val <= 100 +- $-100 <= Node.val <= 100$ ---