Balanced Binary Tree
To solve this coding challenge, we need to determine if a given binary tree is height-balanced. A binary tree is considered height-balanced if and only if the heights of the two child subtrees of any node differ by no more than one. Also, both subtrees themselves need to be balanced. We'll utilize a depth-first search (DFS) approach, where for each node we recursively check the height of the left and right subtrees, and evaluate if the current node is balanced based on these heights.
Explanation
In a height-balanced binary tree:- The difference between the heights of the left and right subtrees for every node is at most 1.
- Both the left subtree and the right subtree must also be balanced. To achieve this, we will use a helper function called
- Return the height of the tree if it is balanced.
- Return -1 if the tree (or subtree) is not balanced. We start by checking the root of the binary tree and recursively assess the left and right subtrees. During this process, we:
- Check the height of the left and right subtrees.
- If the height difference between the left and right subtrees of any node is more than 1, we immediately conclude that the tree is not balanced.
- If the subtree itself is not balanced (indicated by a height of -1), we propagate this information back up the call stack.
-
Define the main function
that takes
is_balancedas a parameter.root -
Define a helper function
inside
check_height. This function should takeis_balancedas a parameter and return its height if balanced, otherwise return -1.node -
Inside
:
check_height -
Check if the
is
node. If so, return height 0.None -
Recursively call
on the
check_heightchild of the node and store the result inleft.left_height -
If
is -1, return -1 to indicate imbalance.
left_height -
Recursively call
on the
check_heightchild of the node and store the result inright.right_height -
If
is -1, return -1 to indicate imbalance.
right_height -
Check if the absolute difference between
and
left_heightis greater than 1.right_height - If so, return -1 to indicate the current node is imbalanced.
-
Otherwise, return the maximum of
and
left_heightplus 1 to account for the current node.right_height -
In
, call
is_balancedwith thecheck_heightnode and check if the result is not equal to -1.root - Return the result of the above condition. If true, the tree is balanced; otherwise, it is not.
check_height
# Pseudocode with comments
# Function to determine if the binary tree is balanced
function is_balanced(root):
# Helper function to check the height of a node
function check_height(node):
# Base case: if node is None, it's height is 0
if node is None:
return 0
# Recursively check the height of the left subtree
left_height = check_height(node.left)
# If left subtree is not balanced, propagate the -1 result
if left_height == -1:
return -1
# Recursively check the height of the right subtree
right_height = check_height(node.right)
# If right subtree is not balanced, propagate the -1 result
if right_height == -1:
return -1
# If the current node is imbalanced
if abs(left_height - right_height) > 1:
return -1
# Return the height of the current node which is
# max height of the left and right subtrees plus 1 for the current node
return max(left_height, right_height) + 1
# Call the helper function on the root and check if it returns -1
return check_height(root) != -1