-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0100 [E] Same Tree
Code with Senpai edited this page Feb 3, 2022
·
1 revision
# 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
# p and q are both None
if not p and not q:
return True
# one of p and q is None
if not q or not p:
return False
if p.val != q.val:
return False
L = self.isSameTree(p.left, q.left)
R = self.isSameTree(p.right, q.right)
return L and R
footer