Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to check two trees are exactly same based on their structure and values in Python
Suppose we have two binary trees, we have to check whether they are exactly same in terms of their structures and values or not. We can say them as twin trees.
So, if the input is like ?
Then the output will be True for the first pair and False for trees with different values or structures.
Algorithm
To solve this, we will follow these steps ?
Define a method
solve(), this will take two tree rootsIf both roots are null, return
True(both empty trees are identical)If one root is null and other is not, return
FalseIf values of both roots are different, return
FalseRecursively check left subtrees and right subtrees, return
Trueonly if both are identical
Implementation
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def solve(self, root0, root1):
# Both trees are empty
if not root0 and not root1:
return True
# One tree is empty, other is not
if not root0 or not root1:
return False
# Values are different
if root0.val != root1.val:
return False
# Recursively check left and right subtrees
return (self.solve(root0.left, root1.left) and
self.solve(root0.right, root1.right))
# Test with identical trees
ob = Solution()
# Create first tree: 10 -> (5, 15) -> (3, 8), (None, None)
root1 = TreeNode(10)
root1.left = TreeNode(5)
root1.right = TreeNode(15)
root1.left.left = TreeNode(3)
root1.left.right = TreeNode(8)
# Create identical second tree
root2 = TreeNode(10)
root2.left = TreeNode(5)
root2.right = TreeNode(15)
root2.left.left = TreeNode(3)
root2.left.right = TreeNode(8)
print("Identical trees:", ob.solve(root1, root2))
Identical trees: True
Testing Different Cases
# Test with different values
root3 = TreeNode(10)
root3.left = TreeNode(3) # Different value: 3 instead of 5
root3.right = TreeNode(15)
print("Different values:", ob.solve(root1, root3))
# Test with different structure
root4 = TreeNode(10)
root4.left = TreeNode(5)
# Missing right subtree
print("Different structure:", ob.solve(root1, root4))
# Test with empty trees
print("Both empty:", ob.solve(None, None))
print("One empty:", ob.solve(root1, None))
Different values: False Different structure: False Both empty: True One empty: False
How It Works
The algorithm uses recursive depth-first traversal to compare trees:
Base cases: Handle null nodes efficiently
Value comparison: Check if current nodes have same values
Recursive calls: Check left and right subtrees independently
Short-circuit evaluation: Returns
Falseimmediately when difference is found
Time Complexity
Time: O(n) where n is the number of nodes in the smaller tree. Space: O(h) where h is the height of the tree due to recursion stack.
Conclusion
This recursive solution efficiently compares two binary trees by checking both structure and node values. The algorithm handles all edge cases including empty trees and uses short-circuit evaluation for optimal performance.
