smj007

Untitled

Jun 19th, 2022
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. class Solution:
  2.     def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
  3.        
  4.         # base case: if both are null nodes
  5.         if not p and not q:
  6.             return True
  7.  
  8.         # if both happen to be actual nodes
  9.         if p and q:
  10.             return (p.val == q.val) and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
  11.  
  12.         # if either of them is not a null node
  13.         return False
Advertisement
Add Comment
Please, Sign In to add comment