- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMirrorImageTree.py
More file actions
Latest commit
51 lines (44 loc) · 1014 Bytes
/
Copy pathMirrorImageTree.py
File metadata and controls
51 lines (44 loc) · 1014 Bytes
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
classNode(object):
def__init__(self,data=None,left=None,right=None):
self.data=data
self.left=left
self.right=right
defMirror(node):
'''
Build a mirror image for a binary tree
Args: start node in the binary tree
'''
ifnodeisNone:
return
else:
newNode=Node(node.data)
newNode.left=Mirror(node.right)
newNode.right=Mirror(node.left)
returnnewNode
defbfsPrint(root):
queue= []
queue.append(root)
node=root
whilenodeandlen(queue) >0 :
node=queue.pop(0)
printnode.data
ifnode.leftisnotNone:
queue.append(node.left)
ifnode.rightisnotNone:
queue.append(node.right)
defMirrorTrees(root1,root2):
'''
Check whether two trees are mirror
Args: two root node for the binary trees
Return : True or False if the two trees are mirror or not
'''
1
23
45
'''
a = Node(1,Node(2,Node(4),Node(5)),Node(3))
print 'a'
bfsPrint(a)
mirror = Mirror(a)
print 'mirror'
bfsPrint(mirror)