LeetCode 110 | Balanced Binary Tree | Java | Easy | D F S & Bottom-Up Recursion | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 110 - Balanced Binary Tree using Depth-First Search and Bottom-Up Recursion in Java.
A binary tree is defined as height-balanced if, for every node in the tree, the height difference between its left and right subtrees is at most one. A naive top-down approach computes subtree heights repeatedly for each node, resulting in inefficient quadratic O(n^2) time complexity on skewed trees. Instead, we implement an optimal bottom-up post-order traversal that evaluates both heights and balance conditions in a single unified recursion.
Our recursive function computes the height of subtrees from the leaves up to the root. If either child subtree is unbalanced, or if the absolute difference between the left and right subtree heights exceeds one, we immediately return a sentinel value of -1 to avoid further unnecessary computation. This early-exit pattern guarantees that each node is visited exactly once, achieving optimal linear O(n) runtime and O(h) recursion stack space.
π₯ Problem Highlights
β Replaced quadratic top-down height recalculations with linear bottom-up post-order DFS
β Used a sentinel value of -1 to immediately stop computation upon detecting any unbalanced subtree
β Checked that the absolute difference between left and right subtree heights is at most one at each node
β Handled the empty tree base case, where root is null, by returning true
β Clean, idiomatic Java implementation
β Achieved optimal O(n) time and O(h) auxiliary stack space complexity
β±οΈ TIMESTAMPS
00:00 Intro
00:54 Problem Understanding and Constraints
01:46 Bottom-Up DFS and Sentinel Optimization
03:08 Post-Order Recursive Traversal Strategy
04:19 Java Code Walkthrough
05:26 Execution and Test Cases
06:50 Time Complexity Analysis - O(n)
07:32 Space Complexity Analysis - O(h)
08:04 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/8-February/110-Balanced-Binary-Tree
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Binary Tree Traversals
Bottom-Up Post-Order DFS
Recursion and Early Pruning
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is the bottom-up post-order DFS approach for checking a balanced binary tree O(n) instead of O(n^2)?
1οΈβ£ Because each node's height is calculated only once and returned upward rather than recalculated from the root for every node
2οΈβ£ Because binary trees can never have height greater than log(n)
3οΈβ£ Because the tree is sorted before checking balance
4οΈβ£ Because we only inspect the left subtree and ignore the right subtree
#LeetCode110 #LeetCode #BalancedBinaryTree #Java #JavaDSA #Tree #BinaryTree #DFS #Recursion #DepthFirstSearch #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 110 - Balanced Binary Tree using Depth-First Search and Bottom-Up Recursion in Java.
A binary tree is defined as height-balanced if, for every node in the tree, the height difference between its left and right subtrees is at most one. A naive top-down approach computes subtree heights repeatedly for each node, resulting in inefficient quadratic O(n^2) time complexity on skewed trees. Instead, we implement an optimal bottom-up post-order traversal that evaluates both heights and balance conditions in a single unified recursion.
Our recursive function computes the height of subtrees from the leaves up to the root. If either child subtree is unbalanced, or if the absolute difference between the left and right subtree heights exceeds one, we immediately return a sentinel value of -1 to avoid further unnecessary computation. This early-exit pattern guarantees that each node is visited exactly once, achieving optimal linear O(n) runtime and O(h) recursion stack space.
π₯ Problem Highlights
β Replaced quadratic top-down height recalculations with linear bottom-up post-order DFS
β Used a sentinel value of -1 to immediately stop computation upon detecting any unbalanced subtree
β Checked that the absolute difference between left and right subtree heights is at most one at each node
β Handled the empty tree base case, where root is null, by returning true
β Clean, idiomatic Java implementation
β Achieved optimal O(n) time and O(h) auxiliary stack space complexity
β±οΈ TIMESTAMPS
00:00 Intro
00:54 Problem Understanding and Constraints
01:46 Bottom-Up DFS and Sentinel Optimization
03:08 Post-Order Recursive Traversal Strategy
04:19 Java Code Walkthrough
05:26 Execution and Test Cases
06:50 Time Complexity Analysis - O(n)
07:32 Space Complexity Analysis - O(h)
08:04 Outro and Wrap-up
π Problem Folder and Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/February/8-February/110-Balanced-Binary-Tree
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/February/8-February/110-Balanced-Binary-Tree/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Easy Problems
Java DSA Preparation
Binary Tree Traversals
Bottom-Up Post-Order DFS
Recursion and Early Pruning
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is the bottom-up post-order DFS approach for checking a balanced binary tree O(n) instead of O(n^2)?
1οΈβ£ Because each node's height is calculated only once and returned upward rather than recalculated from the root for every node
2οΈβ£ Because binary trees can never have height greater than log(n)
3οΈβ£ Because the tree is sorted before checking balance
4οΈβ£ Because we only inspect the left subtree and ignore the right subtree
#LeetCode110 #LeetCode #BalancedBinaryTree #Java #JavaDSA #Tree #BinaryTree #DFS #Recursion #DepthFirstSearch #DSA #CodingInterview #ProblemSolving #LeetCodeEasy #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
