Hi, here’s your problem today. This problem was recently asked by Apple:
Given 2 binary trees t and s, find if s has an equal subtree in t, where the structure and the values are the same. Return True if it exists, otherwise return False.
Here’s some starter code and an example:
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __repr__(self):
return f"(Value: {self.value} Left: {self.left} Right: {self.right})"
def find_subtree(s, t):
# Fill this in.
t3 = Node(4, Node(3), Node(2))
t2 = Node(5, Node(4), Node(-1))
t = Node(1, t2, t3)
s = Node(4, Node(3), Node(2))
"""
Tree t:
1
/ \
4 5
/ \ / \
3 2 4 -1
Tree s:
4
/ \
3 2
"""
print(find_subtree(s, t))
# True