Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 110 | Balanced Binary Tree | Java | Easy | D F S & Bottom-Up Recursion | CodeByTushu

Video information, playback and saved actions.

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
Β Β Β Β Β Β 
Videos related to "LeetCode 110 | Balanced Binary Tree | Java | Easy | D F S & Bottom-Up Recursion | CodeByTushu"

Related videos are temporarily unavailable.

Try again, or come back later.