Symmetric Tree
1
/ \
2 2
/ \ / \
3 4 4 3 1
/ \
2 2
\ \
3 3# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
def symmetric(n1, n2):
if not n1 and not n2:
return True
if (not n1 and n2) or (not n2 and n1):
return False
return n1.val == n2.val and symmetric(n1.left, n2.right) and symmetric(n1.right, n2.left)
return symmetric(root, root)Last updated