🌳 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
Solved Balanced Binary Tree with Post-Order DFS in Java
More Relevant Posts
-
#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
-
-
🚀 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-70) LeetCode #3234 – Count Substrings With Dominant Ones Just solved an interesting binary string problem where a substring is considered dominant if the number of 1s is greater than or equal to the square of the number of 0s. 🔍 Challenge: Efficiently count all such substrings in a given binary string. 🧠 My Approach (Java): Used a prefix sum array to track the balance between 1s and 0s Explored substring ranges with a smart condition check Focused on clean logic and edge case handling 📈 Why it matters: This problem blends math with string manipulation and highlights how preprocessing (like prefix sums) can drastically reduce brute-force overhead. 📎 Code snippet attached — open to feedback or alternate strategies! #LeetCode #Java #DSA #BinaryStrings #ProblemSolving #CodingInPublic #TechJourney
To view or add a comment, sign in
-
-
🔹 Day 50: Maximum Average Subarray I (LeetCode #643) 📌 Problem Statement: Given an integer array nums and an integer k, find the contiguous subarray of length k that has the maximum average value and return this value. ✅ My Approach: I used the sliding window technique to efficiently calculate the sum of subarrays of length k. First, I computed the sum of the first k elements. Then, as the window slid forward, I subtracted the element going out and added the new element entering the window. I continuously updated the maximum sum encountered and finally returned the average by dividing it by k. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 99.90%) Memory: 56.27 MB (Beats 66.43%) 💡 Reflection: This problem highlighted how the sliding window technique can drastically improve performance over recalculating sums for each subarray, making the approach both elegant and efficient. 🚀 #LeetCode #Java #SlidingWindow #Optimization #100DaysOfCode #Day50
To view or add a comment, sign in
-
-
🚀Day 38/100 - Problem of the day :- Ones and Zeros. 🎯 Goal Solve the “Ones and Zeroes” problem using Dynamic Programming by selecting the maximum number of binary strings within the constraints of m zeroes and n ones. 💡 Core Idea Convert each string into its count of zeroes and ones, then use a 2D DP array where dp[i][j] tells the maximum number of strings that can be formed with i zeroes and j ones. Iterate in reverse to avoid overwriting states, updating DP for each string. 📌 Key Takeaway This problem shows how DP shines when choices are constrained by multiple resource limits. Turning strings into (zero, one) pairs and applying knapsack logic simplifies the solution beautifully. 📄 Space Complexity O(m × n) ⏱️Time Complexity O(k × m × n) where k is the number of strings #100DaysChallenge #Java #DataStructure #Day38 #Leetcode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
✅Day 50 : Leetcode 1283 - Find the Smallest Divisor Given a Threshold #60DayOfLeetcodeChallenge 🧩 Problem Statement: Given an array of integers nums and an integer threshold, find the smallest positive integer divisor such that the sum of each element divided by the divisor (rounded up to the nearest integer) is less than or equal to the threshold. 💡 My Approach: Binary Search on Divisor Range: The smallest possible divisor is 1. The largest possible divisor is the maximum element in nums. Check Function: For a given divisor, calculate the sum of ceil(nums[i] / divisor) for all elements. If the total sum ≤ threshold → we can try smaller divisors. Otherwise, increase the divisor. Repeat until optimal divisor is found. ⏱️ Time Complexity: O(n * log(max(nums))) Each binary search iteration takes O(n) to compute the sum. #Algorithms #BinarySearch #LeetCode #Java #ProblemSolving #DSA #100DaysOfCode #CodeNewbie
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
-
-
📌 Day 21/100 – Sort an Array (LeetCode 912) 🔹 Problem: Given an integer array, sort it in ascending order without using built-in sorting functions and ensure the solution runs in O(n log n) time. 🔹 Approach (Merge Sort): Divide the array into two halves recursively Sort each half independently Merge the two sorted halves while maintaining order Uses Divide & Conquer and guarantees stable sorting 🔹 Key Learnings: Merge Sort ensures worst-case O(n log n) time unlike QuickSort Works well for linked lists & large datasets because of stability Uses extra space due to temporary list merging Great example of recursion + two-pointer merging technique #100DaysOfCode #Day21 #LeetCode #Java #DSA #MergeSort #ProblemSolving #CodingJourney
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
-
-
📌 Day 38/100 – Make array elements equal to zero (LeetCode 3354) 🔹 Problem: Given an integer array nums, each element can be a number or zero. You need to find how many zeros in the array can be replaced by either +1 or -1 such that the total sum on both sides of that zero (left and right) remains balanced or differs by 1. 🔹 Approach: First, calculate the total sum s of all elements. Maintain a prefix sum l as you iterate. For each zero: If l * 2 == s, both +1 and -1 replacements are valid → add 2 to ans. If |l * 2 - s| == 1, only one replacement is valid → add 1 to ans. Return the total count ans. 🔹 Key Learning: Prefix sums simplify balance-based problems. Comparing 2 * prefixSum with total sum helps quickly check left-right equilibrium. 🔹 Complexity: Time: O(n) — single pass through array Space: O(1) — no extra storage used 🔹 Hashtags: #Day38Of100 #LeetCode3354 #100DaysOfCode #Java #DSA #ProblemSolving #PrefixSum #CodingChallenge
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