Problem 110

Abstract / Summary

Determine if a binary tree is height balanced so that the depth difference between left and right subtrees of every node does not exceed one.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 0110.py

class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        failed = float("inf")

        def deeper(node):
            if not node:
                return 0

            l = deeper(node.left)
            r = deeper(node.right)
            if l == failed or r == failed or abs(l - r) > 1:
                return float("inf")
            else:
                return 1 + max(l, r)

        return deeper(root) != failed
16 lines | 460 bytes | submitted August 31, 2023 | GitHub   tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 59% (4+ million submissions).

Typical patterns to solve this problem:
  • Binary Tree
  • Depth First Search
  • Tree
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The provided implementation defines a Solution class with a single public method isBalanced. The method determines whether a binary tree, represented by TreeNode objects, satisfies the height-balanced property: for every node, the heights of its left and right subtrees differ by no more than one. The algorithm employs a depth-first recursion that simultaneously computes subtree heights and detects imbalance, returning a Boolean result.

Key Steps

Sentinel Value Definition

A special floating-point value, failed, is created to signal that an imbalance has been discovered in any part of the tree.

failed = float("inf")

Recursive Height Computation

A nested helper function deeper performs a post-order traversal. For a None node it returns a height of zero, establishing the base case for leaf children.

def deeper(node):
    if not node:
        return 0

Left and Right Subtree Evaluation

The function recursively obtains the heights of the left and right children.

l = deeper(node.left)
r = deeper(node.right)

Imbalance Detection and Propagation

If either child already reported an imbalance (l == failed or r == failed) or the current node’s subtrees differ in height by more than one, the function returns the sentinel failed. This return value propagates upward, preventing further unnecessary height calculations.

if l == failed or r == failed or abs(l - r) > 1:
    return float("inf")

Height Aggregation for Balanced Nodes

When the current node is balanced, its height is one plus the larger of its children’s heights.

else:
    return 1 + max(l, r)

Final Result Determination

The public method invokes deeper on the root and checks whether the sentinel was ever returned. A non-sentinel result indicates the entire tree is balanced.

return deeper(root) != failed

Edge Cases Handled

The algorithm gracefully manages an empty tree because the base case returns zero for a None root, and the final comparison against failed yields True.

When a subtree is already known to be unbalanced, the sentinel value is immediately returned without further descent, ensuring that deeper recursive calls do not waste cycles on already invalid portions of the structure.

The use of abs(l - r) > 1 captures the precise condition for imbalance at each node, covering scenarios where one side is significantly deeper than the other, including cases where one child is missing (None) and the other is a deep chain.

Because the sentinel is a floating-point infinity, it cannot be confused with any legitimate integer height, guaranteeing correct detection even for very tall trees.

Performance Optimizations

The most notable optimization is the early-exit mechanism implemented via the sentinel failed. As soon as an imbalance is detected at any node, the recursion stops propagating real height values and instead returns the sentinel upward. This prevents the algorithm from traversing the remainder of the tree once the answer is known, reducing the number of recursive calls in unbalanced cases.

The algorithm also avoids separate passes for height calculation and balance checking. By merging both concerns into a single post-order traversal, it eliminates redundant work that a naïve two-pass approach would incur.

Since each node is visited at most once and the height information is computed in constant time per node, the overall execution follows a linear pattern relative to the number of nodes, with minimal overhead from the sentinel checks.

The use of local variables (l, r) and direct arithmetic (abs, max) keeps the inner loop tight, contributing to fast execution on typical Python interpreters.

written by openai gpt

ELI5 - Simplified Explanation

Imagine a giant family tree where every person has at most two children dangling below them. This code is a clever helper that explores the tree from the top all the way down to the bottom to see if it looks neat and tidy.

First, the helper looks at an empty spot and decides that it has a height of zero. If it finds a person, it quickly runs over to check the left side and the right side to see how deep they go. It keeps track of those heights while it travels back up the branches.

As it climbs back up, it compares the height of the left side and the right side. If one side is much taller than the other, it marks the tree as broken by setting a special signal. If the sides are close enough in height, it adds one to the taller side to show the new height of that branch. By the time it finishes checking every single branch, it concludes whether the whole tree is balanced or if it has become too lopsided.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 8 | μ = 7.67 | σ = 0.94

Complexity Analysis

Time: linear
As a rule of thumb, the maximum input size for one second execution time is
  • factorial: up to 10 elements
  • exponential: up to 20 elements
  • polynomial: a few thousand elements
  • loglinear: up to 100,000 elements
  • linear: a few million elements
  • logarithmic: a billion elements
  • constant: practically unlimited
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Python Java TS
WalkCCC C++ Python Java
leetcode.ca C++ Python Rust JS Java Go TS
LeetcodeHelp C++ Python Java
MemoryLimitExceeded C++ Python
Progiez C++ Python Java
github aylei Rust
begeekmyfriend C
BigEggStudy C#
black-shadows Python
bujosa Rust
chandansgowda Python
charles-wangkai Java
chenyanzhe C++
DimitrisJim JS
everthis JS
fartem Ruby
fengvyi C++
gaosq0604 Python
halfrost Go
haoel C++
JoshCrozier JS
keineahnung2345 C++
lennylxx C
linouk23 C++
lkwq007 Python
luangong C++
MacJim C++
muhammadfarhankt Go
qiyuangong Python
ruoru Rust
shreymehul C#
sonugiri1043 C++
suxrobGM C#
varunu28 Java
vthwang Java
wisdompeak C++
withaarzoo C++
XiaotaoGuo C++
yuanliu666 Kotlin
YuriSpiridonov Python
zfang399 C++
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, Go, Java, JavaScript, Python, Rust, TypeScript
LeetcodeHelp C++, Java, Python
MemoryLimitExceeded Python, C++
Progiez C++, Java, Python
aylei Rust
begeekmyfriend C
BigEggStudy C#
black-shadows Python, Python
bujosa Rust
chandansgowda Python
charles-wangkai Java
chenyanzhe C++
DimitrisJim JavaScript
everthis JavaScript
fartem Ruby
fengvyi C++
gaosq0604 Python
halfrost Go
haoel C++
JoshCrozier JavaScript
keineahnung2345 C++
lennylxx C
linouk23 C++
lkwq007 Python
luangong C++
MacJim C++
muhammadfarhankt Go
qiyuangong Python
ruoru Rust
shreymehul C#
sonugiri1043 C++
suxrobGM C#
varunu28 Java
vthwang Java
wisdompeak C++
withaarzoo C++
XiaotaoGuo C++
yuanliu666 Kotlin
YuriSpiridonov Python
zfang399 C++
58 links | 42 authors | 11 languages

