Find the largest Perfect Subtree in a given Binary Tree in Python

A Perfect Binary Tree is a binary tree where all internal nodes have exactly two children and all leaves are at the same level. This article explains how to find the size of the largest perfect subtree within a given binary tree using Python.

2 3 4 5 6 7 Perfect Subtree (Size: 3)

Algorithm Approach

The solution uses a recursive approach with a helper class to track three properties for each subtree:

  • isPerfect: Whether the subtree is perfect
  • height: Height of the subtree
  • rootTree: Root node of the largest perfect subtree found

Implementation

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

class RetType:
    def __init__(self):
        self.isPerfect = False
        self.height = 0
        self.rootTree = None

def get_perfect_subtree(root):
    r_type = RetType()
    
    # Base case: empty node
    if root is None:
        r_type.isPerfect = True
        r_type.height = 0
        r_type.rootTree = None
        return r_type
    
    # Recursively check left and right subtrees
    left_subtree = get_perfect_subtree(root.left)
    right_subtree = get_perfect_subtree(root.right)
    
    # Check if current subtree is perfect
    if (left_subtree.isPerfect and right_subtree.isPerfect and 
        left_subtree.height == right_subtree.height):
        r_type.height = left_subtree.height + 1
        r_type.isPerfect = True
        r_type.rootTree = root
        return r_type
    
    # Current subtree is not perfect
    r_type.isPerfect = False
    r_type.height = max(left_subtree.height, right_subtree.height)
    
    # Choose the subtree with larger height
    if left_subtree.height > right_subtree.height:
        r_type.rootTree = left_subtree.rootTree
    else:
        r_type.rootTree = right_subtree.rootTree
    
    return r_type

def print_tree(root):
    if root is not None:
        print_tree(root.left)
        print(root.data, end=', ')
        print_tree(root.right)

# Create the binary tree
root = TreeNode(2)
root.left = TreeNode(3)
root.right = TreeNode(4)
root.left.left = TreeNode(5)
root.left.right = TreeNode(6)
root.right.left = TreeNode(7)

# Find largest perfect subtree
result = get_perfect_subtree(root)
size = pow(2, result.height) - 1

print("Size:", size)
print("Tree:", end=" ")
print_tree(result.rootTree)
Size: 3
Tree: 5, 3, 6, 

How It Works

The algorithm works by:

  1. Base Case: Empty nodes are considered perfect with height 0
  2. Recursive Check: For each node, check if both left and right subtrees are perfect
  3. Perfect Condition: A subtree is perfect if both children are perfect and have equal heights
  4. Size Calculation: Perfect binary tree size = 2^height - 1

Time Complexity

The time complexity is O(n) where n is the number of nodes, as each node is visited once. The space complexity is O(h) where h is the height of the tree due to recursion stack.

Conclusion

This algorithm efficiently finds the largest perfect subtree by using bottom-up recursion. The key insight is checking if both subtrees are perfect with equal heights to determine if the current subtree is perfect.

Updated on: 2026-03-25T09:28:23+05:30

856 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements