-
Notifications
You must be signed in to change notification settings - Fork 458
/
SymmetricTree.py
51 lines (42 loc) · 1.17 KB
/
SymmetricTree.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
class BinaryTree:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def treeinput():
data=int(input())
if data==-1:
return None
root=BinaryTree(data)
leftdata=treeinput()
rightdata=treeinput()
root.left=leftdata
root.right=rightdata
return root
def printTree(root):
if root==None:
return
print(root.data,end=":")
if root.left!=None:
print("L",root.left.data,end=",")
if root.right!=None:
print("R",root.right.data,end="")
print()
printTree(root.left)
printTree(root.right)
def isSymmetric(root):
if not root:
return True
def check(left,right):
if not left and not right:
return True
if not left or not right:
return False
return (left.data==right.data and check(left.left,right.right) and check(left.right,right.left))
return check(root.left,root.right)
root=treeinput()
printTree(root)
if isSymmetric(root):
print("THE TREE IS SYMMETRIC")
else:
print("THE TREE IS NOT SYMMETRIC")