Day 58/100 of #100DaysOfCode 🚀 Minimum Absolute Difference in a BST (Leetcode) For a Binary Search Tree, an in-order traversal produces values in sorted order.So, the minimum absolute difference will always be between two consecutive nodes in this order. Approach: Used DFS (in-order traversal): left → root → right Maintained: prev → previously visited node value minDifference → smallest difference found so far At each node: Calculated root.val - prev (only if prev exists) Updated minDifference with the minimum value Updated prev after visiting the current node. #Day58 #100DaysOfCode #LeetCode #BST #BinaryTree #InorderTraversal #DFS #Java #DSA #ProblemSolving #Consistency
Minimum Absolute Difference in BST via Inorder Traversal
More Relevant Posts
-
Day 4 of #60daysofLeetcode, #7. Reverse Integer - https://lnkd.in/dqA_ciDP is the question. Solution. To reverse the integer, we repeatedly extract the last digit using modulo 10, then build the result from left to right. Each iteration: extract the last digit, update result = result * 10 + digit, and divide input by 10. We continue until the input becomes zero. Before each result update, we check if the operation would cause overflow, and if so, return 0. Time Complexity is O(log n) or O(d) where d is the number of digits. Space complexity is O(1). #LeetCode, #Java, #DSA, #LearningInPublic
To view or add a comment, sign in
-
-
Day 15 of #200daysofdsa LeetCode 13 | Roman to Integer (Java) Solved this problem using a HashMap + traversal from right to left. Key logic: If current value < next value → subtract Else → add *>Time Complexity: O(n) *>Space Complexity: O(1) (fixed map size) *>Clean handling of subtractive cases like IV, IX, XL, XC, CD, CM Consistency is the real progress #LeetCode #DSA #Java #Strings #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
📝 Day 17/30 – LeetCode #238 (Product of Array Except Self) | Java The straightforward solution using division fails due to edge cases involving zeros and is explicitly disallowed. A brute-force approach would also be inefficient with O(n²) time complexity. By computing prefix products and suffix products, I was able to calculate the result for each index without using division. Each element in the output array represents the product of all numbers before and after it, resulting in an O(n) time solution with constant extra space. This problem highlighted how breaking a problem into directional passes can simplify complex constraints. #LeetCode #Java #DSA #Arrays #PrefixSum #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
✅ Day 62/75 – Non-overlapping Intervals 📌 Problem Given a list of intervals, remove the minimum number of intervals so that the remaining intervals are non-overlapping. 👉 Intervals that only touch at a point (e.g., [1,2] and [2,3]) are considered non-overlapping. 💡 Approach (Greedy) 🔺 Sort intervals by their ending time 🔺 Always keep the interval that ends earliest 🔺 If the next interval overlaps with the previous one, remove it 📘 Example 🔺Input: [[1,2], [2,3], [3,4], [1,3]] 🔺Output: 1 ⏱ Complexity 🔺Time: O(n log n) 🔺Space: O(1) #Day62 #75DaysOfCode #GreedyAlgorithm #Java #DSA #ProblemSolving #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 15 of #100DaysOfCode Solved Reverse Linked List on LeetCode 🔄🔗 🧠 Key insight: Reversing a linked list is all about careful pointer manipulation. By tracking prev, current, and next, we can reverse links in-place without extra memory. ⚙️ Approach: 🔹Initialize prev = null, current = head 🔹Store next node before breaking the link 🔹Reverse current.next to point to prev 🔹Move pointers forward until the list ends ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 23 / 100 | Add Two Numbers -Approach : O(n) - Traverse both linked lists simultaneously. - Maintain a carry variable to handle sums that are greater than or equal to 10. - Use a ans node to simplify the construction of the result list. - At each step, compute the sum as: sum = val1 + val2 + carry. - Update the carry as carry = sum / 10 and store sum % 10 in a new node. - Continue this process until both lists and the carry are exhausted. - Finally, return ans.next. #100DaysOfCode #LeetCode #Java #DSA #LinkedList #ProblemSolving
To view or add a comment, sign in
-
-
📅 Day 90 of #100DaysOfLeetCode Problem: 3634. Minimum Removals to Balance Array Difficulty: Medium Key Insight: Instead of deciding which elements to remove, focus on keeping the largest possible balanced subarray where max ≤ min × k. Minimum removals = total elements − size of this subarray. Approach: Sort the array to make min/max tracking easy Use a sliding window Expand the right pointer Shrink the left pointer whenever nums[j] > k × nums[i] Track the maximum valid window length Complexity: Time: O(n log n) Space: O(1) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
Solved one of the classic Binary Tree problems on LeetCode: Validate Binary Search Tree. At first glance, the problem seems straightforward just check if left <root< right. But the real challenge is ensuring that every node respects the global BST constraints, not just its immediate parent. Problems like this remind me how important it is to think beyond local conditions and consider global invariants in tree structures. #LeetCode #DataStructures #Java #BinaryTree #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 Solved Continuous Subarray Sum using Prefix Sum & HashMap. ➤ Approach (O(n), O(n) space): — Maintain a running totalSum — Compute remainder = totalSum % k — Store the first index where each remainder appears — If the same remainder appears again and the distance between indices ≥ 2, a valid subarray exists ➤ Key Insight: If two prefix sums have the same remainder when divided by k, their difference is a multiple of k. #LeetCode #Java #DSA #PrefixSum #HashMap #ProblemSolving #20DaysChallenge #Consistency
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