Problem: Given a binary tree, determine if it is height-balanced, meaning for every node: |height(left) - height(right)| ≤ 1 Key Insight: Instead of calculating height repeatedly (which leads to O(N²)), we can compute height + balance check together in one DFS traversal, achieving O(N) time complexity. Optimal Strategy: 1.Use postorder traversal (bottom-up) 2.Return -1 immediately if any subtree is unbalanced 3.Otherwise, return the height of the subtree Why this matters? This pattern is widely used in: Tree Height problems Diameter of Binary Tree Maximum Path Sum Balanced Tree checks #Python #ProblemSolving
Check if Binary Tree is Height Balanced
More Relevant Posts
-
LeetCode Problem 1461: "Check if a binary string contains all binary codes of size k": Given a binary string s and an integer k, return true if every binary code of length k is a substring of s. Otherwise, return false. Approach: I simply used a sliding window and a hash map where hash map stores the seen substrings of size k. Two pointers keep track of each substring of size k. A count variable is initialized to 0 which is incremented each time when a unique unseen substring of size k is seen, if this count reaches the max possible number of unique binary codes of size k (i.e. 2^k) the function returns True else False. #LeetCode #Python #BitManipulation #Strings #SlidingWindow #DataStructures #Algorithms #CompetitiveProgramming #ProblemSolving #OptimalSolution
To view or add a comment, sign in
-
-
🚀 Day 42 / 200 – Minimum Window Substring (Hard) Solved LeetCode 76 – Minimum Window Substring today! 💪 This problem was a great test of Sliding Window + HashMap concepts. The challenge was to find the smallest substring that contains all characters of another string (including duplicates). 🔎 Key Learnings: Mastered the two-pointer (left & right) sliding window technique Used frequency counters to track required characters Optimized window shrinking to ensure minimum length Improved understanding of handling duplicate character constraints ✅ 268 / 268 test cases passed ⚡ Runtime: 75 ms 🐍 Language: Python Problems like this strengthen real-world problem-solving skills and improve optimization thinking. Consistency > Motivation. 200 Days. No excuses. 💯 #Day42 #200DaysChallenge #LeetCode #Python #DataStructures #Algorithms #SlidingWindow #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
✅ Day 6 of #DSAPrep > Problem: Reverse Integer > Platform: LeetCode > Concept: Number Manipulation + Overflow Handling Reversed an integer using modulo (%) and integer division (//). Handled: Negative numbers using a sign variable 32-bit integer overflow constraint Returned 0 if the reversed number goes outside the 32-bit signed integer range > Time Complexity: O(log n) > Space Complexity: O(1) #DSAPrep #Algorithms #Python #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Comparing 2D Unsteady Linear Convection vs Pure Diffusion (Python + NumPy + Matplotlib): using an explicit finite-difference approach in Python. Both cases start from the same initial condition (a localised high-velocity region), but the evolution is fundamentally different: Implementing a side-by-side numerical comparison between: 🔹 2D Fully Unsteady Linear Convection 🔹 2D Fully Unsteady Pure Diffusion Linear Convection → the profile translates across the domain while largely retaining its shape (advection-dominated behaviour). c = constant (Makes Convection term linear) Pure Diffusion → the profile spreads and smooths out over time. nu = Kinematic Viscosity #CFD #FluidMechanics #NumericalMethods #Python #ScientificComputing #LearningByDoing
To view or add a comment, sign in
-
Solved an interesting array problem using prefix sums + hash maps 🚀 This solution efficiently finds the longest subarray based on a comparison condition (arr[i] > k) by converting the problem into a prefix sum balance and tracking first occurrences with a hashmap. ✅ O(n) time complexity ✅ Smart use of prefix sums ✅ Great example of turning a complex condition into a simple math problem Always fun when problem-solving meets optimization! 💻📊 #Python #DataStructures #Algorithms #CodingInterview #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 96 — #100DaysOfLeetCode ✅ Problem: Binary Number with Alternating Bits (Easy) Today’s challenge was to check whether a given integer’s binary representation has alternating bits — meaning no two adjacent bits are the same. 🧠 Approach: Convert number to binary. Check if it contains "11" or "00". If yes → not alternating. Otherwise → valid alternating pattern. ⚡ Example n = 5 → binary = 101 → ✔ alternating ⏱ Complexity Time: O(k) Space: O(1) (k = number of bits) #LeetCode #DSA #CodingChallenge #Python #ProblemSolving
To view or add a comment, sign in
-
-
Solved "Encode and Decode Strings" (LeetCode 271) The trick? Use a length-prefixed encoding: <length>#<string> For example: ["lint","code"] → "4#lint4#code" Why this works: The length tells the decoder exactly how many characters to read No delimiter conflicts — even if the string itself contains # or digits O(n) time and space A clean example of how a simple idea can handle all edge cases elegantly. #leetcode #dsa #python #problemsolving
To view or add a comment, sign in
-
✅ Day 5 of #DSAPrep > Problem: Palindrome Number > Platform: LeetCode > Concept: Number Manipulation Checked if an integer is a palindrome without converting it to a string. Reversed the number using modulo (%) and integer division (//), then compared it with the original value. Handled edge case: Negative numbers are not palindromes. > Time Complexity: O(log n) > Space Complexity: O(1) #DSAPrep #DataStructures #Algorithms #Python #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🔥 Day 30 of #100DaysOfCode 📌 LeetCode 26 – Remove Duplicates from Sorted Array Today’s problem focused on in-place array manipulation and strengthening the two-pointer technique. 🧠 Problem Summary Given a sorted array, remove duplicates such that each unique element appears only once. Return the count of unique elements (k). Important: Do it in-place with O(1) extra space. 💡 Key Insight Because the array is already sorted, duplicates are always adjacent. So instead of checking all elements repeatedly: 👉 Compare current element with previous one. 👉 If different → place it at the next unique index. This is where the Two Pointer Approach shines: Pointer i → traverses array Pointer k → tracks position of next unique element 📊 Complexity ⏱ Time: O(n) 📦 Space: O(1) Consistency > Motivation 30 Days Done. Still going strong 💪 #Day30 #LeetCode #DSA #100DaysOfCode #Python #CodingJourney
To view or add a comment, sign in
-
-
Day 4/60 ✅ Solved Form the Largest Number. The key idea was customizing the sorting logic. Instead of normal numerical sorting, I compared numbers as strings by checking which combination — a+b or b+a — forms a larger value. This ensures the final concatenation produces the maximum possible number. Time Complexity: O(n log n) A good reminder that sometimes the problem isn’t about complex logic — just about choosing the right comparison strategy. #60DayChallenge #DSA #ProblemSolving #Python
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