🚀 Day 62 of DSA consistency Today I solved Lowest Common Ancestor in a Binary Search Tree. 🔹 Key Idea: In a Binary Search Tree (BST): Left subtree → values smaller than root Right subtree → values greater than root Using this property: If both nodes are greater than root, move right. If both nodes are smaller than root, move left. Otherwise, the current node is the Lowest Common Ancestor. 💡 This allows us to solve the problem efficiently without storing paths. 🧠 Time Complexity: O(H) (H = height of the tree) 💾 Space Complexity: O(H) due to recursion stack. #DSA #Java #BinarySearchTree #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode
Lowest Common Ancestor in BST Solved
More Relevant Posts
-
#100DaysOfCode – Day 2 Today I worked on a DSA problem based on arrays: Check if an array is sorted and rotated 🔍 Approach: Instead of finding the exact rotation point, I focused on identifying a pattern: In a sorted and rotated array, the order should break at most once. So, I checked how many times an element is greater than the next element while traversing the array in a circular manner. ✔️ If the count of such breaks is 0 or 1 → valid ❌ If it’s more than 1 → not a sorted rotated array 🧠 Key Takeaway: This problem taught me how pattern observation can simplify logic and avoid unnecessary complexity. Sometimes the best solution is not the most obvious one! 📈 Staying consistent and improving step by step 💪 #100DaysOfCode #DSA #DataStructures #Algorithms #Java #CodingJourney #ProblemSolving #LeetCode #Consistency
To view or add a comment, sign in
-
-
#Day76 of my second #100DaysOfCode Binary search continues to surprise me with how many variations it has. DSA • Solved Find Peak Element (LeetCode 162) – Brute: check every element → O(n) – Optimal: binary search based on slope comparison → O(log n) • Key idea: if the next element is greater, move right; else move left — a peak is guaranteed • Didn’t need to check all elements, just follow the increasing/decreasing trend Interesting how this doesn’t require a fully sorted array, yet binary search still works. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 47 🔍 Solved: Find Minimum in Rotated Sorted Array Today I explored another interesting variation of Binary Search. Instead of searching for a target, the goal was to find the minimum element in a rotated sorted array. 💡 Key Insight: By comparing the middle element with the last element, we can determine which half contains the minimum value. Approach: ✔ Used Binary Search to achieve O(log n) time complexity ✔ Compared mid with end to identify the unsorted portion ✔ Narrowed down the search space efficiently What I Learned: This problem helped me understand how binary search can be applied beyond simple searching—especially in rotated and partially sorted arrays. #Java #DSA #LeetCode #BinarySearch #CodingJourney #ProblemSolving #TechSkills
To view or add a comment, sign in
-
-
🚀 Day 21 of My DSA Journey Solved LeetCode Problem #704 – Binary Search today! 🔍 This problem is a classic example of how powerful Binary Search can be when working with sorted arrays. Instead of checking each element one by one, we reduce the search space by half every time — making it highly efficient. 💡 Key Learnings: Importance of sorted data for Binary Search Optimizing time complexity from O(n) to O(log n) Handling edge cases carefully (like target not found) 🧠 Approach: Used two pointers (low and high) and calculated the middle index to compare with the target value. Based on comparison, reduced the search space accordingly. 💻 Time Complexity: O(log n) 📦 Space Complexity: O(1) Consistency is key 🔑 — one step closer to mastering DSA! #Day21 #LeetCode #DSA #BinarySearch #Java #CodingJourney #ProblemSolving #TechGrowth
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 27 Today’s focus: Binary Search on modified arrays. Problem solved: • Search in Rotated Sorted Array (LeetCode 33) Concepts used: • Binary Search • Identifying sorted halves • Conditional search space reduction Key takeaway: This problem extends binary search to a rotated sorted array, where the array is not fully sorted but divided into two sorted parts. At each step, we: • Find the mid element • Check which half (left or right) is sorted • Decide whether the target lies in the sorted half • Eliminate the other half This allows us to still achieve O(log n) time complexity. Continuing to strengthen fundamentals and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 58/100 | #100DaysOfDSA 🔄🔍 Today’s problem: Search in Rotated Sorted Array A twist on Binary Search with a rotated array. Key idea: Even though the array is rotated, one half is always sorted. Approach: • Use binary search • Find mid element • Check which half is sorted (left or right) • Decide if target lies in the sorted half • Narrow the search accordingly Why it works: At every step, we eliminate half of the search space just like standard binary search. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Understanding the structure of the problem helps adapt classic algorithms like binary search. Rotation doesn’t break order — it just shifts it. 🔥 Day 58 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 37 of my DSA Journey Today I worked on the “Same Tree” problem on LeetCode and strengthened my understanding of recursion in Binary Trees. Problem: Given two binary trees, check whether they are identical or not. What I learned: Two trees are considered the same if: • Both nodes are null • Values of nodes are equal • Left subtrees are identical • Right subtrees are identical Approach: I used recursion to compare both trees node by node. Code: class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) return true; if (p == null || q == null) return false; if (p.val != q.val) return false; return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } } Time Complexity: O(n) Space Complexity: O(n) (due to recursion stack) Key takeaway: In tree problems, breaking the problem into smaller subtrees makes complex logic much simpler. Learning step by step, improving every day #Day37 #DSA #BinaryTree #Recursion #LeetCode #Java #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
🚀 Day 55 of DSA – Median of Two Sorted Arrays Solved a hard binary search problem where we need to find the median of two sorted arrays in O(log(m+n)) time. 💡 Key Insight: Instead of merging both arrays, use binary search on partitions to directly find the correct median. ⚡ Approach: Find partition in the smaller array Adjust partition in the second array accordingly Check if left half and right half are valid Use boundary values to compute median ⏱️ Time Complexity: O(log(min(m, n))) 💾 Space Complexity: O(1) #DSA #LeetCode #Java #Algorithms #BinarySearch #ProblemSolving #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