Day 76/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Lowest Common Ancestor of a Binary Tree A fundamental tree problem that builds strong recursion intuition. Problem idea: Find the lowest node in the tree that has both given nodes as descendants. Key idea: DFS + recursion (bottom-up approach). Why? • Each subtree can independently tell if it contains p or q • Combine results while backtracking • First node where both sides return non-null → answer How it works: • If current node is null / p / q → return it • Recursively search left and right subtree • If both left & right are non-null → current node is LCA • Else return the non-null side Time Complexity: O(n) Space Complexity: O(h) (recursion stack) Big takeaway: Tree problems often rely on post-order traversal + combining child results. Understanding this pattern unlocks many binary tree problems. 🔥 Day 76 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #Recursion #DFS #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
Yogesh ..’s Post
More Relevant Posts
-
Day 79/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Flatten Binary Tree to Linked List A powerful tree transformation problem using pointer manipulation. Problem idea: Convert a binary tree into a linked list in-place following preorder traversal. Key idea: Iterative traversal + rewiring (similar to Morris traversal idea). Why? • We need preorder sequence (Root → Left → Right) • Instead of extra space, we modify pointers in-place • Efficient and avoids recursion stack How it works: • Traverse using a pointer curr • If left child exists: → Find rightmost node of left subtree → Connect it to current’s right subtree → Move left subtree to right → Set left = null • Move to next node (curr.right) Time Complexity: O(n) Space Complexity: O(1) Big takeaway: Tree problems can often be optimized using in-place pointer rewiring, avoiding extra space. 🔥 This pattern is very useful for tree flattening and traversal optimizations. Day 79 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #MorrisTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 LeetCode Challenge 27/50 💡 Approach: Classic Binary Search The problem itself demands O(log n) — no room for linear search! Binary Search is one of the most fundamental algorithms in computer science, and mastering it is non-negotiable for every developer. 🔍 Key Insight: → Maintain left and right pointers on the sorted array → Calculate mid = left + (right - left) / 2 — NOT (left+right)/2 → Why? (left+right) can cause INTEGER OVERFLOW for large values! → nums[mid] == target → found! → nums[mid] < target → search right half (left = mid+1) → nums[mid] > target → search left half (right = mid-1) 📈 Complexity: ❌ Linear Search → O(n) Time ✅ Binary Search → O(log n) Time, O(1) Space Real impact: For n=1,000,000 → Linear needs 1M comparisons, Binary Search needs just 20! That's the power of logarithms. 🔥 #LeetCode #DSA #BinarySearch #Java #ADA #PBL2 #LeetCodeChallenge #Day27of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #SearchAlgorithm
To view or add a comment, sign in
-
-
Day 85/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Populating Next Right Pointers in Each Node A clean pointer-manipulation problem that strengthens tree traversal intuition without extra space. Problem idea: Given a perfect binary tree, connect each node to its next right node using a next pointer. Key idea: Level-by-level traversal using already established next pointers (no queue needed). Why? • The tree is perfect → every level is fully filled • We can use existing next links to move horizontally • This avoids extra space (unlike BFS with a queue) How it works: • Start from the leftmost node of each level • Connect left child → right child • If a next node exists, connect right child → next node’s left child • Move across the level using next pointers • Then go down to the next level Time Complexity: O(n) Space Complexity: O(1) Big takeaway: When a tree is perfect, you can often avoid extra space by cleverly using existing structure (like next pointers). 🔥 This is a powerful pattern for constant-space tree traversal problems. Day 85 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinaryTree #Pointers #TreeTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 Consistency + Clarity = Progress Today I solved the Root to Leaf Paths problem on binary trees using recursion and backtracking. 🔍 Key Learnings: Understood the backtracking pattern (add → recurse → remove) Learned how to correctly identify leaf nodes Improved clarity on managing state (path list) during recursion 💡 What made the difference: Instead of memorizing, I focused on understanding the pattern — and everything clicked. 📊 Result: ✅ All test cases passed (1115/1115) ✅ 100% accuracy This is part of my ongoing journey to strengthen my Data Structures & Algorithms (DSA) fundamentals. 🎯 Takeaway: Mastering patterns like recursion and backtracking makes complex problems feel simple. #DSA #Java #Coding #ProblemSolving #Recursion #Backtracking #BinaryTree #LearningJourney #Consistency
To view or add a comment, sign in
-
-
Day 71/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Maximum Subarray Min-Product A powerful problem combining prefix sums and monotonic stack. Problem idea: For every subarray, calculate (minimum element × sum of subarray) and find the maximum value. Key idea: Monotonic stack + prefix sum optimization. Why? • Brute force would be too slow (O(n²)) • Need to efficiently find range where each element is minimum • Prefix sum helps compute subarray sum in O(1) How it works: • Build prefix sum array • Use monotonic increasing stack • For each element, find its left & right boundary • Treat each element as the minimum of a subarray • Compute subarray sum using prefix • Multiply with element → track maximum Time Complexity: O(n) Space Complexity: O(n) Big takeaway: Monotonic stack helps solve “nearest smaller element” problems efficiently, which unlocks many advanced patterns. 🔥 Day 71 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #MonotonicStack #PrefixSum #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 57/100 | #100DaysOfDSA ⛰️🔍 Today’s problem: Peak Index in a Mountain Array A perfect use-case of Binary Search on a pattern. Key observation: The array increases → reaches a peak → then decreases. Approach: • Use binary search on the index • Compare mid with mid + 1 • If arr[mid] > arr[mid + 1] → we are on the decreasing side → move left • Else → we are on the increasing side → move right This guarantees we always move toward the peak. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Binary search isn’t just for sorted arrays — it works on patterns too. Recognizing these patterns is a game changer. 🔥 Day 57 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 45/75 — Lowest Common Ancestor of a Binary Tree Today’s problem was about finding the lowest common ancestor (LCA) of two nodes in a binary tree. Approach: • Use recursion (DFS) • If current node is null, return null • If current node matches p or q, return it • Search left and right subtree • If both sides return non-null → current node is LCA Key logic: if (root == null || root == p || root == q) return root; TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if (left != null && right != null) return root; return (left != null) ? left : right; Time Complexity: O(n) Space Complexity: O(h) Key Insight: The first node where both p and q are found in different subtrees is the LCA. 45/75 🚀 #Day45 #DSA #BinaryTree #Recursion #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Consistency is starting to compound 💻🌱🔥 Solved another problem today—this time from Binary Trees 🌳🚀 Completed Root to Leaf Paths with 100% accuracy (1115/1115 test cases passed) 💯✅ 🔍 Problem Insight: The task was to return all possible paths from the root node to every leaf node in a binary tree 🌳➡️🍃 🧠 My Approach: 👉 Used DFS (Depth-First Search) with recursion 🔁 👉 Maintained a currentPath list to track nodes while traversing 🛤️ 👉 At each node, added the current value to the path ➕ 👉 If a leaf node was reached, stored a copy of the current path in the final answer 📌 👉 Used backtracking by removing the last node after returning from recursion 🔙 ✨ Why this approach works well: ✔ Natural fit for tree traversal 🌳 ✔ Explores every root-to-leaf path efficiently 🚀 ✔ Backtracking keeps memory controlled 🧠 ✔ Clean recursive structure and easy to follow 🧼 📊 Complexity: ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(h) recursion stack (plus output space for storing all paths) 📦 💡 Key Takeaway: Tree problems become much simpler when you think in terms of: 👉 traversal pattern 👉 path tracking 👉 backtracking DFS + recursion continues to be one of the cleanest ways to solve path-based tree problems 🎯🌳 One more problem solved, one more concept strengthened 💪📈🔥 #BinaryTree #Trees #Recursion #DFS #Java #DataStructures #DSA #ProblemSolving #CodingJourney #100DaysOfCode #SoftwareEngineering #Programming #TechGrowth 💻🌳🚀
To view or add a comment, sign in
-
-
Day 59/100 | #100DaysOfDSA 🔍⚡ Today’s problem: Single Element in a Sorted Array A clever Binary Search problem with a twist. Key observation: In a sorted array where every element appears twice except one, pairs follow a pattern. Before the single element: • First occurrence → even index • Second occurrence → odd index After the single element: • Pattern breaks Approach: • Use binary search • Check mid with its pair using index trick (mid ^ 1) • If nums[mid] == nums[mid ^ 1] → move right • Else → move left This helps pinpoint where the pattern breaks. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Bit manipulation + pattern observation can simplify binary search problems significantly. Small tricks → big optimizations. 🔥 Day 59 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #BitManipulation #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
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