finish 101
This commit is contained in:
parent
6866cf0b82
commit
17b4ef8970
36
leetcode/101.py
Normal file
36
leetcode/101.py
Normal file
@ -0,0 +1,36 @@
|
||||
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:
|
||||
if root is None:
|
||||
return True
|
||||
return self.isMirrorTree(root.left, root.right)
|
||||
|
||||
def isMirrorTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
|
||||
if p is None or q is None:
|
||||
if p is None and q is None:
|
||||
return True
|
||||
return False
|
||||
if p.val != q.val:
|
||||
return False
|
||||
if p.left and q.right:
|
||||
if not self.isMirrorTree(p.left, q.right):
|
||||
return False
|
||||
elif p.left or q.right:
|
||||
return False
|
||||
|
||||
if p.right and q.left:
|
||||
if not self.isMirrorTree(p.right, q.left):
|
||||
return False
|
||||
elif p.right or q.left:
|
||||
return False
|
||||
return True
|
Loading…
x
Reference in New Issue
Block a user