#110
Balanced Binary Tree
easy· Treesruns: 0Given a binary tree, determine if it is height-balanced. A height-balanced tree is one in which the depth of the two subtrees of every node differs by at most 1.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 452
class Solution:
def isBalanced(self, root: TreeNode | None) -> bool:
def check(node: TreeNode | None) -> int:
if not node:
return 0
left = check(node.left)
if left == -1:
return -1
right = check(node.right)
if right == -1 or abs(left - right) > 1:
return -1
return 1 + max(left, right)
return check(root) != -1
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.