Created
October 6, 2019 00:14
-
-
Save sheldonrobinson/2999bfcccde98ad5e4ed02a6060645b2 to your computer and use it in GitHub Desktop.
CodeSignal Solution isTreeSymmetric
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Binary trees are already defined with this interface: | |
# class Tree(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.left = None | |
# self.right = None | |
def isEqual(left, right): | |
if left == None and right == None: | |
return True | |
if right == None and left != None: | |
return False | |
if left == None and right != None: | |
return False | |
if left.value != right.value: | |
return False | |
return isEqual(left.right, right.left) and isEqual(left.left, right.right) | |
def isTreeSymmetric(t): | |
if t == None: | |
return True | |
return isEqual(t.left, t.right) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment