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
Check if leaf traversal of two Binary Trees is same in Python
Suppose we have two binary trees. We need to check whether the leaf traversal of these two trees is the same or not. The leaf traversal is the sequence of leaf nodes traversed from left to right.
So, if the input is like ?
Then the output will be True as the leaf traversal sequence of both trees is the same: [5, 7, 8].
Algorithm
To solve this, we will follow these steps ?
- Use two stacks (s1, s2) to traverse both trees simultaneously
- Push the root nodes into their respective stacks
- While both stacks are not empty ?
- Pop nodes from each stack and find the next leaf node
- Compare the leaf values - if different, return False
- If one tree has more leaves than the other, return False
- Return True if all leaves match
Implementation
class TreeNode:
def __init__(self, x):
self.val = x
self.left = self.right = None
def is_leaf(self):
return self.left == None and self.right == None
def solve(r1, r2):
s1 = []
s2 = []
s1.append(r1)
s2.append(r2)
while len(s1) != 0 or len(s2) != 0:
if len(s1) == 0 or len(s2) == 0:
return False
# Find next leaf in tree 1
r1_node = s1.pop(-1)
while r1_node != None and not r1_node.is_leaf():
if r1_node.right != None:
s1.append(r1_node.right)
if r1_node.left != None:
s1.append(r1_node.left)
if len(s1) > 0:
r1_node = s1.pop(-1)
else:
r1_node = None
# Find next leaf in tree 2
r2_node = s2.pop(-1)
while r2_node != None and not r2_node.is_leaf():
if r2_node.right != None:
s2.append(r2_node.right)
if r2_node.left != None:
s2.append(r2_node.left)
if len(s2) > 0:
r2_node = s2.pop(-1)
else:
r2_node = None
# Compare leaf nodes
if r1_node == None and r2_node != None:
return False
if r1_node != None and r2_node == None:
return False
if r1_node != None and r2_node != None:
if r1_node.val != r2_node.val:
return False
return True
# Create first tree
root1 = TreeNode(2)
root1.left = TreeNode(3)
root1.right = TreeNode(4)
root1.left.left = TreeNode(5)
root1.right.left = TreeNode(7)
root1.right.right = TreeNode(8)
# Create second tree
root2 = TreeNode(1)
root2.left = TreeNode(6)
root2.right = TreeNode(9)
root2.left.right = TreeNode(5)
root2.right.left = TreeNode(7)
root2.right.right = TreeNode(8)
print(solve(root1, root2))
True
How It Works
The algorithm uses two stacks to perform depth-first traversal of both trees simultaneously. For each tree, it finds the next leaf node by:
- Popping a node from the stack
- If it's not a leaf, pushing its children (right first, then left) to maintain left-to-right order
- Continuing until a leaf is found
The leaf values are then compared, and if any mismatch is found, the function returns False.
Conclusion
This approach efficiently compares leaf traversals of two binary trees using stacks for iterative traversal. The algorithm ensures left-to-right leaf comparison without storing all leaves in memory, making it space-efficient for large trees.
