-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathidentical_binary_tree.py
63 lines (43 loc) · 1.23 KB
/
identical_binary_tree.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class TreeNode():
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def __repr__(self):
return '<TreeNode {}>'.format(self.val)
def __eq__(self, other):
return eq_node(self, other)
def is_same_tree(tree_a, tree_b):
"Given two binary trees ensure they have the same nodes."
return compare_recursive(tree_a, tree_b)
def compare_recursive(a, b):
if not a and not b:
return True
if a and not b:
return False
if not a and b:
return False
if a.val != b.val:
return False
return compare_recursive(a.left, b.left) and compare_recursive(a.right, b.right)
def test_is_same_tree():
def create_tree():
n1 = TreeNode(1)
n2 = TreeNode(2)
n3 = TreeNode(3)
n1.left = n2
n1.right = n3
return n1
assert is_same_tree(create_tree(), create_tree())
def test_is_not_same_tree():
def create_tree_1():
n1 = TreeNode(1)
n3 = TreeNode(3)
n1.right = n3
return n1
def create_tree_2():
n1 = TreeNode(1)
n2 = TreeNode(2)
n1.left = n2
return n1
assert not is_same_tree(create_tree_1(), create_tree_2())