Day 88 of #100DaysOfCode Today I solved "Construct Binary Search Tree from Preorder Traversal" on LeetCode using Recursion + Range Validation. Key Idea: In preorder traversal, elements are processed as: Root → Left → Right We can rebuild the BST by maintaining a valid range (lower, upper) for each node. Approach: • Start with range (-∞, +∞) • Pick current value → create node • For left subtree → range becomes (lower, root value) • For right subtree → range becomes (root value, upper) • Move index forward while constructing tree This ensures we build a valid BST without searching Concepts Used: • Binary Search Tree (BST) • Recursion • Preorder Traversal • Range constraints Time Complexity: O(n) Space Complexity: O(h) This problem shows how powerful range-based recursion can be for tree construction Understanding traversal patterns is unlocking new levels #Day88 #100DaysOfCode #LeetCode #BST #Recursion #Cpp #CodingJourney
Constructing BST from Preorder Traversal with Recursion
More Relevant Posts
-
🚀 Day 71 of #100DaysOfCode Today, I solved LeetCode 1572 – Matrix Diagonal Sum, a problem that focuses on matrix traversal and efficient computation. 💡 Problem Overview: Given a square matrix, the task is to calculate the sum of its primary and secondary diagonals, ensuring that the center element (if overlapping) is counted only once. 🧠 Approach: ✔️ Traversed the matrix to calculate: Primary diagonal → elements where row == column Secondary diagonal → elements where row + column == n - 1 ✔️ Handled the center element separately to avoid double counting ⚡ Key Takeaways: Understanding index relationships simplifies matrix problems Edge cases (like overlapping elements) are important Clean logic can avoid unnecessary loops 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) Small problems strengthen big concepts 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 Day 79 of #100DaysOfCode Today, I solved LeetCode 32 – Longest Valid Parentheses, a challenging problem that focuses on stack-based logic and string processing. 💡 Problem Overview: Given a string containing only '(' and ')', the goal is to find the length of the longest valid (well-formed) parentheses substring. 🧠 Approach: ✔️ Used a stack-based approach to track indices ✔️ Initialized stack with -1 to handle edge cases ✔️ For every closing bracket, popped from stack ✔️ Calculated valid substring length using current index and stack top This approach efficiently tracks valid sequences and avoids reprocessing. ⚡ Key Takeaways: Stack is powerful for handling matching problems Index-based tracking simplifies substring calculations Handling edge cases (like invalid starting brackets) is crucial 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(n) Solving hard problems step by step and improving every day 🚀 #LeetCode #100DaysOfCode #DSA #Stack #Strings #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep #HardProblems
To view or add a comment, sign in
-
-
🚀 Day 62 of #100DaysOfCode Today, I solved LeetCode 2075 – Decode the Slanted Ciphertext, a problem that focuses on matrix traversal and string manipulation. 💡 Problem Overview: Given an encoded text written in a slanted manner across a matrix, the task is to reconstruct the original message by decoding it correctly. 🧠 Approach: To solve this problem efficiently, I focused on: ✔️ Constructing the matrix from the given encoded string ✔️ Traversing diagonally to extract the original message ✔️ Handling trailing spaces carefully to get the correct output This approach ensures accurate decoding while maintaining simplicity. ⚡ Key Takeaways: Matrix traversal techniques are useful in string problems Diagonal traversal patterns often appear in encoding/decoding tasks Edge case handling (like trailing spaces) is crucial 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(n) Consistency and structured practice are driving continuous improvement 🚀 #LeetCode #100DaysOfCode #DSA #Strings #MatrixTraversal #ProblemSolving #CodingJourney #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day 5️⃣ /15 — Consistency Challenge 🚀 Today’s problem: LeetCode 1855 — Maximum Distance Between a Pair of Values Solved using a clean two-pointer approach. 🔹 Problem Idea: We need to find the maximum value of j - i such that: i <= j nums1[i] <= nums2[j] Both arrays are non-increasing (sorted in descending order), which makes this a perfect two-pointer problem. 🔹 Approach (Two Pointers): Start two pointers: i = 0 for nums1 j = 0 for nums2 Traverse while both pointers are in bounds: If nums1[i] <= nums2[j] ✅ Valid pair found Update answer with j - i Move j forward to try for a larger distance Else (nums1[i] > nums2[j]) ❌ Invalid pair Move i forward to reduce nums1[i] and try to make condition valid 🔹 Time Complexity: Each pointer moves at most once through its array 👉 O(n + m) 🔹 Space Complexity: 👉 O(1) Clean logic, efficient solution, and another step in the consistency journey 💯 #LeetCode #DSA #TwoPointers #Consistency #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 66 of #100DaysOfCode Today, I solved LeetCode 73 – Set Matrix Zeroes, a classic problem that tests in-place matrix manipulation and optimization techniques. 💡 Problem Overview: Given a matrix, if any cell contains 0, its entire row and column must be set to 0. The challenge is to perform this efficiently without using excessive extra space. 🧠 Approach: To solve this optimally, I focused on: ✔️ Using the first row and first column as markers ✔️ Tracking whether the first row/column initially contained zero ✔️ Updating the matrix in-place based on these markers This avoids using additional space and achieves optimal performance. ⚡ Key Takeaways: In-place algorithms help reduce space complexity Using matrix itself as storage is a powerful optimization trick Handling edge cases (first row/column) is critical 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(1) Improving problem-solving skills one day at a time 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
Day 171/365 – DSA Challenge 🚀 Solved Convert Sorted List to Binary Search Tree on LeetCode today. 🔹 Problem: Given a sorted linked list, convert it into a height-balanced Binary Search Tree (BST). 🔹 Approach Used: Two Pointers + Recursion Steps followed: 1️⃣ Use slow & fast pointers to find the middle node → root 2️⃣ Recursively build: Left subtree from left half of list Right subtree from right half 3️⃣ Continue until sublist becomes empty 🔹 Key Idea: Middle node acts as root → ensures the BST remains balanced. 🔹 Time Complexity: O(n log n) 🔹 Space Complexity: O(log n) 💻 Language: C++ Great problem to understand how linked list traversal + tree construction can be combined. #Day171 #365DaysOfCode #DSA #LeetCode #BinaryTree #BST #LinkedList #Recursion #CodingChallenge #Cpp
To view or add a comment, sign in
-
-
🚀 Day 71 of #100DaysOfCode 🔥 Problem: Container With Most Water (LeetCode 11) Today’s problem was all about optimizing brute force into a smarter approach. 💡 Key Idea: We need to find two lines that together form a container holding the maximum water. 👉 Instead of checking all pairs (O(n²)), we use a two-pointer approach: - Start with left = 0 and right = n-1 - Calculate area = width × min(height[left], height[right]) - Move the pointer with smaller height inward ⚡ Why this works? Because the height is limited by the smaller line — moving the taller one won’t help increase area. 🧠 What I learned: - Two-pointer technique can drastically reduce time complexity - Think in terms of eliminating useless comparisons - Greedy movement based on constraints works powerful here ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistency > Motivation 💯 See you tomorrow with Day 72 🔥 #DSA #LeetCode #CodingJourney #PlacementPrep #100DaysOfCode
To view or add a comment, sign in
-
-
Day 46 of #100DaysOfLeetCode Problem: Length of Last Word Difficulty: Easy Key Concept Learned: Reverse Traversal with Two Pointer Thinking Today I solved the problem Length of Last Word. The task was to find the length of the last word in a given string. Instead of splitting the string, I used a more efficient approach by traversing from the end. First, I ignored all the trailing spaces. Then I counted the characters until I reached a space or the beginning of the string. This approach avoids extra space and keeps the solution simple and clean. Time Complexity: O(n) Space Complexity: O(1) Key Takeaways Traversing from the end can simplify string problems Avoiding extra operations like split can make solutions more efficient Handling edge cases like trailing spaces is important Simple logic often leads to optimal solutions Consistency is helping me improve step by step #100DaysOfLeetCode #DSA #LeetCode #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
.Day 47 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Bracket Validity (Valid Parentheses) We were given a string s. It contains only brackets: (), {}, [] Task was to check whether the string is valid. Valid means: ✔️ Same type of brackets ✔️ Correct order ✔️ Every opening has a matching closing 💻 Approach (Using Stack) 🔹️Create an empty stack. 🔹️Traverse the string. 🔹️If opening bracket → push into stack. 🔹️If closing bracket: ▪️Check if stack is empty → invalid ▪️Check top of stack ▪️If matching → pop ▪️Else → invalid 🔹️At the end, stack should be empty. If empty → valid Else → not valid 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Stack is the best fit for bracket matching problems. ▫️Checking empty stack avoids runtime errors. ▫️Matching pairs logic must be handled carefully. ▫️Order of brackets is more important than count. Day 47 completed. Revising stack patterns again 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 30/50 – LeetCode Challenge 🧩 Problem #8 – String to Integer (atoi) Today’s problem focused on converting a string into an integer while handling multiple edge cases, making it a great exercise in string parsing and validation. 📌 Problem Summary: Implement the atoi function that converts a string into a 32-bit signed integer. The function must: ✔ Ignore leading whitespaces ✔ Handle optional + or - sign ✔ Read digits until a non-digit character appears ✔ Clamp the result within the 32-bit integer range 🔍 Approach Used ✔ Removed leading spaces using strip() ✔ Checked for sign (+ or -) ✔ Iterated through characters and built the number ✔ Stopped when a non-digit character appeared ✔ Handled overflow by restricting values to valid integer range ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 💡 Key Learning ✔ Handling multiple edge cases carefully ✔ Understanding string parsing logic ✔ Managing integer overflow conditions ✔ Writing clean and robust code This problem shows how important it is to handle real-world input scenarios properly. Consistency builds strong problem-solving skills 🚀 🔗 Problem Link: https://lnkd.in/g-Df-jxW #50DaysOfLeetCode #LeetCode #DSA #Strings #ProblemSolving #CodingJourney #FutureAIEngineer #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