🔥 Day 82 of my LeetCode Journey 🔥 📘 Problem: 110. Balanced Binary Tree 🎯 Difficulty: Easy 🔹 Problem Statement: Given a binary tree, determine if it is height-balanced. A binary tree is balanced if the height difference between the left and right subtree of every node is not more than 1. 🔹 Approach Used: Traverse the tree using Depth First Search (DFS) Calculate the height of left and right subtrees recursively If the height difference exceeds 1, return -1 to indicate the tree is not balanced Otherwise, return the height of the current node Finally, check if the returned value is -1 or not 🔹 Key Concepts: Binary Tree traversal Depth First Search (DFS) Recursion Height calculation of tree 🔹 Learning: Instead of calculating heights separately for every node (which would increase complexity), this approach combines height calculation and balance checking in a single traversal, making the solution more efficient. Time Complexity: O(n) Space Complexity: O(h) (recursion stack) #LeetCode #Day82 #Java #BinaryTree #DFS #Recursion #DSA #ProblemSolving #CodingJourney
Balanced Binary Tree LeetCode Solution
More Relevant Posts
-
Today I solved LeetCode 110 – Balanced Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, determine if it is height-balanced. A binary tree is balanced if: The height difference between the left and right subtree of every node is not more than 1. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Height calculation 🧠 Approach: Use DFS to calculate the height of each subtree. For every node: Get height of left subtree Get height of right subtree Check if the absolute difference is greater than 1: If yes → tree is not balanced Optimize by returning -1 early when imbalance is found to avoid unnecessary calculations. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Combining height calculation with validation in one traversal. Optimizing recursive solutions with early stopping. Understanding balanced vs unbalanced trees. Strengthening DFS and recursion skills. Improving step by step with tree problems Consistency is building confidence every day #LeetCode #DSA #BinaryTree #BalancedTree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 19 – Balanced Binary Tree 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on checking whether a binary tree is height-balanced, which is a key concept in many tree problems. Problem Balanced Binary Tree A binary tree is balanced if: For every node: |height(left subtree) - height(right subtree)| ≤ 1. We use recursion (DFS) to: 1️⃣ Calculate height of left subtree 2️⃣ Calculate height of right subtree 3️⃣ Check balance condition. Time Complexity : O(N) . Space Complexity : O(H) . Github Code :- https://lnkd.in/gfDCrpfJ. #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #Recursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day19 #DeveloperJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 66/100 🚀 | #100DaysOfDSA Solved LeetCode 14 – Longest Common Prefix today. The task was to find the longest common prefix string among an array of strings. Initially, I couldn’t come up with this optimal approach and first solved it using a brute-force O(n²) method, comparing characters across all strings. Approach (Optimized): Used sorting to simplify the problem. • Sorted the array of strings. • Took the first and last strings after sorting. • Compared characters of both strings until they differ. • The common prefix between these two will be the answer for all strings. This works because after sorting, the most different strings will be at the extremes. Time Complexity: O(n log n + m) (where n = number of strings, m = length of prefix comparison) Space Complexity: O(1) (ignoring sorting space) Key takeaway: Sorting can sometimes reduce multi-string comparison problems to just comparing two extreme cases, making the solution much simpler and efficient. #100DaysOfDSA #LeetCode #DSA #Java #Strings #Sorting #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Today I solved LeetCode 104 – Maximum Depth of Binary Tree. 🧩 Problem Summary: Given the root of a binary tree, return its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree traversal 🧠 Approach: Use DFS (recursion) to explore the tree. For each node: Recursively calculate the depth of the left subtree. Recursively calculate the depth of the right subtree. The depth of the current node = 1 + max(left depth, right depth) Base case: if node is null, return 0. ⏱ Time Complexity: O(N) — Each node is visited once. 📚 What I Learned: Understanding tree depth calculation. Strengthening recursion and DFS concepts. Difference between maximum depth vs minimum depth. Building intuition for tree-based problems. Strong fundamentals in trees make advanced problems easier Consistency is the key to improvement #LeetCode #DSA #BinaryTree #MaximumDepth #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
Day 22 – Binary Tree Level Order Traversal 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem was about traversing a binary tree level by level (BFS traversal). Approach (Recursive Level-wise Traversal) ✔ First calculate height ✔ Then collect nodes for each level separately. Time Complexity : O(N²). Space Complexity : O(N) . GitHub Code :- https://lnkd.in/gB8w6Fhz #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #BFS #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day22 #DeveloperJourney
To view or add a comment, sign in
-
-
Today I solved LeetCode 572 – Subtree of Another Tree. 🧩 Problem Summary: Given the roots of two binary trees root and subRoot, check whether subRoot is a subtree of root. A subtree is a node in root along with all its descendants that forms a tree identical to subRoot. 💡 Key Concepts Used: Binary Trees Recursion Depth First Search (DFS) Tree comparison 🧠 Approach: Traverse the main tree (root) using recursion. At each node: Check if the subtree starting from that node is identical to subRoot. Use a helper function to compare two trees: If both nodes are null → true If one is null → false If values differ → false Recursively compare left and right subtrees If a match is found at any node, return true. Otherwise, continue checking in left and right subtrees. 📚 What I Learned: Combining tree traversal with tree comparison. Reusing logic (Same Tree concept) effectively. Breaking complex problems into smaller reusable parts. Strengthening DFS and recursive problem-solving. Building strong foundations in tree problems step by step Consistency is the real game changer #LeetCode #DSA #BinaryTree #Subtree #DFS #Recursion #Java #CodingJourney #ProblemSolving #100DaysOfCode #TechGrowth
To view or add a comment, sign in
-
-
🔥 Day 85 of my LeetCode Journey 🔥 📘 Problem: 257. Binary Tree Paths 🎯 Difficulty: Easy 🔹 Problem Statement: Given the root of a binary tree, return all root-to-leaf paths in any order. A leaf is a node with no children. 🔹 Approach Used: Use recursion to traverse the tree If a node is a leaf → add its value as a path For non-leaf nodes: Recursively get paths from left subtree Recursively get paths from right subtree Append current node value with "->" to each child path Collect and return all paths 🔹 Key Concepts: Recursion Depth-First Search (DFS) Tree traversal String manipulation 🔹 Learning: This problem strengthens understanding of DFS traversal and how to build paths dynamically during recursion. It also shows how to combine results from subtrees efficiently. ✅ Status: Accepted (Runtime: 5 ms) 📌 Break problems into smaller subproblems — recursion does the rest 🚀 #LeetCode #Day82 #Java #BinaryTree #DFS #Recursion #DSA #ProblemSolving
To view or add a comment, sign in
-
-
Day 20/100: The "Cheat Code" for String Rotations 🔄 I’m back on the grind! Today’s challenge was checking if one string is a rotation of another (e.g., "waterbottle" and "erbottlewat"). The Strategy: Instead of writing complex loops to shift characters, I used the Concatenation Trick: 1️⃣ Check if lengths are equal. 2️⃣ Create a new string by adding the first string to itself (s1 + s1). 3️⃣ Check if the second string exists inside that combined string. It’s a simple, elegant O(n) solution that shows how sometimes "working smarter" with data structures beats "working harder" with loops. 20% of the way there. Let's keep moving! 🚀 #100DaysOfCode #Java #DSA #Strings #ProblemSolving #Unit2 #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
Day 15 – Maximum Depth of Binary Tree 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on finding the maximum depth (height) of a binary tree, which is a fundamental concept used in many tree-related problems. Approach We use recursion (DFS) to compute the height of the tree. For every node: Height = 1 + max(height of left subtree, height of right subtree) Base case: If node == null → return 0. Time complexity : O(N) . Space Complexity : O(N). Github Code :- https://lnkd.in/gnDkqcFQ #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #TreeRecursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day15 #DeveloperJourney
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