🚀 Day 69 of #100DaysOfCode Solved 98. Validate Binary Search Tree on LeetCode 🔗 🧠 Key Insight: A valid BST must satisfy: 👉 Left subtree → all values < root 👉 Right subtree → all values > root 👉 This must hold for every node (not just immediate children) ⚙️ Approach (Range Validation - DFS): 1️⃣ Start with full range: 🔹 (-∞, +∞) 2️⃣ For each node: 🔹 Check if min < node.val < max 🔹 If not → ❌ invalid BST 3️⃣ Recurse left: 🔹 Range becomes (min, node.val) 4️⃣ Recurse right: 🔹 Range becomes (node.val, max) 5️⃣ Return true only if both subtrees are valid ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) #100DaysOfCode #LeetCode #DSA #BinaryTree #BST #Recursion #DFS #Java #InterviewPrep #CodingJourney
Validating Binary Search Tree on LeetCode with DFS
More Relevant Posts
-
𝐃𝐚𝐲 𝟔𝟑 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on rotating an array to the right by k steps efficiently. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Rotate Array 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐑𝐞𝐯𝐞𝐫𝐬𝐚𝐥 𝐓𝐞𝐜𝐡𝐧𝐢𝐪𝐮𝐞 • First normalized k using k % n • Reversed the entire array • Reversed the first k elements • Reversed the remaining elements This results in the desired rotation. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Reversal technique is a powerful in-place trick • Breaking a problem into steps simplifies logic • Modulo helps handle large values of k • In-place operations reduce space complexity 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the best solution is not shifting elements — but rearranging them smartly. 63 days consistent 🚀 On to Day 64. #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟖𝟏 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding two unique numbers where all others appear twice. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Single Number III 🔗 https://lnkd.in/dEQaaF3F 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐁𝐢𝐭 𝐌𝐚𝐧𝐢𝐩𝐮𝐥𝐚𝐭𝐢𝐨𝐧 (𝐎𝐩𝐭𝐢𝐦𝐚𝐥) Steps: • XOR all elements → gives a ⊕ b (two unique numbers) • Find rightmost set bit → differentiates a & b • Divide numbers into 2 groups based on that bit • XOR each group → get the two unique numbers 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • XOR cancels duplicate elements • Bit tricks help split data into meaningful groups • Problems can often be optimized from O(n²) → O(n) • Understanding bit-level operations is very powerful 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 When duplicates cancel out, think in terms of XOR and bit patterns. 81 days consistent 🚀 On to Day 82. #DSA #Arrays #BitManipulation #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Day 83/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Validate Binary Search Tree A core problem that tests your understanding of BST rules beyond just parent-child relationships. Problem idea: Check whether a given binary tree is a valid BST. Key idea: Maintain a valid range (min, max) for every node Why? • BST rule applies to the entire subtree, not just immediate children • Left subtree must be strictly less than root • Right subtree must be strictly greater than root • Each node must lie within its allowed range How it works: • Start with range (-∞, +∞) • For each node: • Check if value lies within (min, max) • Recursively validate left subtree with updated max = node.val • Recursively validate right subtree with updated min = node.val Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: Never validate BST using only local checks — always think in terms of global constraints (ranges). 🔥 This pattern is extremely common in tree validation problems. Day 83 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #Recursion #TreeTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 39/100 – LeetCode Challenge Problem: Power of Two Today I solved the “Power of Two” problem, which involves determining whether a given integer is a power of 2. Instead of using loops or repeated division, I used a bit manipulation trick. The key observation is that a power of two has exactly one set bit in its binary representation. So, for any number n > 0, if (n & (n - 1)) == 0, then it is a power of two. This works because subtracting 1 flips all bits after the only set bit, and performing an AND operation clears it completely. The solution runs in O(1) time and uses O(1) space, making it extremely efficient. This problem reinforced how powerful bit manipulation can be for optimizing simple checks and reducing time complexity. Thirty-nine days in. Small optimizations are starting to feel natural. #100DaysOfLeetCode #Java #DSA #BitManipulation #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 87/100 – 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 🚀 Problem: 228. 𝐒𝐮𝐦𝐦𝐚𝐫𝐲 𝐑𝐚𝐧𝐠𝐞𝐬 Today I solved a problem where we need to summarize consecutive numbers in a sorted unique array into ranges. 🔑 𝐈𝐝𝐞𝐚: Traverse the array and keep extending the range while consecutive numbers continue. Once the sequence breaks, close the range and store it. 💡 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Start with the first element as start Move forward while nums[i] + 1 == nums[i+1] If range exists → "start->end" Else → single number "start" 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Efficient single pass solution (O(n)) by grouping consecutive elements on the fly. #LeetCode #Java #ProblemSolving #DSA #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
Day 86 – Sorted List to Height-Balanced BST Today’s problem was about converting a sorted linked list into a height-balanced Binary Search Tree (BST). 💡 Key Idea: Use the slow & fast pointer technique to find the middle element of the linked list. The middle element becomes the root Left half → left subtree Right half → right subtree 🔁 Apply this recursively to build a balanced BST. 🧠 What I learned: How to simulate array-like access in a linked list Importance of finding the middle efficiently Recursion for tree construction 💻 Time Complexity: O(n log n) 💾 Space Complexity: O(log n) (recursive stack) #Day86 #DSA #Java #CodingJourney #LeetCode #BinaryTree #LinkedList
To view or add a comment, sign in
-
-
🚀 Day 64 of #100DaysOfCode Solved 222. Count Complete Tree Nodes on LeetCode 🔗 🧠 Key Insight: In a complete binary tree, all levels are fully filled except possibly the last, and nodes are as left as possible. 👉 This property helps us optimize beyond simple traversal ⚙️ Approach (Simple DFS - Your Solution): 1️⃣ If root is null → return 0 2️⃣ Recursively count: 🔹 left = countNodes(root.left) 🔹 right = countNodes(root.right) 3️⃣ Total nodes: 👉 1 + left + right ⏱️ Time Complexity: Current → O(n) Optimized → O(log² n) 📦 Space Complexity: O(h) #100DaysOfCode #LeetCode #DSA #BinaryTree #Recursion #DivideAndConquer #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Linked list problems often test how well you can manipulate pointers without losing track. 🚀 Day 114/365 — DSA Challenge Solved: Swap Nodes in Pairs Problem idea: We need to swap every two adjacent nodes in a linked list without changing values, only pointers. Efficient approach: Use a dummy node and carefully adjust pointers in pairs. Steps: 1. Use a dummy node pointing to head 2. Maintain a pointer prev before the current pair 3. Identify two nodes: first and second 4. Swap them by updating pointers 5. Move prev forward to the next pair This ensures all pairs are swapped correctly. ⏱ Time: O(n) 📦 Space: O(1) Day 114/365 complete. 💻 251 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LinkedList #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Many subarray problems follow the same hidden pattern — once you recognize it, they become much easier. 🚀 Day 102/365 — DSA Challenge Solved: Count Number of Nice Subarrays Problem idea: We need to count the number of subarrays that contain exactly k odd numbers. Efficient approach: Use the same trick as previous problem: subarrays with exactly k odds = subarrays with ≤ k odds − subarrays with ≤ (k − 1) odds Steps: 1. Use a sliding window to count subarrays with at most k odd numbers 2. Expand window by moving right pointer 3. If odd count exceeds k, shrink window from left 4. Add valid subarrays ending at each index 5. Subtract results to get exact count This pattern works beautifully for binary-like conditions. ⏱ Time: O(n) 📦 Space: O(1) Day 102/365 complete. 💻 263 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
LeetCode — Problem 11 | Day 2 💡 Problem: Container With Most Water Given an array of heights, find two lines that together form a container which holds the maximum water. 🧠 My Approach: - Used two pointers (start & end) - Calculated area using "min(height) * width" - Moved the pointer with smaller height to maximize area - Repeated until pointers meet This problem gave a good understanding of: ✔️ Two Pointer Technique ✔️ Optimizing from brute force to O(n) ✔️ Logical thinking in arrays #LeetCode #DSA #Java #CodingJourney
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