🔥 Day 171 of My LeetCode Journey Problem 112: Path Sum 💡 Problem Insight: Today’s problem was about checking whether a binary tree has a root-to-leaf path whose sum equals a target value. The key detail here is root-to-leaf — not any path. Missing that leads to wrong answers. 🧠 Concept Highlight: The solution is a clean use of DFS (recursion): Subtract the current node’s value from the target Move to left and right subtrees When you reach a leaf, check if the remaining sum is zero This ensures you explore all valid paths without unnecessary work. 💪 Key Takeaway: Tree problems often reduce to accumulating state along a path. Instead of storing paths, update the condition as you traverse. ✨ Daily Reflection: This problem reinforced how recursion naturally fits tree traversal. Once you think in terms of paths and state, the solution becomes straightforward. #Day171 #LeetCode #BinaryTree #DFS #PathSum #ProblemSolving #DSA #CodingJourney #Consistency
LeetCode 112: Path Sum in Binary Tree
More Relevant Posts
-
🔥 Day 173 of My LeetCode Journey Problem 113: Path Sum II 💡 Problem Insight: Today’s problem extends Path Sum — instead of just checking existence, you must return all root-to-leaf paths whose sum equals the target. This shift from a Boolean list makes the problem more complex. 🧠 Concept Highlight: The solution uses DFS + backtracking: Traverse the tree while maintaining the current path Subtract node values from the target sum When a valid leaf is reached, store the path Backtrack to explore other possibilities Backtracking is essential to avoid mixing paths. 💪 Key Takeaway: Whenever a problem asks for all possible paths/combinations, backtracking is the go-to technique. Managing state correctly is more important than traversal itself. ✨ Daily Reflection: This problem reinforced the need for discipline when storing results. Without proper backtracking, solutions become incorrect quickly. #Day173 #LeetCode #BinaryTree #DFS #Backtracking #PathSum #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 75/150 🚀 LeetCode 101: Symmetric Tree 🧠 Problem Given the root of a binary tree, check whether it is a mirror of itself. 📌 Example Input → root = [1,2,2,3,4,4,3] Output → true 💡 Approach • Use recursion • Compare left subtree with right subtree • Check mirror structure • Continue recursively ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day75 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 10/100 — LeetCode Challenge Solved 3Sum The brute force approach is simple: Try all triplets → O(n³) But that’s not scalable. 💡 Optimized Approach: 1) First, sort the array 2) Fix one element 3) Use two pointers to find the remaining pair 👉 This reduces the complexity significantly. Also handled duplicates carefully to avoid repeated triplets. 🧠 Time Complexity: O(n²) 💾 Space Complexity: O(1) (ignoring output) 💡 What I learned: Sometimes optimization is not about complex logic, but about: 1) Sorting 2) Using patterns like two pointers 3) Eliminating unnecessary work This problem felt like a step up from previous ones. Staying consistent. #LeetCode #DSA #100DaysOfCode #Cpp #TwoPointers #CodingJourney
To view or add a comment, sign in
-
-
🚀 LeetCode 74 — Search a 2D Matrix | Solved with 100% Runtime ✅ Solved this interesting Binary Search problem today! 🔍 Problem Insight: Given a 2D matrix where: • Each row is sorted • First element of each row > last element of previous row 💡 Approach: Used a two-step Binary Search: 1️⃣ First, identify the correct row using binary search 2️⃣ Then, apply binary search within that row ⚡ Time Complexity: O(log m + log n) ≡ O(log(m × n)) ✨ Key Learning: A 2D matrix like this can also be treated as a flattened sorted array, enabling a single binary search approach as well. 📈 Result: 0 ms runtime (Beats 100%) 💯 Consistency + pattern recognition = growth 🚀 Grateful to Pratyush Narain for the guidance and clear explanations. #LeetCode #DSA #BinarySearch #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 37 of Consistency | #75DaysLeetCodeChallenge 🧠 Today’s Problem: Subtree of Another Tree (#572) 💡 Key Learning: This problem deepens understanding of tree comparison + recursion, combining subtree traversal with structural validation. ⚡ Approach: Use recursion (DFS) Traverse each node of root At each node, check if trees are identical If not, recursively check left & right 🧠 Why this works: Checks every possible subtree Uses same-tree logic to validate structure & values 🔥 Result: ✔️ Runtime: 35 ms (Beats 86.67%) ⚡ Time Complexity: O(n * m) ⚡ Space Complexity: O(n) (recursion stack) 📈 A great problem that combines traversal and comparison—very useful for mastering tree-based questions. A big thanks to Shivam Singh, Nikhil Yadav & Akshat Tiwari for this amazing challenge 🙌 Consistency is compounding. Keep going. 💪 #Day37 #LeetCode #DSA #CodingJourney #BinaryTree #Recursion #DFS #100DaysOfCode
To view or add a comment, sign in
-
-
Day 74/150 🚀 LeetCode 226: Invert Binary Tree 🧠 Problem Given the root of a binary tree, invert the tree and return its root. 📌 Example Input → root = [4,2,7,1,3,6,9] Output → [4,7,2,9,6,3,1] 💡 Approach • Use recursion • Swap left and right child • Recursively invert left subtree • Recursively invert right subtree ⏱ Time Complexity O(n) 📦 Space Complexity O(h) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) #Day74 #LeetCode #DSA #BinaryTree #Recursion #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 66 of #100DaysOfCode Today, I solved LeetCode 73 – Set Matrix Zeroes, a classic problem that tests in-place matrix manipulation and optimization techniques. 💡 Problem Overview: Given a matrix, if any cell contains 0, its entire row and column must be set to 0. The challenge is to perform this efficiently without using excessive extra space. 🧠 Approach: To solve this optimally, I focused on: ✔️ Using the first row and first column as markers ✔️ Tracking whether the first row/column initially contained zero ✔️ Updating the matrix in-place based on these markers This avoids using additional space and achieves optimal performance. ⚡ Key Takeaways: In-place algorithms help reduce space complexity Using matrix itself as storage is a powerful optimization trick Handling edge cases (first row/column) is critical 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(1) Improving problem-solving skills one day at a time 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
Day 84/150 🚀 LeetCode 124: Binary Tree Maximum Path Sum 🧠 Problem Find the maximum path sum in a binary tree. A path can start and end at any node. 💡 Approach • Use DFS (post-order traversal) • Ignore negative paths → max(0, left/right) • Update global max using left + right + root • Return max single path for recursion ⏱ Time Complexity O(n) 📦 Space Complexity O(h) (recursion stack) ✅ Result: Accepted ⚡ Runtime: 0 ms (Beats 100%) Hard problems getting comfortable 🌳 #Day84 #LeetCode #BinaryTree #DFS #Recursion #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 82 of #100DaysOfCode Today, I solved LeetCode 41 – First Missing Positive, a classic hard problem that tests in-place array manipulation and optimization. 💡 Problem Overview: Given an unsorted array, the goal is to find the smallest missing positive integer in O(n) time and O(1) space. 🧠 Approach: ✔️ Used index-based placement (cyclic sort idea) ✔️ Placed each number x at index x - 1 whenever possible ✔️ Ignored negative numbers and values out of range ✔️ Finally, scanned the array to find the first index where the value is incorrect This ensures optimal time and space complexity without using extra data structures. ⚡ Key Takeaways: In-place manipulation can eliminate the need for extra space Index mapping is a powerful trick for array problems Hard problems often rely on simple but clever observations 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) Solving challenging problems and strengthening core fundamentals 🚀 #LeetCode #100DaysOfCode #DSA #Arrays #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep #HardProblems
To view or add a comment, sign in
-
-
🚀 LeetCode 219 — Contains Duplicate II | Solved ✅ Today I tackled a classic Sliding Window + Hashing problem. 🔍 Problem: Check if there exist two equal elements such that their index difference is at most k. 💡 Approach: Used a sliding window of size k with a hash set to track elements. If an element already exists in the window → duplicate found. ⚡ Complexity: Time: O(n) Space: O(k) ✨ Key Learning: Instead of tracking frequency, sometimes just tracking presence is enough — simplifying both logic and performance. Consistency is the real key 🔑 #LeetCode #DSA #CodingJourney #SlidingWindow #Hashing
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