# 08.02.2026 [110. Balanced Binary Tree]
Is tree balanced? #easy #dfs
08.02.2026
Join me on Telegram
Problem TLDR
Is tree balanced? #easy #dfs
Intuition
Solve the sub-problem for every node. Compare max depths for the left and right.
Approach
we can use ‘marker’ depth as a boolean
we can shortcircuit and don’t check the right subtree
we can override values in a tree to golf the solutino
BFS will not solve this: leafs can be at any heights, only max depths left&right for each node matters
Complexity
Time complexity: $$O(n)$$
Space complexity: $$O(log(n))$$
Code
// 0ms
fun isBalanced(r: TreeNode?): Boolean = r?.run {
val b = isBalanced(left) && isBalanced(right)
val l = left?.`val`?:0; val r = right?.`val`?:0
`val`= 1 + max(l,r); b && abs(l-r) < 2
} ?: true
// 0ms
pub fn is_balanced(r: Option<Rc<RefCell<TreeNode>>>) -> bool {
fn d(r: &Option<Rc<RefCell<TreeNode>>>) -> Result<i8, ()> {
let Some(n) = r else { return Ok(0) }; let n = n.borrow();
let l = d(&n.left)?; let r = d(&n.right)?;
if (l-r).abs() > 1 { Err(()) } else { Ok(1 + l.max(r)) }
}
d(&r).is_ok()
}


