✅ 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
How to Convert a Sorted Array to a Balanced BST
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
-
-
#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
-
-
✨ 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
-
-
🚀 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
-
-
✅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
-
-
🚀 Just Solved LeetCode #1367 — Linked List in Binary Tree 📘 Problem: Given the head of a linked list and the root of a binary tree, determine whether all the elements of the linked list appear as a downward path in the binary tree. Downward means starting from any node and moving only to child nodes. Example: Input → head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3] Output → true Explanation → Nodes in blue form a subpath in the binary tree. 🧠 My Approach: I used recursion to check whether the linked list sequence can be found starting from any node in the binary tree. 1️⃣ For each node in the tree, check if the list starting from `head` matches the downward path from that node. 2️⃣ If not, recursively check the left and right subtrees. 3️⃣ Used a helper function `checkPath()` to verify if a valid path continues as the recursion goes deeper. 💡 What I Learned: ✅ How to combine linked list and binary tree traversal logic ✅ How recursion can efficiently check multiple starting points ✅ Improved my understanding of tree path traversal and backtracking logic #LeetCode #Java #DSA #BinaryTree #LinkedList #CodingUpdate #LearningByDoing
To view or add a comment, sign in
-
-
23/30 days✅ Solved LeetCode Problem : #70 – Climbing Stairs The challenge was to determine how many distinct ways one can reach the top of a staircase with n steps, where at each step you can either climb 1 or 2 steps. The logic behind the problem is similar to the Fibonacci sequence, where each state depends on the sum of the previous two states. I implemented the solution in Java, using an iterative approach with three variables to optimize space complexity. Instead of using recursion or an array, the approach updates values in constant space (O(1)) while maintaining linear time complexity (O(n)). Here’s the logic in brief: Initialize a = 0, b = 1, and c = 1. For each step, compute c = a + b, then shift values forward (a = b, b = c). Return c as the total number of distinct ways to reach the top. #LeetCode #Java #DynamicProgramming #ProblemSolving #CodingJourney
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