🌳 DSA Challenge – Day 100 🎉 Problem: Construct Binary Tree from Preorder and Inorder Traversal 🌲 We made it to Day 100 of the challenge! 💪🔥 Let’s end this journey with a classic Tree Construction problem — a perfect blend of recursion, hashing, and traversal logic. 🧠 Problem Summary: You’re given two integer arrays: preorder → preorder traversal of a binary tree inorder → inorder traversal of the same tree Your goal: Reconstruct and return the binary tree. ⚙️ My Approach: 1️⃣ The first element of preorder is always the root. 2️⃣ Use a hash map (index) to quickly find the root’s position in the inorder array. 3️⃣ Recursively build: The left subtree from elements before the root in inorder. The right subtree from elements after the root in inorder. 4️⃣ Reversing preorder allows efficient pop() operations from the end (O(1)). 💡 Why This Works: Preorder gives the root order, Inorder gives the structure (left–root–right). By combining both, we can rebuild the entire tree recursively in O(n) time. 📈 Complexity Analysis: Time Complexity: O(n) — Each node is processed once, and lookup is O(1) using a hash map. Space Complexity: O(n) — For recursion + hash map. ✨ Key Takeaway: Efficient recursion often comes from using traversal properties smartly — here, preorder identifies roots, while inorder defines subtree boundaries. 🌟 Milestone Moment: That’s Day 100 of DSA 🔥 From arrays to trees, recursion to heaps — what a journey! 🌱 Keep learning, keep building, and keep challenging yourself 💪 🔖 #Day100 #100DaysOfCode #DSA #BinaryTree #Preorder #Inorder #LeetCode #Recursion #Python #DataStructures #ProblemSolving #CodingChallenge #TechCommunity #Milestone
Reconstructing a Binary Tree from Preorder and Inorder Traversal
More Relevant Posts
-
💡 Day 43 / 100 – Search in Rotated Sorted Array (LeetCode #33) Today’s problem was a twist on the classic binary search — quite literally! The challenge was to find a target element in a rotated sorted array. At first glance, the array looks unsorted, but there’s actually a pattern. By identifying which part of the array is properly sorted at every step, we can still apply binary search logic efficiently — achieving O(log n) time complexity. This problem beautifully blends pattern recognition with logical precision. 🔍 Key Learnings Even when data looks “unsorted,” patterns often exist beneath. Modified binary search can adapt to many problem variations. Understanding midpoint relationships helps in avoiding brute force. 💭 Thought of the Day Adaptability is key — in coding and in life. Just like binary search adjusts to a rotated array, we can adjust to challenges by recognizing the underlying order in the chaos. Clear logic turns confusion into clarity. 🔗 Problem Link: https://lnkd.in/gS8FcbeE #100DaysOfCode #Day43 #LeetCode #Python #BinarySearch #ProblemSolving #Algorithms #CodingChallenge #DataStructures #CodingJourney #PythonProgramming #LogicBuilding #KeepLearning #TechGrowth #Motivation
To view or add a comment, sign in
-
-
Day 39 / 100 – Combination Sum (LeetCode #39) Today’s challenge was Combination Sum, a classic problem that teaches how to explore all possible combinations that add up to a target. The key idea here is recursion with backtracking — trying different paths, and “undoing” choices when they don’t lead to a solution. At first, it felt complex to keep track of combinations without duplicates, but recursion made it elegant once I understood how to structure the calls properly. It’s a great example of how backtracking mirrors human problem-solving: try, fail, and refine until success. 🔍 Key Learnings Recursion helps break complex problems into smaller, reusable patterns. Backtracking avoids unnecessary computation by pruning invalid paths early. Always remember to copy the current combination when adding it to results — lists are mutable! 💭 Thought of the Day Sometimes the most powerful solutions come from simplicity — recursion mirrors the way we naturally think through problems step by step. Today’s problem reminded me that patience and clarity often lead to clean, beautiful solutions ✨ 🔗 Problem Link: https://lnkd.in/gxtE573Z 🏷️ #100DaysOfCode #Day39 #LeetCode #Python #Recursion #Backtracking #ProblemSolving #Algorithms #DataStructures #CodingChallenge #LearnToCode #CleanCode #MindsetMatters #CodeEveryday #TechJourney
To view or add a comment, sign in
-
-
🔥 Day 10 of 30 – LeetCode Challenge: First & Last Position in Sorted Array 🎯 Today’s challenge involved finding the starting and ending index of a target value in a sorted array — but with an important constraint: the solution must run in O(log n) time. This makes Binary Search the perfect approach! 🧩 Problem Summary Given a sorted array nums, return the first and last position of a given target. If the target doesn’t exist, return [-1, -1]. Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3, 4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1, -1] Example 3: Input: nums = [], target = 0 Output: [-1, -1] 💡 Approach – Modified Binary Search We use Binary Search twice: 1️⃣ First to find the leftmost (first) occurrence 2️⃣ Second to find the rightmost (last) occurrence By narrowing down boundaries instead of stopping at the first match, we maintain O(log n) complexity. ⚙️ Complexity Time O(log n) Space O(1) 🏆 Result & Learnings ✅ Implemented binary search in two forms 📍 Learned how to adjust boundaries to find first/last occurrence ⚡ Better understanding of narrowing search space efficiently ✨ Real-World Use Cases 🔍 Search results pagination (finding first & last index) 📊 Range queries in sorted data 🧠 Used in problems involving frequency count of numbers #Day10 #LeetCode #BinarySearch #Python #CodingChallenge #DSA #30DaysOfCode #WomenWhoCode #ProblemSolving #MTech
To view or add a comment, sign in
-
-
Day 35 / 100 – Subarray Sum Equals K (LeetCode #560) Today’s challenge was about finding the number of continuous subarrays whose sum equals a given value k. At first, the brute-force approach felt tempting — checking every possible subarray — but that would be too slow. Instead, I learned to use a Hash Map to store cumulative sums and find the solution in linear time. This problem truly deepened my understanding of prefix sums and how storing previous computations can make complex problems simple and elegant. 🔍 Key Learnings Prefix Sum helps keep track of cumulative totals efficiently. Hash Maps allow quick lookups, reducing time complexity from O(n²) to O(n). Sometimes, the key to optimization is remembering what you’ve already calculated. 💭 Thought of the Day Today reminded me that not every problem needs to be solved from scratch — sometimes, the best solutions come from building on what you already know. Logic and memory, when used together, create magic in code ✨ 🔗 Problem Link: https://lnkd.in/gVGGGm6J #100DaysOfCode #Day35 #LeetCode #Python #HashMap #ProblemSolving #DataStructures #Algorithms #CodingChallenge #CodeEveryday #LearningJourney #Optimization #CleanCode #TechGrowth
To view or add a comment, sign in
-
-
🔹 Day 2 of 30 – LeetCode Challenge: Postorder Traversal of Binary Tree 🌲Today’s problem was about implementing Postorder Traversal of a binary tree — one of the most fundamental tree traversal techniques in Data Structures and Algorithms. 🧩 Problem: Return the postorder traversal (Left → Right → Root) of a given binary tree. Example: Input: root = [1, null, 2, 3] Output: [3, 2, 1] 💡 Approach: In Postorder Traversal, we: Visit Left Subtree Visit Right Subtree Visit Root Node I implemented both recursive and iterative approaches. The recursive version is simpler, while the iterative version helps understand how to simulate recursion using a stack. ⚙️ Complexity: Time Complexity: O(n) Space Complexity: O(h) 🏆 Result: ✅ All test cases passed 🚀 Runtime Efficiency: 100% 💬 Learning: This problem helped me deepen my understanding of recursion and stack-based traversal logic. Iterative postorder traversal is tricky but a great exercise to strengthen logical thinking in DSA. #LeetCode #Day2 #Python #DataStructures #BinaryTree #PostorderTraversal #Algorithms #Recursion #30DaysOfCode #MTech #CodingChallenge
To view or add a comment, sign in
-
-
🚀 DSA Challenge – Day 89 Problem: Subsets II (Handling Duplicates in Power Set) ⚙️✨ This problem was a great exploration of recursion, backtracking, and how to systematically avoid duplicate subsets using careful pruning. 🧠 Problem Summary: You are given an integer array nums that may contain duplicates. Your goal: return all possible subsets (the power set) without including any duplicate subsets. ⚙️ My Approach: 1️⃣ First, sort the array — this step helps group duplicates together, which is crucial for skipping them efficiently. 2️⃣ Use recursive backtracking to explore all possible inclusion/exclusion combinations. 3️⃣ Whenever a duplicate element is found (i.e., nums[i] == nums[i-1]), skip it if it’s at the same recursion depth — ensuring unique subset generation. 4️⃣ Keep track of the current subset and add a copy to the result whenever we reach a new state. 📈 Complexity: Time: O(2ⁿ) → Each element can be either included or excluded. Space: O(n) → For recursion and temporary subset storage. ✨ Key Takeaway: Sorting before recursion is often the simplest way to handle duplicates in backtracking problems. With one small condition check, you can turn exponential chaos into structured exploration. ⚡ 🔖 #DSA #100DaysOfCode #LeetCode #ProblemSolving #Recursion #Backtracking #Algorithms #CodingChallenge #Python #TechCommunity #InterviewPrep #EfficientCode #LearningByBuilding #CodeEveryday
To view or add a comment, sign in
-
-
🚀 𝐃𝐚𝐲 𝟏𝟑 𝐨𝐟 #𝟏𝟔𝟎𝐃𝐚𝐲𝐬𝐎𝐟𝐂𝐨𝐝𝐞 — 𝐏𝐚𝐥𝐢𝐧𝐝𝐫𝐨𝐦𝐞 𝐍𝐮𝐦𝐛𝐞𝐫 | 𝐒𝐭𝐫𝐢𝐧𝐠𝐬 🧩 Today’s focus was on a simple yet classic logic check — determining if a number reads the same forward and backward. While it looks straightforward, it’s a great reminder that elegant solutions often come from clarity, not complexity. Problem: 𝐂𝐡𝐞𝐜𝐤 𝐢𝐟 𝐚𝐧 𝐢𝐧𝐭𝐞𝐠𝐞𝐫 𝐢𝐬 𝐚 𝐩𝐚𝐥𝐢𝐧𝐝𝐫𝐨𝐦𝐞. Approach: Convert the integer to a string and compare it with its reverse. Time Complexity: O(n) Space Complexity: O(n) This small exercise reinforces foundational reasoning that scales up when designing more complex systems — where reversing, mirroring, or symmetry detection shows up in data validation, pattern recognition, and even natural language tasks. 🔗 GitHub: https://lnkd.in/gaim_PJS #Python #LeetCode #CodingChallenge #160DaysOfCode #ProblemSolving #DSA #AIEngineerJourney
To view or add a comment, sign in
-
🌟 Day 22 of 30 – LeetCode Challenge | Merge Two Sorted Linked Lists 🔗 Today’s challenge was all about linked lists — one of the most fundamental data structures in coding interviews. 🔍 Problem Summary You are given the heads of two sorted linked lists: list1 = [1,2,4] list2 = [1,3,4] You must merge them into a single sorted linked list by splicing nodes (not creating new values). ✔️ Output [1,1,2,3,4,4] 💡 Approach To merge both lists efficiently, I used a two-pointer technique: Create a dummy node to simplify pointer operations Compare the current nodes of both lists Append the smaller node to the new list Move the corresponding pointer forward Finally, attach any remaining nodes This approach ensures the list stays sorted at all times. ⚙️ Time & Space Complexity Time Complexity: O(n + m) (because we iterate through both lists once) Space Complexity: O(1) (no extra space except pointer variables) 🏆 What I Learned Today ✔ How to merge sorted sequences efficiently ✔ Importance of dummy nodes to simplify linked list manipulation ✔ Better understanding of pointer movement ✔ Linked Lists become easier once you visualize node connections #LeetCode #Day22 #CodingChallenge #LinkedLists #Python #DataStructures #Algorithms #30DaysOfCode #MTechJourney
To view or add a comment, sign in
-
-
💯 Day 46 / 100 – 100 Days of #LeetCode Challenge 🔁 Problem: 326. Power of Three Goal: Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three if there exists an integer x such that n == 3ˣ. Approach: ✅ Initialize a list with [1] (since 3⁰ = 1). ✅ Iterate from 1 to n, compute powers of 3 using temp = 3 ** i. ✅ Break the loop if temp > n. ✅ Store valid powers in a list and check if n exists in it. Complexity: ⏱ Time: O(log₃n) – since powers of 3 grow exponentially. 🗂 Space: O(log₃n) – storing powers of 3 up to n. 💡 Key Takeaway: Patterns like “power of a number” can be generalized using loops and exponentiation. Recognizing exponential growth helps optimize logic for mathematical problems. #100DaysOfLeetCode #Day46 #Python #Math #ProblemSolving #LeetCode #CodingChallenge #PowerOfThree #LogicalThinking
To view or add a comment, sign in
-
-
🚀 DSA Challenge – Day 90 Problem: Single Element in a Sorted Array ⚙️🔍 This problem was a clever application of binary search where understanding the index patterns of paired elements was the key to achieving logarithmic efficiency. 🧠 Problem Summary: You are given a sorted array where every element appears exactly twice, except for one element that appears only once. Your task: return that single element in O(log n) time and O(1) space. ⚙️ My Approach: 1️⃣ Used binary search to narrow down the segment containing the single element. 2️⃣ Checked if the middle element breaks the pairing rule — if so, that’s our unique number. 3️⃣ Observed the pattern: Before the single element, pairs start at even indices. After it, pairs start at odd indices. 4️⃣ Used this parity observation to adjust the search boundaries efficiently. 📈 Complexity: Time: O(log n) → Binary search halves the search space each step. Space: O(1) → Constant space used. ✨ Key Takeaway: Sometimes, the structure of sorted pairs reveals more than meets the eye — a small parity trick transforms a linear scan into a logarithmic search. ⚡ 🔖 #DSA #100DaysOfCode #LeetCode #ProblemSolving #BinarySearch #Algorithms #CodingChallenge #Python #Optimization #EfficientCode #TechCommunity #InterviewPrep #LearningByBuilding #CodeEveryday
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