Check if a given Binary Tree is height balanced like a Red-Black Tree in Python

A Red-Black Tree is a balanced binary search tree where the height difference is controlled. In such trees, the longest path from any node to a leaf is at most twice the length of the shortest path. This property ensures the tree remains balanced and provides efficient operations.

We need to check if a given binary tree satisfies this height-balanced property where for every node, the longest leaf-to-node path is not more than double the shortest path from node to leaf.

Problem Understanding

Given a binary tree like this:

10 5 100 50 150

The output should be True if the tree satisfies the Red-Black Tree height property.

Algorithm

We'll use a recursive approach to calculate both minimum and maximum heights for each subtree:

  • For each node, calculate the minimum and maximum heights of left and right subtrees
  • Check if max_height ? 2 × min_height for the current node
  • If this condition fails for any node, the tree is not balanced

Implementation

class TreeNode:
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None

def solve(root):
    # Returns (is_balanced, min_height, max_height)
    if root is None:
        return True, 0, 0
    
    # Check left subtree
    left_balanced, left_min, left_max = solve(root.left)
    if not left_balanced:
        return False, 0, 0
    
    # Check right subtree  
    right_balanced, right_min, right_max = solve(root.right)
    if not right_balanced:
        return False, 0, 0
    
    # Calculate heights for current node
    min_height = min(left_min, right_min) + 1
    max_height = max(left_max, right_max) + 1
    
    # Check Red-Black Tree property
    if max_height <= 2 * min_height:
        return True, min_height, max_height
    else:
        return False, 0, 0

def is_tree_balanced(root):
    if root is None:
        return True
    
    balanced, _, _ = solve(root)
    return balanced

# Create test tree
root = TreeNode(10)
root.left = TreeNode(5)
root.right = TreeNode(100)
root.right.left = TreeNode(50)
root.right.right = TreeNode(150)
root.right.left.left = TreeNode(40)

print("Is tree balanced like Red-Black Tree?", is_tree_balanced(root))
Is tree balanced like Red-Black Tree? True

Example with Unbalanced Tree

Let's test with an unbalanced tree to see the difference:

# Create an unbalanced tree
unbalanced_root = TreeNode(1)
unbalanced_root.left = TreeNode(2)
unbalanced_root.left.left = TreeNode(3)
unbalanced_root.left.left.left = TreeNode(4)
unbalanced_root.left.left.left.left = TreeNode(5)

print("Is unbalanced tree balanced?", is_tree_balanced(unbalanced_root))

# The tree has min_height = 1 (right path) and max_height = 5 (left path)
# Since 5 > 2 * 1, it's not balanced
Is unbalanced tree balanced? False

How It Works

The algorithm works by:

  • Base case: Empty nodes return height 0 and are considered balanced
  • Recursive case: For each node, we calculate minimum and maximum heights of both subtrees
  • Height calculation: Current node's height = max/min of subtree heights + 1
  • Balance check: Verify that max_height ? 2 × min_height

Time and Space Complexity

  • Time Complexity: O(n) where n is the number of nodes
  • Space Complexity: O(h) where h is the height of the tree (recursion stack)

Conclusion

This solution efficiently checks if a binary tree satisfies the Red-Black Tree height-balanced property. The key insight is to calculate both minimum and maximum heights simultaneously and verify the 2:1 ratio constraint at each node.

Updated on: 2026-03-25T10:03:26+05:30

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements