✅ Day 34 of #100DaysOfCode Challenge 📘 LeetCode Problem 111: Minimum Depth of Binary Tree 🧩 Problem Statement: 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. Example: Input: root = [3,9,20,null,null,15,7] Output: 2 Explanation: The shortest path is 3 → 9, so the minimum depth = 2. 💡 Approach (Simple Recursive): If the tree is empty → return 0. Recursively find the left and right subtree depths. If one side is missing, the depth = 1 + depth of the other side. Otherwise, take 1 + min(leftDepth, rightDepth). 💻 Java Code (Easy Version): class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); // If one child is missing, take the non-null side if (left == 0 || right == 0) return 1 + left + right; return 1 + Math.min(left, right); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Growing one problem at a time 🌱 #Day34 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge #ProblemSolving
Solving LeetCode Problem 111: Minimum Depth of Binary Tree
More Relevant Posts
-
✅ Day 34 of #100DaysOfCode Challenge 📘 LeetCode Problem 111: Minimum Depth of Binary Tree 🧩 Problem Statement: 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. Example: Input: root = [3,9,20,null,null,15,7] Output: 2 Explanation: The shortest path is 3 → 9, so the minimum depth = 2. 💡 Approach (Simple Recursive): If the tree is empty → return 0. Recursively find the left and right subtree depths. If one side is missing, the depth = 1 + depth of the other side. Otherwise, take 1 + min(leftDepth, rightDepth). 💻 Java Code (Easy Version): class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); // If one child is missing, take the non-null side if (left == 0 || right == 0) return 1 + left + right; return 1 + Math.min(left, right); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Growing one problem at a time 🌱 #Day34 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 34 of #100DaysOfCode Challenge 📘 LeetCode Problem 111: Minimum Depth of Binary Tree 🧩 Problem Statement: 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. Example: Input: root = [3,9,20,null,null,15,7] Output: 2 Explanation: The shortest path is 3 → 9, so the minimum depth = 2. 💡 Approach (Simple Recursive): If the tree is empty → return 0. Recursively find the left and right subtree depths. If one side is missing, the depth = 1 + depth of the other side. Otherwise, take 1 + min(leftDepth, rightDepth). 💻 Java Code (Easy Version): class Solution { public int minDepth(TreeNode root) { if (root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); // If one child is missing, take the non-null side if (left == 0 || right == 0) return 1 + left + right; return 1 + Math.min(left, right); } } ⚙ Complexity: ⏱ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Growing one problem at a time 🌱 #Day34 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 33 of #100DaysOfCode Challenge 📘 LeetCode Problem 110: Balanced Binary Tree 🧩 Problem Statement: Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Explanation: Both left and right subtrees of every node differ in height by at most 1. 💡 Approach (Simple & Efficient): Use a recursive function to calculate the height of each subtree. If any subtree is unbalanced, return -1. Otherwise, return the height of the tree. If the final result is -1 → tree is not balanced. 💻 Java Code: class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } } ⚙ Complexity: ⏱ Time: O(n) → Each node visited once 💾 Space: O(h) → Recursion stack (h = tree height) 🌱 Another step towards mastering Binary Trees! #Day33 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BalancedTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙ Complexity: ⏱ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
📅 Day 81 of #100DaysOfLeetCode Problem: Insert into a Binary Search Tree (LeetCode #701) Approach: The task is to insert a new node with a given value into a Binary Search Tree (BST). Start from the root and recursively find the correct position: If the new value is smaller than the current node’s value, go to the left subtree. Otherwise, go to the right subtree. When a null spot is found, insert a new node there. The BST property is preserved throughout this process. Complexity: ⏱️ Time: O(h) — where h is the height of the tree. 💾 Space: O(h) — recursive call stack. 🔗 Problem Link: https://lnkd.in/dCS7zxVG 🔗 Solution Link: https://lnkd.in/dxB4ZNtV #LeetCode #100DaysOfCode #BinarySearchTree #Recursion #Java #TreeTraversal #DSA #Algorithms #CodingChallenge #ProblemSolving #CodeNewbie #StudyWithMe #BuildInPublic #LearnToCode #DailyCoding
To view or add a comment, sign in
-
-
🔥 LeetCode Day--- 4 | “Median of Two Sorted Arrays” (Hard, Java) Today’s challenge was one of those that really test your logic, patience, and understanding of binary search. This problem wasn’t about just merging two sorted arrays — it was about thinking smarter 🧠. Instead of brute-forcing through both arrays (O(m+n)), I implemented a binary partition approach to achieve O(log(min(m, n))) efficiency 💡 What I learned today: Always choose the smaller array for binary search — it makes the partition logic simpler. Handle boundaries carefully with Integer.MIN_VALUE and Integer.MAX_VALUE. The goal is to find the perfect partition where: Left half ≤ Right half Elements are balanced across both arrays Once that’s done → median can be easily calculated! ✅ Result: Accepted | Runtime: 0 ms 🚀 Hard problem turned into a logic puzzle that was actually fun to solve! 🧩 Concepts Strengthened: Binary Search Partitioning Logic Edge Case Handling Mathematical Thinking #LeetCode #Day4 #Java #BinarySearch #ProblemSolving #CodingChallenge #DataStructures #Algorithms #CodeEveryday #DeveloperJourney #TechLearning #LeetCodeHard #CodingCommunity
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
🌳 Day 65 of #LeetCode Journey 🔹 Problem: 106. Construct Binary Tree from Inorder and Postorder Traversal 🔹 Difficulty: Medium 🔹 Language: Java Today’s problem is about rebuilding a binary tree when you’re given its inorder and postorder traversal arrays. 🧩 Key Idea: The last element in postorder is always the root. In inorder traversal, elements to the left of the root are in the left subtree, and elements to the right are in the right subtree. We recursively build the tree using this property. 💡 Approach: Start from the end of the postorder array to get the root. Use a hashmap to store inorder indices for O(1) lookup. Recursively construct the right subtree first, then the left subtree (since we’re moving backward in postorder). 🧠 Concepts reinforced: Recursion HashMap for index lookup Understanding inorder & postorder relationships 🔥 Another step forward in mastering tree construction problems! #LeetCode #100DaysOfCode #Java #CodingChallenge #DataStructures #BinaryTree #Recursion #ProblemSolving #ProgrammingJourney
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development