✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙️ Complexity: ⏱️ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
"Converting Sorted Array to Balanced BST in #100DaysOfCode"
More Relevant Posts
-
✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙ Complexity: ⏱ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 32 of #100DaysOfCode Challenge 📘 LeetCode Problem 108: Convert Sorted Array to Binary Search Tree 🧩 Problem Statement: Given a sorted array, convert it into a height-balanced Binary Search Tree (BST). A height-balanced BST ensures both left and right subtrees of every node differ in height by at most 1. 💡 Simple Idea: Pick the middle element as the root. Elements on the left become the left subtree. Elements on the right become the right subtree. Repeat the same steps recursively. ⚙ Complexity: ⏱ Time: O(n) 💾 Space: O(log n) (recursion stack height) 🌱 Step by step, building balance in both code and logic! #Day32 #100DaysOfCode #LeetCode #Java #DSA #BinarySearchTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #110 — Balanced Binary Tree 📘 Problem: Given the root of a binary tree, determine if it is height-balanced — that is, for every node, the height difference between the left and right subtree should not exceed 1. Example: Input → [3,9,20,null,null,15,7] Output → true 🧠 My Approach: I used a bottom-up recursive approach to check balance efficiently. 1️⃣ For each node, I calculated the height of its left and right subtrees. 2️⃣ If any subtree was unbalanced, I returned -1 immediately to stop further checks. 3️⃣ If the height difference between left and right subtrees was greater than 1, I marked the tree as unbalanced. 4️⃣ Otherwise, I returned the height of the current node as 1 + max(left, right). This approach ensures every node is visited only once — making it O(n) in time complexity. 💡 What I Learned: ✅ The difference between top-down and bottom-up recursion in tree problems ✅ How to optimize recursion by early termination when imbalance is detected ✅ Strengthened my understanding of recursive depth and height calculation in binary trees #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
✨ LeetCode 1657 – Determine if Two Strings Are Close Today I solved an interesting string problem that combines character frequency analysis with transformation logic. In this problem, we’re given two strings and need to determine if one can be transformed into the other using two operations: Swap any two existing characters. Transform all occurrences of one existing character into another existing character (and vice versa). To solve this, I analyzed the conditions that make two strings “close.” They must have: The same set of unique characters, and The same frequency distribution of those characters (even if attached to different letters). By counting character frequencies, verifying the presence of the same characters, and comparing sorted frequency arrays, we can efficiently determine if the strings are close. Time Complexity: O(n) Space Complexity: O(1) This problem helped me strengthen my understanding of frequency mapping and string transformations — a great mix of logic and pattern recognition! ⚡ #LeetCode #Java #DSA #ProblemSolving #StringManipulation #CodingJourney #LearningEveryday
To view or add a comment, sign in
-
-
✅Day 41 : Leetcode 153 - Find Minimum in Rotated Sorted Array #60DayOfLeetcodeChallenge 🧩 Problem Statement Given a sorted array that has been rotated at an unknown pivot, find the minimum element in the array. The array contains unique elements, and the solution must run in O(log n) time. 💡 My Approach I used a binary search technique to efficiently find the minimum element. I maintained two pointers, low and high. At each step, I calculated the mid-point. If the left part (nums[low] to nums[mid]) was sorted, I updated my answer with the smaller of nums[low] and current ans, and moved low to mid + 1. Otherwise, I updated my answer with nums[mid] and moved high to mid - 1. This approach ensures we keep narrowing the search space toward the minimum element. ⏱️ Time Complexity O(log n) — Because the search space is halved in each iteration. #BinarySearch #LeetCode #RotatedSortedArray #DSA #CodingPractice #Java #ProblemSolving
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 68 String Manipulation Problem 1:- Largest Odd Number in String Task:- Given a numeric string, return the largest-valued odd number (as a substring) or an empty string if none exists. Example: Input: num = "35427" → Output: "35427" My Approach: Started scanning the string from right to left. The first odd digit encountered marks the end of the required substring. Returned the substring from start to that index. Time Complexity:- O(N) Space Complexity:- O(1) Sometimes, it’s not about complex algorithms just a small logical observation can lead to an efficient solution. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #CodeNewbie #StringManipulation #LogicBuilding #CleanCode
To view or add a comment, sign in
-
-
📌 Day 2/100 – Remove Element (LeetCode 27) 🔹 Problem: Given an integer array nums and a value val, remove all instances of that value in-place and return the new length of the array. The order of elements can be changed. 🔹 Approach: Used the two-pointer technique to efficiently modify the array in-place. One pointer iterates through the array, while the other tracks the position to overwrite non-val elements. Returned the position of the second pointer as the new length. 🔹 Key Learning: Strengthened understanding of in-place array manipulation. Improved logic building for pointer movement and conditional overwriting. Learned how to minimize extra space usage while maintaining readability and clarity. Another small yet powerful step toward mastering array-based problems! 💻 🔥 #100DaysOfCode #LeetCode #Java #ProblemSolving #TwoPointers #DSA #CodingJourney
To view or add a comment, sign in
-
-
🚀 #PostLog20 “Two Sum II - Input Array Is Sorted” (#LeetCode167) Since the array is already sorted, instead of using brute force, I applied a binary search for the complement of each number. This helped reduce unnecessary comparisons and improved efficiency 💡 🧩 Problem: Find two numbers that add up to a target in a sorted array and return their indices (1-based). 💻 Approach Used: For each number i, perform a binary search on the right subarray to find target - numbers[i]. Adjust search boundaries (s and e) until the pair is found. Return indices as soon as the sum matches the target. 📸 Snippet from my submission: ✅ 24/24 test cases passed ⚡ Runtime: 5 ms (Beats 16.59%) 💾 Memory: 47.44 MB #LeetCode #Java #ProblemSolving #BinarySearch #CodingJourney #TechLearning #DSA
To view or add a comment, sign in
-
-
💡 LeetCode #977 — Squares of a Sorted Array Today I solved LeetCode Problem 977: Squares of a Sorted Array ⚡ Problem Summary: Given a sorted array of integers nums, return an array of the squares of each number, also sorted in non-decreasing order. Key Idea: Even though the input array is sorted, squaring can make negative numbers larger. So we use the two-pointer approach: One pointer at the start (left), one at the end (right). Compare absolute values, place the larger square at the end of the result array, and move pointers accordingly. #LeetCode #Java #DSA #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
Explore related topics
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