-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBT2.py
More file actions
87 lines (68 loc) · 2.22 KB
/
Copy pathIBT2.py
File metadata and controls
87 lines (68 loc) · 2.22 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# A binary tree node
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def __repr__(self):
return str(self.data)
def is_node_equal(node1, node2):
return node1 and node2 and node1.data == node2.data
def isIdentical(root1, root2):
# If both tree is null return true
if not root1 and not root2:
return True
# If only one of them is null
elif (root1 and not root2) or (not root1 and root2):
return False
# create a stack to hold Node pairs
arr = [[root1,root2]]
# do till stack is not empty
while arr.__len__() > 0:
# pop top pair from the stack and process it
node_array = arr.pop(0)
node1 = node_array.pop()
node2 = node_array.pop()
# if value of their root node don't match, return false
if not is_node_equal(node1, node2):
return False
# Push left node pair if that exists
if node1.left and node2.left:
arr.append([node1.left, node2.left])
# If only one of them exists
elif node1.left or node2.left:
return False
# Push right node pair if that exists
if node1.right and node2.right:
arr.append([node1.right, node2.right])
# If only one of them exists
elif node1.right or node2.right:
return False
#if we reach here, both binary trees are identical
return True
if __name__ == '__main__':
root1 = Node(1)
root1.left = Node(2)
root1.right = Node(3)
root1.left.left = Node(4)
root1.left.right = Node(5)
root1.left.right.left = Node(8)
root1.left.right.left.left = Node(9)
root1.left.right.left.left.left = Node(10)
root1.right.right = Node(7)
root2 = Node(1)
root2.left = Node(2)
root2.right = Node(3)
root2.left.left = Node(4)
root2.left.right = Node(5)
root2.left.right.left = Node(8)
root2.left.right.left.left = Node(9)
root2.left.right.left.left.left = Node(10)
root2.right.right = Node(7)
print("root1 and root2 are Identical " if isIdentical(root1, root2)
else "root1 and root2 are not Identical ")
root1.right.right.right = Node(11)
print("root1 and root2 are Identical " if isIdentical(root1, root2)
else "root1 and root2 are not Identical ")
# This code is contributed by Sumit Bhardwaj