1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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:
```python
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
```
|