🚀 #100DaysOfCode | Day 36 📌 LeetCode – Path Sum Today I solved the Path Sum problem using a recursive Depth-First Search (DFS) approach. Given a binary tree and a target sum, determine whether there exists a root-to-leaf path such that the sum of all node values equals the target. 💡 Approach: ✔ If the node is null, return false. ✔ If it's a leaf node, check whether the remaining sum equals the node value. ✔ Recursively subtract the current node value from targetSum. ✔ Check both left and right subtrees. ✔ If either returns true → path exists. ⏱ Complexity: Time Complexity: O(n) Space Complexity: O(h) — height of the tree Binary tree problems are all about mastering recursion and understanding base cases clearly. #Java #LeetCode #DSA #BinaryTree #Recursion #ProblemSolving
Path Sum in Binary Tree with DFS Approach
More Relevant Posts
-
Day 71/100 – LeetCode Challenge ✅ Problem: #1356 Sort Integers by The Number of 1 Bits Difficulty: Easy Language: Java Approach: Custom Comparator with Bit Counting Time Complexity: O(n log n) Space Complexity: O(n) Key Insight: Sort integers based on number of set bits (1s in binary representation). If bit counts are equal, sort by natural integer order. Use Integer.bitCount() for efficient population count. Solution Brief: Converted primitive array to Integer[] for custom sorting. Applied Arrays.sort() with comparator: Compare bitCount(a) vs bitCount(b) If equal, compare integers directly using a.compareTo(b) Converted back to primitive array. #LeetCode #Day71 #100DaysOfCode #Sorting #Java #Algorithm #CodingChallenge #ProblemSolving #SortByBits #EasyProblem #BitManipulation #Comparator #DSA
To view or add a comment, sign in
-
-
🚀 Day 34 of #100DaysOfCode Solved 852. Peak Index in a Mountain Array on LeetCode ⛰️📈 🧠 Key Insight: A mountain array strictly increases to a peak and then decreases. Instead of scanning the whole array, we can use Binary Search to efficiently find the peak. ⚙️ Approach: 🔹Use binary search with two pointers left and right 🔹Compare arr[mid] with arr[mid + 1] 🔹If arr[mid] > arr[mid + 1] → peak is on the left side (including mid) 🔹Otherwise → peak is on the right side 🔹Continue until left == right, which gives the peak index ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 83/100 – LeetCode Challenge ✅ Problem: #46 Permutations Difficulty: Medium Language: Java Approach: Backtracking with Swapping Time Complexity: O(n × n!) Space Complexity: O(n) for recursion stack Key Insight: Generate all permutations by fixing each element at current position and recursively permuting remaining elements. Use swapping to avoid extra space for visited tracking. Solution Brief: Base case: When index i reaches end, add current array as permutation. Recursive case: For each position j from i to end: Swap elements at i and j Recurse on i + 1 Swap back (backtrack) to restore original order #LeetCode #Day83 #100DaysOfCode #Backtracking #Java #Algorithm #CodingChallenge #ProblemSolving #Permutations #MediumProblem #Recursion #Swapping #DSA
To view or add a comment, sign in
-
-
Merge Sorted Array (LeetCode : 88) 💻✨ In this problem, we are given two sorted arrays. The task is to create a final sorted array inside nums1 without using extra space. I used Two Pointer Technique here 👇 🔹 i → last valid index of nums1 🔹 j → last index of nums2 🔹 k → last index of nums1 (m + n - 1) We compare from the back because if we compare from the front, the data would be overwritten. 👉 If nums1[i] > nums2[j], then we put the larger value at the back. 👉 Otherwise, we put nums2[j]. 👉 Once any one of the arrays is finished, we copy the remaining elements. Time Complexity of this approach: O(m + n) Space Complexity: O(1) (In-place solution) 🚀 This problem reminded me again — If you think in the right direction, the solution becomes much easier. #LeetCode #androidDeveloper#problemslover#dsapattern#twopointer#dailyChallenge #DSA #Java #TwoPointerTechnique #ProblemSolving #CodingJourney #SoftwareDevelopment #InPlaceAlgorithm #DataStructures #AlgorithmThinking
To view or add a comment, sign in
-
-
After exploring path-based recursion, I moved to generating all subsets of a string. This introduced a very clean pattern - For every element, you either include it or exclude it. That’s it. But that “either-or” creates a full decision tree. What became clear here: - Every element creates two branches. - Total subsets = 2ⁿ (tree expansion becomes visible). - Recursion can systematically explore combinations. - The structure is more important than the syntax. Core logic : if (index == str.length()) { System.out.println(current); return; } subset(index + 1, current + str.charAt(index)); // include subset(index + 1, current); // exclude This is where recursion started feeling predictable. Once the pattern is seen, similar problems start looking connected. #recursion #java #dsajourney #backtracking #problemSolving
To view or add a comment, sign in
-
Day 89/100 – LeetCode Challenge ✅ Problem: #459 Repeated Substring Pattern Difficulty: Easy Language: Java Approach: Substring Division Check Time Complexity: O(n²) worst case Space Complexity: O(n) Key Insight: A string can be formed by repeating its substring if the substring length divides the string length. Check all divisors of length from largest to smallest for efficiency. Solution Brief: Iterated i from l/2 down to 1 (larger substrings first). If l % i == 0, substring length divides string length. Constructed repeated pattern by appending substring m = l/i times. Compared constructed string with original. #LeetCode #Day89 #100DaysOfCode #String #Java #Algorithm #CodingChallenge #ProblemSolving #RepeatedSubstring #EasyProblem #Substring #Pattern #DSA
To view or add a comment, sign in
-
-
🚀 Day 20 of #100DaysOfCode 🌱 Topic: Binary Trees / Recursion ✅ Problem Solved: LeetCode 105 – Construct Binary Tree from Preorder and Inorder Traversal 🛠 Approach: Used preorder to identify the root node. Used inorder to split left and right subtrees. Stored inorder indices in HashMap for fast lookup. Recursively built left and right subtrees using calculated ranges. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) (HashMap) #100DaysOfCode #Day20 #DSA #BinaryTree #Recursion #Java #LeetCode #ProblemSolving #CodingJourney #Consistency #LearningInPublic #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 18 of My Daily DSA Challenge Today I solved the "Merge Two Sorted Lists" problem. This problem strengthened my understanding of linked list manipulation and recursive thinking. The main challenge was carefully comparing nodes and attaching them in sorted order while maintaining proper references between nodes. Key learnings: Recursive solutions can make linked list problems more intuitive. Base cases are critical in recursion to avoid infinite calls. Handling null conditions correctly prevents runtime errors. Problems like this remind me that data structures are not just about code syntax, but about understanding how data moves and connects internally. Consistency is slowly building stronger logic and confidence with each passing day. #DSA #Java #LinkedList #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
🚀 DSA Consistency - Day 57 Today I solved the Same Tree problem on LeetCode, which focuses on understanding binary tree structure comparison using recursion. The goal is to determine whether two binary trees are structurally identical and have the same node values. 🧠 Approach: Recursive Tree Traversal To check if two trees are the same: 1️⃣ If both nodes are null, they are identical → return true. 2️⃣ If one node is null and the other is not, trees differ → return false. 3️⃣ If node values are different, trees are not identical. 4️⃣ Recursively check: Left subtree of both trees Right subtree of both trees Both must match for the trees to be identical. ⏱ Complexity Analysis Time Complexity: O(n) Each node is visited once. Space Complexity: O(h) Due to recursion stack (where h is the height of the tree). #DSA #LeetCode #BinaryTree #Java #CodingJourney #Consistency #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
#Day46 of #100DaysDSAChallenge 🚀 Solved LeetCode 237: Delete Node in a Linked List This problem is interesting because we are not given the head of the linked list — only the node that needs to be deleted. So we can’t traverse from the start or adjust previous pointers directly. Approach: 1️⃣ Value Shift Technique Copy the value of the next node into the current node and move forward until reaching the last node. Then remove the final duplicate node by updating the previous pointer. Time: O(n) Space: O(1) Another great reminder that sometimes problems test how well you understand data structure behavior, not just traversal. 🔗 Solutions Repository: https://lnkd.in/g_rSFCh8 #100DaysOfDSA #DSA #LeetCode #Java #Algorithms #DataStructures #ProblemSolving #CodingJourney #LearningInPublic #InterviewPrep #PlacementPreparation #KunalKushwaha #Striver #GeeksForGeeks
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