Day 90 of #100DaysOfCode Today I solved "Path Sum III" on LeetCode using a DFS + Recursion approach. Key Idea: We need to count all paths where the sum of node values equals the target. The path doesn’t have to start from the root — it can start from any node! Approach: • For every node, treat it as a starting point • Use DFS to explore all downward paths • Reduce the target at each step (target - node->val) • If a node matches the remaining target → count it • Repeat the process for left and right subtrees Why this works: Every node gets a chance to act as the starting point, and DFS ensures we explore all possible paths efficiently. Concepts Used: • Binary Trees • Depth First Search (DFS) • Recursion • Backtracking (implicit) Time Complexity: O(n²) in worst case Space Complexity: O(h) This problem helped me understand how to explore all possible paths in a tree, not just root-based ones — a big step forward in mastering tree problems From single path problems → to handling multiple dynamic paths… growing every day #Day90 #100DaysOfCode #LeetCode #BinaryTree #DFS #Cpp #CodingJourney
Mastering Tree Problems with DFS and Recursion on LeetCode
More Relevant Posts
-
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 32/75 🚀 Solved Maximum Depth of Binary Tree (LeetCode 104) today! ✅ All 39/39 test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 18.94 MB (Beats ~77%) 🔍 Approach: Used recursion (DFS) to calculate depth of the tree. ✔️ If node is NULL → return 0 ✔️ Recursively find depth of left subtree ✔️ Recursively find depth of right subtree ✔️ Return 1 + max(leftDepth, rightDepth) This naturally explores the tree depth-first. 💡 Key Learning: Tree problems often become intuitive with recursion. Think in terms of subproblems (left + right) and combine results. Consistency + recursion thinking = strong tree concepts 🌳 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
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 23/75 🚀 Solved Equal Row and Column Pairs (LeetCode 2352) today! ✅ All test cases passed ⚡ Runtime: 11 ms (Beats ~74%) 💾 Memory: 30.44 MB (Beats ~81%) 🔍 Approach: Converted rows into a comparable format and matched them with columns. Transposed the matrix to easily access columns as rows. Then: ✔️ Compared each row with every column ✔️ Counted pairs where both are identical 💡 Key Learning: Matrix problems often become easier when you transform the data (like transpose). Thinking in terms of patterns instead of positions makes comparisons simpler. Consistency + small improvements = big progress 💪 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
🚀 Day 85 of #100DaysOfCode Today, I solved LeetCode 79 – Word Search, a classic problem that combines backtracking with grid traversal. 💡 Problem Overview: Given a 2D grid of characters and a word, the task is to determine if the word exists in the grid by forming it through sequentially adjacent cells (horizontal or vertical), without reusing any cell. 🧠 Approach: ✔️ Applied DFS with backtracking ✔️ Explored all possible paths starting from matching characters ✔️ Marked cells as visited during traversal and backtracked when needed ✔️ Ensured each cell is used only once per path ⚡ Key Takeaways: Backtracking is essential for exploring multiple possibilities DFS helps in exploring paths deeply Proper state management (visited/unvisited) is crucial 📊 Complexity Analysis: Time Complexity: O(n × m × 4^L) (L = length of word) Space Complexity: O(L) (recursion stack) Building strong recursion and backtracking skills 🚀 #LeetCode #100DaysOfCode #DSA #Backtracking #DFS #Recursion #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode Day 77/100 – Permutations 🧠 Problem: Given an array of distinct integers, return all possible permutations. 👉 Order matters here → [1,2,3] ≠ [3,2,1] 💡 Core Idea This is a classic Backtracking + Swapping problem 🔥 1️⃣ Fix one element at current index 2️⃣ Swap it with every possible element 3️⃣ Recursively generate permutations for remaining 4️⃣ Backtrack (swap back) 👉 Swap → Recurse → Undo 📚 Key Learnings 1. Backtracking with swapping technique 2. Generating all permutations efficiently 3. Understanding recursion tree deeply ⏱️ Complexity Time Complexity: O(n!) Space Complexity: O(n) (recursion stack) #100DaysOfCode #Day77 #LeetCode #DSA #Backtracking #Recursion #Algorithms #CodingJourney #ProblemSolving #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 8 🚀 Solved: Diameter of Binary Tree (LeetCode 543) This problem builds directly on tree depth and recursion. The diameter is the length of the longest path between any two nodes. 💡 My approaches: 🔹 Approach 1 (Brute Force): For every node, compute left and right subtree heights Check all possible paths Results in repeated computations 🔹 Approach 2 (Optimal – DFS): Compute height using postorder traversal At each node, update diameter as left_height + right_height Done in a single traversal 💡 Key learning: While calculating height, we can simultaneously track the best diameter instead of recomputing values. ⏱️ Time Complexity: Brute Force → O(n²) Optimal → O(n) 🔗 GitHub: https://lnkd.in/gnVqwUNQ #DSA #LeetCode #Coding
To view or add a comment, sign in
-
🔥 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
-
-
📱 LeetCode 17 – Letter Combinations of a Phone Number | Mastering Backtracking Solved a classic recursion problem today that perfectly demonstrates the power of backtracking and combination building. The challenge: given a string of digits (2–9), generate all possible letter combinations based on the traditional phone keypad mapping. 💡 Key Insight: Each digit maps to multiple characters, and the goal is to explore all possible combinations by picking one character from each digit. This naturally leads to a backtracking approach, where: We build combinations step by step Explore all possibilities recursively Backtrack to try different paths ⚙️ Approach: Use a mapping of digits → letters Traverse the input using recursion Append characters and move forward Store the result when a valid combination is formed 🚀 Complexity: Time: O(4ⁿ) Space: O(n) (recursion stack) 📌 What I learned: How to think in terms of recursion trees Efficiently generating combinations Writing clean and structured backtracking code Problems like this build a strong foundation for tackling advanced topics like DFS, recursion, and combinatorial algorithms. #LeetCode #DSA #Backtracking #Recursion #ProblemSolving #CodingJourney #SoftwareEngineering
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