Videos

These are a few popular YouTube videos for this problem:
YouTube video 'Balanced Binary Tree - Leetcode 110 - Python'
NeetCode 14 mins
Balanced Binary Tree - Leetcode 110 - Python
YouTube video 'Balanced Binary Tree - Solution | Binary Trees | Data Structure and Algorithms in JAVA'
Pepcoding 11 mins
Balanced Binary Tree - Solution | Binary Trees | Data Structure and Algorithms in JAVA
YouTube video 'Balanced Binary Tree (Top-down, Bottom-up)| Live Coding with Explanation | Leetcode #110'
Algorithms Made Easy 7 mins
Balanced Binary Tree (Top-down, Bottom-up)| Live Coding with Explanation | Leetcode #110
YouTube video 'Balanced Binary Tree - Question | Binary Trees | Data Structure and Algorithms in JAVA'
Pepcoding 3 mins
Balanced Binary Tree - Question | Binary Trees | Data Structure and Algorithms in JAVA
YouTube video 'LeetCode 110 | Balanced Binary Tree | Solution Explained (Java + Whiteboard)'
Xavier Elon 12 mins
LeetCode 110 | Balanced Binary Tree | Solution Explained (Java + Whiteboard)
YouTube video 'Balanced Binary Tree | balanced binary tree | leetcode 110 | tree'
Naresh Gupta 9 mins
Balanced Binary Tree | balanced binary tree | leetcode 110 | tree
YouTube video 'Balanced Binary Tree | Simple Clean Approach | Leetcode 110 | codestorywithMIK'
codestorywithMIK 17 mins
Balanced Binary Tree | Simple Clean Approach | Leetcode 110 | codestorywithMIK
YouTube video 'Balanced Binary Tree | Leet code 110 | Theory explained + Python code'
Sai Anish Malla 16 mins
Balanced Binary Tree | Leet code 110 | Theory explained + Python code
YouTube video '[Java] Leetcode 110. Balanced Binary Tree [Binary Tree #15]'
Eric Programming 9 mins
[Java] Leetcode 110. Balanced Binary Tree [Binary Tree #15]
YouTube video '110. Balanced Binary Tree Java Solution'
Code with Carter 10 mins
110. Balanced Binary Tree Java Solution
YouTube video 'Balanced Binary Tree #leetcode #binarytree'
Deepti Talesra 15 mins
Balanced Binary Tree #leetcode #binarytree
YouTube video '110. Balanced Binary Tree | JavaScript | Blind 75 | DFS | LeetCode'
LeetCodeWithMonu 12 mins
110. Balanced Binary Tree | JavaScript | Blind 75 | DFS | LeetCode
YouTube video 'Balanced Binary Tree - Trees - Python'
CheatCode Ninja 5 mins
Balanced Binary Tree - Trees - Python
YouTube video 'Balanced Binary Tree || Binary Tree 13 || Placement Preparation Series | C++ | LeetCode 110 |'
Bro Coders 13 mins
Balanced Binary Tree || Binary Tree 13 || Placement Preparation Series | C++ | LeetCode 110 |
YouTube video '110. Balanced Binary Tree | Leetcode Daily - Python'
Leetcode Daily 6 mins
110. Balanced Binary Tree | Leetcode Daily - Python
YouTube video 'Leetcode | Easy | 110. Balanced Binary Tree | Recursion| Time complexity: O(n) | Javascript'
Software Interviews Prep 11 mins
Leetcode | Easy | 110. Balanced Binary Tree | Recursion| Time complexity: O(n) | Javascript
YouTube video 'Is a Binary Tree Balanced'
SDE Skills 29 mins
Is a Binary Tree Balanced
YouTube video 'Daily Leetcode #2140: Feb 08, 2026 - 110. Balanced Binary Tree'
Programming Live with Larry 6 mins
Daily Leetcode #2140: Feb 08, 2026 - 110. Balanced Binary Tree
YouTube video '48. Leetcode 110. Balanced Binary Tree : Javascript'
Web Fusion with Bharadwaj 18 mins
48. Leetcode 110. Balanced Binary Tree : Javascript
YouTube video 'Leetcode 110: Balanced Binary Tree in Telugu | Brute Force + Optimized | Python DSA | srkcodes'
Srk Codes 11 mins
Leetcode 110: Balanced Binary Tree in Telugu | Brute Force + Optimized | Python DSA | srkcodes
20 videos   did my crawler come up with invalid videos ? drop me a line.

Similar Problems

1 similar problem

Solved On The Same Day

27 solutions   August 31, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality