To determine if a binary tree is height-balanced, you need to ensure that for every node in the tree, the height of the two subtrees of that node differ by no more than one. A height-balanced tree is often referred to as a balanced binary tree.
Here's a step-by-step approach to solve this problem:
-
Define Tree Height: Understand that the height of a node in a binary tree is the number of edges on the longest path from the node to a leaf. A leaf node will have a height of 0.
-
Recursive Height Calculation: Use a recursive function to calculate the height of each node. This function should return the height of a node as an integer.
-
Balance Check at Each Node: At each node, you need to check the balance condition: the absolute difference between the heights of the left subtree and the right subtree should be no more than one.
-
Propagate Imbalance: If any subtree is unbalanced, propagate this information up the tree. This can be done by using a special value (like -1) to indicate that a subtree is unbalanced.
-
Final Decision: After traversing all nodes, if you never encounter an unbalanced subtree, then the tree is height-balanced
Time complexity:
Space complexity:
The submitted code beat 99-percent of all Python submissions' runtime.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def height(node) -> int:
# Base Case: an empty subtree is balanced with height 0
if not node:
return 0
# Recursively get the height of the left and right subtrees
left_height = height(node.left)
right_height = height(node.right)
# If any subtree is unbalanced, propagate the unbalance up
if left_height == -1 or right_height == -1 or abs(left_height - right_height) > 1:
return -1
# Otherwise, return the height of the node
return max(left_height, right_height) + 1
return height(root) != -1