🚀 116 days of #200DaysOfCode Problem: 113. Path Sum II Problem Statement: Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values. Approach: Used Depth-First Search (DFS) to explore all root-to-leaf paths while tracking the current path and sum. Added valid paths to the result when a leaf was reached and the sum matched the target. Logic: DFS with backtracking efficiently explores all possible paths and ensures only valid ones are stored. This approach is intuitive for tree problems and leverages recursion for clean path management. 👉 Question link 🔗: https://lnkd.in/gEp7MAYW #LeetCode #Java #Tree #DFS #Backtracking #DSA #Coding #Algorithms #InterviewPrep #200DaysOfCode
Solved #200DaysOfCode problem 113: Path Sum II using DFS and backtracking.
More Relevant Posts
-
#98day of #100DaysOfCode 🌿 LeetCode 1302: Deepest Leaves Sum Today’s problem was about exploring the depths of a binary tree 🌳 We needed to find the sum of all the deepest leaf nodes — those that rest at the very bottom of the tree. 🧠 Approach: Two main ways to solve it: 1️⃣ Level Order Traversal (BFS): Traverse level by level; the last level’s sum is the answer. 2️⃣ Depth-first Search (DFS): Track maximum depth and accumulate the sum of deepest nodes. 📈 Complexity: Time: O(n) Space: O(n) 💬 Learning: Whether in trees or life, depth reveals value — the deeper you go, the richer the sum 🌱 #LeetCode #DSA #BinaryTree #CodingChallenge #java #ProblemSolving #LeetCodeDaily #100DaysOfCode #Algorithm
To view or add a comment, sign in
-
-
📅 Day 83 of #100DaysOfLeetCode Problem: Lowest Common Ancestor of a Binary Tree (LeetCode #236) Approach: The problem asks to find the lowest common ancestor (LCA) of two nodes in a binary tree. The LCA is the deepest node that has both given nodes as descendants. The approach used: Find the path from the root to each of the two nodes (p and q). Compare both paths node by node until they differ. The last common node before divergence is the LCA. Steps: Use DFS to record paths from the root to p and q. Iterate through both paths to find the common node. Complexity: ⏱️ Time: O(n) — traverses each node at most once. 💾 Space: O(n) — for storing paths. 🔗 Problem Link: https://lnkd.in/dKavVJpA 🔗 Solution Link: https://lnkd.in/dq-Qr2Dm #LeetCode #100DaysOfCode #BinaryTree #Recursion #TreeTraversal #DSA #Java #Algorithms #ProblemSolving #DailyCoding #CodeNewbie #LearnToCode #BuildInPublic
To view or add a comment, sign in
-
-
🌳 Day 73 of #100DaysOfCode 🌳 💡 Problem: Binary Tree Inorder Traversal – LeetCode ✨ Approach: Implemented a recursive inorder traversal to explore the binary tree in Left → Root → Right order. A perfect balance of recursion and logic — the beauty of tree traversal lies in its simplicity 🌿 📊 Complexity Analysis: Time Complexity: O(n)** — visiting each node exactly once Space Complexity: O(n)** — due to recursion stack and list storage ✅ Runtime: 0 ms (🔥 Beats 100%) ✅ Memory: 43.25 MB 🔑 Key Insight: Recursion simplifies complex tree structures into elegant solutions — one node at a time 🌱 #LeetCode #100DaysOfCode #DataStructures #BinaryTree #Recursion #ProblemSolving #DSA #AlgorithmDesign #CodeEveryday #ProgrammerJourney #Java #CodingChallenge
To view or add a comment, sign in
-
-
🌿 Day 74 of #100DaysOfCode 🌿 💡 Problem: Binary Tree Preorder Traversal – LeetCode 🚀 Approach: Used a recursive traversal to explore nodes in the order Root → Left → Right. Preorder traversal is all about visiting the leader first — just like taking the initiative before exploring possibilities! 💫 📊 Complexity Analysis: Time Complexity: O(n)** — every node is visited once Space Complexity: O(n)** — due to recursion stack ✅ Runtime: 0 ms (⚡ Beats 100%) ✅ Memory: 43.06 MB 🔑 Key Insight: Recursion helps untangle even the deepest branches — one root call at a time 🌱 #LeetCode #100DaysOfCode #BinaryTree #Recursion #CodingJourney #DSA #ProblemSolving #Java #Algorithms #ProgrammerLife #WomenInTech #CodeEveryday
To view or add a comment, sign in
-
-
Day 40 of 160 Days of DSA 🚀 Problem: Search in a Strictly Sorted Matrix Today I learned how the type of matrix sorting completely changes the search strategy. In a strictly sorted matrix: Each row is sorted in increasing order And the first element of each row is greater than the last element of the previous row This means the matrix can be treated like one fully sorted array. So the efficient approach is: Binary Search on the last column to identify the correct row. Binary Search inside that row to find the target. This gives a time complexity of: O(logn+logm)≈O(log(n⋅m))O(\log n + \log m) \approx O(\log(n \cdot m))O(logn+logm)≈O(log(n⋅m))✨ Key Takeaway: Understanding how data is sorted helps us choose the optimal searching technique — one small detail can change the entire solution. #100DaysOfCode #geeksforgeeks #dsa #java #binarysearch #codingjourney #logicbuilding #learningEveryday
To view or add a comment, sign in
-
-
🚀 112 days of #200DaysOfCode Problem: 454. 4Sum II Problem Statement: Given four integer arrays nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that: nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0. Approach: Used a HashMap to store sums of pairs from the first two arrays and their frequencies. Then iterated over all pairs from the next two arrays, checking if the complement exists in the map to count valid tuples efficiently. Logic: Harnessed hash mapping to reduce a brute-force O(n⁴) approach to O(n²), making the solution highly efficient for large inputs. 👉 Question link 🔗: https://lnkd.in/gsm79HER #LeetCode #Java #HashTable #Array #FourSum #HashMap #DSA #Coding #Algorithms #InterviewPrep #200DaysOfCode
To view or add a comment, sign in
-
-
Today, I explored how Depth First Search (DFS) works in Java by solving the “All Paths from Source to Target” problem. At first, I faced challenges like managing the same reference of path lists and understanding how backtracking actually restores the previous state. But once I understood the flow of recursion — adding, exploring, and removing nodes — everything clicked. Here’s what I learned: ✅ Always clone or backtrack when tracking paths in recursion. ✅ Understanding the call stack flow is key to debugging DFS. ✅ Visualizing the recursion tree helps a lot when things get confusing. Now, I can confidently trace how each path is built and how DFS explores all possible routes in a graph. Small wins like this make the journey of learning algorithms so rewarding! #Java #DSA #GraphAlgorithms #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
#Day14 94 – Binary Tree Inorder Traversal 💡 Approach: Used recursive depth-first search to traverse left subtree, process current node, then traverse right subtree. This follows the natural inorder sequence where nodes are visited in ascending order for BSTs. #LeetCode #Java #BinaryTree #DataStructures #Algorithms #Recursion #DFS #ProblemSolving #Coding #Tech #SoftwareEngineering #DailyCoding
To view or add a comment, sign in
-
-
👇 📅 Day 84 of #100DaysOfLeetCode Problem: Find Largest Value in Each Tree Row (LeetCode #515) Approach: The problem requires finding the largest value in each level of a binary tree. This can be efficiently solved using Level Order Traversal (BFS). For each level, store all node values and select the maximum one. Add these maximum values to the result list. Steps: Use a queue to perform level-by-level traversal. For each level, collect node values in a list. Compute the maximum value for that level and add it to the output list. Continue until the queue is empty. Complexity: ⏱️ Time: O(n) — every node is visited once. 💾 Space: O(n) — queue and result storage. 🔗 Problem Link: https://lnkd.in/eQz5GBU8 🔗 Solution Link: https://lnkd.in/eAC_3SwK #LeetCode #100DaysOfCode #BinaryTree #LevelOrderTraversal #BFS #DataStructures #Java #Algorithms #ProblemSolving #DailyCoding #CodingChallenge #BuildInPublic #LearnToCode #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