From 2cedd6eb81c02f1066cbc2de5f8644f7236a9e9c Mon Sep 17 00:00:00 2001 From: Dibyangana Das <130747608+tupur-29@users.noreply.github.com> Date: Sun, 6 Oct 2024 16:18:43 +0530 Subject: [PATCH] Create SymmetricTree.py --- DS&Algo Programs in Python/SymmetricTree.py | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 DS&Algo Programs in Python/SymmetricTree.py diff --git a/DS&Algo Programs in Python/SymmetricTree.py b/DS&Algo Programs in Python/SymmetricTree.py new file mode 100644 index 00000000..58302c59 --- /dev/null +++ b/DS&Algo Programs in Python/SymmetricTree.py @@ -0,0 +1,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") \ No newline at end of file