100. Same Tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
Solution:
class Solution(object):
def isSameTree(self, p, q):
"""
:type p: TreeNode
:type q: TreeNode
:rtype: bool
"""
if not p and not q:
return True
if not (p and q) or p.val != q.val:
return False
same_left = self.isSameTree(p.left, q.left)
same_right = self.isSameTree(p.right, q.right)
return same_left and same_right