Is Symmetric

Leetcode link:


Given the root of a tree, check if it is symmetric around the center.


To evaluate if a tree is symmetric around its center, we first observe that the left and right sub-tree has to be equal. Based on this we can write a recursive implementation:

def isSymmetric(self, root: TreeNode) -> bool:
def is_mirror(node1, node2):
if(node1 is None and node2 is None):
return True
elif(node1 is None or node2 is None):
return False
return node1.val == node2.val and is_mirror(node1.left, node2.right) and is_mirror(node1.right, node2.left)
return is_mirror(root, root)