Symmetric tree
It's still the weekend so taking it easy. Some binary tree questions.
from typing import Optional
# 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: Optional[TreeNode]) -> bool:
return self.is_mirror(root.left, root.right)
def is_mirror(self, p, q):
if p is None and q is None:
return True
elif p is not None and q is None:
return False
elif p is None and q is not None:
return False
# elif p is not None and q is not None:
if p.val != q.val:
return False
return self.is_mirror(p.right, q.left) and self.is_mirror(p.left, q.right)
from typing import Optional
# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if root == None:
return None
placeholder = root.left
root.left = self.invertTree(root.right)
root.right = self.invertTree(placeholder)
return root