Find largest subtree having identical left and right subtrees in Python

Finding the largest subtree with identical left and right subtrees involves comparing the structure and values of subtrees. We'll use a recursive approach with tree encoding to efficiently identify matching subtrees.

55 15 70 10 25 75 75 65 80 65 80 Red boxes: Identical subtrees (size 3 each)

Algorithm Approach

The solution uses tree serialization to encode subtree structures. When left and right subtrees have identical encodings, we've found a matching pair ?

  • Recursively traverse the tree and encode each subtree

  • Compare left and right subtree encodings at each node

  • Track the largest subtree where left and right subtrees match

  • Return the size and root of the largest matching subtree

Implementation

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

def solve(root, encode, maxSize, maxNode):
    if root == None:
        return 0
    
    left_list = [""]
    right_list = [""]
    
    ls = solve(root.left, left_list, maxSize, maxNode)
    rs = solve(root.right, right_list, maxSize, maxNode)
    
    size = ls + rs + 1
    
    # Check if left and right subtrees are identical
    if left_list[0] == right_list[0]:
        if size > maxSize[0]:
            maxSize[0] = size
            maxNode[0] = root
    
    # Encode current subtree
    encode[0] = "|" + left_list[0] + "|" + str(root.data) + "|" + right_list[0] + "|"
    
    return size

def largestSubtree(node, maxNode):
    maximum = [0]
    encode = [""]
    solve(node, encode, maximum, maxNode)
    return maximum

# Create the binary tree
root = TreeNode(55)
root.left = TreeNode(15)
root.right = TreeNode(70)
root.left.left = TreeNode(10)
root.left.right = TreeNode(25)
root.right.left = TreeNode(75)
root.right.left.left = TreeNode(65)
root.right.left.right = TreeNode(80)
root.right.right = TreeNode(75)
root.right.right.left = TreeNode(65)
root.right.right.right = TreeNode(80)

maxNode = [None]
maximum = largestSubtree(root, maxNode)

print("Root of largest subtree:", maxNode[0].data)
print("Size:", maximum[0])
Root of largest subtree: 70
Size: 7

How It Works

The algorithm works by creating a unique string encoding for each subtree ?

  1. Encoding: Each subtree gets encoded as "|left_encoding|node_value|right_encoding|"

  2. Comparison: At each node, compare left and right subtree encodings

  3. Tracking: When encodings match, check if current subtree size is maximum

  4. Result: Return the root and size of the largest matching subtree

Time Complexity

Operation Complexity Explanation
Tree Traversal O(n) Visit each node once
String Encoding O(n) Create encoding for each subtree
Overall O(n) Linear time complexity

Conclusion

This solution efficiently finds the largest subtree with identical left and right subtrees using tree serialization. The encoding technique ensures accurate comparison while maintaining O(n) time complexity.

Updated on: 2026-03-25T09:42:36+05:30

177 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements