1. Code

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q:
            return True

        # if only one node has a child or children
        if not p or not q:
            return False
        if p.val != q.val:
            return False

        # keep compare the left and right side of each node
        return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

2. Result

        Runtime : 24 ms(94.54%), Memory usage : 14.1 MB(50.13%)
        (Runtime can be different by a system even if it is a same code.)

Check out the my GitHub repo for more info on the code. If you have questions, you can leave a reply on this post.