🚀 Day 11 of #60DaysOfCode Solved a challenging tree problem today: 👉 Largest BST in a Binary Tree 🌳 Problem Statement: Given a binary tree, find the size of the largest subtree that satisfies Binary Search Tree (BST) properties. 💡 Approach: Used a postorder traversal (bottom-up approach) to efficiently determine whether each subtree is a BST. For every node, tracked: - Minimum value - Maximum value - Size of subtree - Whether it's a valid BST If a subtree satisfies BST conditions, we compute its size. Otherwise, we carry forward the maximum size found so far. ⚡ Key Insight: Instead of checking each subtree separately (which is inefficient), we combine results during traversal — achieving O(N) time complexity. ✅ Successfully passed all test cases! 🔍 This problem really strengthened my understanding of: - Tree Traversals - Recursion - Optimized problem-solving #DataStructures #Java #CodingJourney #BinaryTree #BST #ProblemSolving #100DaysOfCode
Optimizing Binary Tree Traversal for Largest BST
More Relevant Posts
-
💻 Day 35 of #LeetCode Journey 🔥 Solved: 19. Remove Nth Node From End of List Today’s problem was all about mastering Linked Lists and understanding the power of the Two Pointer technique. 🔍 Key Idea: Instead of calculating the length, I used a smart approach with fast and slow pointers. Move the fast pointer n steps ahead Then move both pointers together This helps locate the node to remove in a single pass ⚡ Why this approach? Efficient: O(n) time complexity No extra space required Clean and optimal solution 🧠 What I learned: Using a dummy node simplifies edge cases (like removing the head) Two-pointer technique is very powerful in linked list problems 📌 Problem Link: https://lnkd.in/gxXDR-YV #Java #DataStructures #LinkedList #CodingJourney #100DaysOfCode #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
Day 37 of #75DaysofLeetCode 🚀 LeetCode 1372 – Longest ZigZag Path in a Binary Tree Just solved an interesting tree problem that really tests your understanding of DFS + state tracking 🌳 🔍 Problem Insight: A ZigZag path alternates between left ↔ right at every step. The goal is to find the longest such path in a binary tree. 💡 Key Idea: At every node, we track: Current direction (left or right) Current path length We have two choices: Continue the ZigZag → increase length Restart from opposite direction → reset length 🧠 Approach (DFS): Use recursion to explore both directions Keep updating a global maximum Try starting from both left and right 📊 Complexity: Time: O(N) Space: O(H) (recursion stack) 🔥 Takeaway: This problem is a great example of how tracking state in recursion can simplify complex tree traversals. #LeetCode #DataStructures #BinaryTree #DSA #CodingInterview #Java #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 38 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Maximum Product of Two Elements in an Array. Problem Insight: Given an integer array, the goal is to find two elements such that: (nums[i] - 1) * (nums[j] - 1) is maximized Approach: • First, sort the array using Arrays.sort() • Use two nested loops to check all possible pairs • For each pair, calculate → (nums[i] - 1) * (nums[j] - 1) • Keep track of the maximum product Time Complexity: • O(n²) — due to nested loops Space Complexity: • O(1) — no extra space used Key Learnings: • Understanding operator precedence is very important in expressions • Sorting helps in simplifying many problems • Even simple problems can have optimized solutions beyond brute force Takeaway: Brute force helps in understanding the problem deeply, but optimization (like using the two largest elements directly) makes the solution efficient 🚀 #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Arrays
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 48 🔍 Solved: Find Peak Element Today I worked on an interesting Binary Search problem where the goal was to find a peak element in an array. 💡Key Insight: By comparing the middle element with its next element, we can determine the direction of the peak. 📌 Approach: ✔ Applied Binary Search for O(log n) efficiency ✔ Compared mid with next element ✔ Moved towards the increasing side to find peak ✔ Reduced search space step by step Why this works: Since adjacent elements are not equal, there will always be at least one peak. By comparing neighboring elements, we can decide the direction and reduce the search space efficiently. 🎯 What I Learned: This problem showed how Binary Search can be used beyond searching—especially for identifying patterns like peaks in arrays. #Java #DSA #LeetCode #BinarySearch #CodingJourney #ProblemSolving #TechSkills 🚀
To view or add a comment, sign in
-
-
🚀 Day 30of LeetCode Odd Even Linked List — Clean Pointer Manipulation! Just solved a really interesting linked list problem that tests how well you understand pointer manipulation 👇 🔹 Problem: Rearrange a linked list such that all nodes at odd indices come first, followed by nodes at even indices — while maintaining their relative order. 👉 Example: Input: 1 → 2 → 3 → 4 → 5 Output: 1 → 3 → 5 → 2 → 4 💡 Key Insight: Instead of using extra space, we can split the list into: an odd-index list an even-index list Then simply connect them at the end! 🧠 Approach: Maintain two pointers: odd and even Keep track of evenHead Rearrange links in one traversal Attach even list after odd list ⚡ Complexity: Time: O(n) Space: O(1) (in-place) 🎯 What I learned: Mastering linked lists is all about pointer control Problems that look tricky often have simple in-place solutions Always think in terms of structure, not values #LeetCode #DataStructures #LinkedList #Java #CodingInterview #100DaysOfCode #ProblemSolving #TechJourney
To view or add a comment, sign in
-
-
Day 72 of #100DaysOfCode Problem: Convert Sorted Array to Height-Balanced BST Today I learned how to efficiently convert a sorted array into a balanced Binary Search Tree using Divide & Conquer. Key Insight: Pick the middle element as the root to maintain balance. Recursively build: Left subtree from left half Right subtree from right half This ensures: Optimal height Faster search operations ⏱ Time Complexity: O(n) 📦 Space Complexity: O(log n) Consistency is the real game changer #DSA #Java #BinaryTree #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 73 of #100DaysOfLeetCode with edSlash Today’s problem: Next Greater Node in Linked List (LeetCode 1019) 🔍 What I learned: How to find the next greater element in a linked list Converting a linked list into an array for easier processing Using a monotonic stack to solve problems efficiently 💡 Key Insight: Instead of brute force (O(n²)), using a stack helps reduce time complexity to O(n) by keeping track of elements in decreasing order. ⚡ Approach: Convert linked list → array Use stack to track indices Find next greater elements 📈 Complexity: Time: O(n) Space: O(n) Every day = 1% better 💪 Let’s keep pushing 🚀 #Day73 #LeetCode #DSA #LinkedList #Stack #Java #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 21 of my #100DaysOfCode Journey Today, I solved the LeetCode question Contains Duplicate II. The task is to check if there are two equal elements in the array such that their index difference is at most k. ✅ Steps: First, iterate through each element in the array For every element, check only the next k elements Compare values within this limited range If a match is found → return true 💻 My Approach: I used a sliding window + brute force approach (without using HashMap/HashSet). Instead of checking the whole array, I optimized it by limiting the inner loop to k distance only. 🌟 Learning Takeaways: Optimizing brute force can avoid TLE Understanding constraints (like k distance) helps reduce complexity Multiple approaches exist — choosing based on space/time is key #DSA #Java #LeetCode #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 60 / 100 – LeetCode Challenge ✅ Solved: 160. Intersection of Two Linked Lists Today’s problem was all about understanding pointer behavior and memory references in linked lists. 💡 Key Insight: Instead of comparing values, we compare node references. If two linked lists intersect, they will share the same tail nodes. 🚀 Approach Used (Optimal): Two pointers (pA, pB) Traverse both lists When one reaches the end, switch to the other list They eventually meet at the intersection node (or null) 🧠 Why it works: Both pointers travel equal distance → LengthA + LengthB, aligning perfectly without extra space. ⏱ Complexity: Time: O(m + n) Space: O(1) 💻 Result: ✔️ Accepted (41/41 test cases) ⚡ Runtime: 1 ms (Beats 99.94%) 📌 Takeaway: Sometimes the smartest solution is not about extra data structures, but about clever traversal. #Day60 #100DaysOfCode #LeetCode #Java #DSA #LinkedList #CodingJourney
To view or add a comment, sign in
-
-
Day 56 : Crushing Binary Trees on LeetCode 💡 Today’s live practical session in Alpha Plus 7.0 We focused on some of the most challenging Binary Tree questions on LeetCode, breaking down the logic behind them. What I practiced today: ✅ Tree Diameter: understand the logic to calculate the absolute longest path between any two nodes in the entire tree. ✅ Maximum Path Sum: Tackled a famous "Hard" level problem! Figured out how to find the path with the highest possible sum, even if it doesn't pass through the root. ✅ Target Deletion: Wrote the recursive code to systematically find and delete leaf nodes that match a specific target value. #BinaryTree #LeetCode #ProblemSolving #DSA #Java #SoftwareEngineering #100DaysOfCode #ApnaCollege
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