写一个函数来解决下列编程问题:给定两个二进制树的根 `p`和 `q`,写一个函数来检查它们是否相同.如果它们结构相同,并且节点具有相同的值,则两个二进制树被认为相同.
Here is a Python function that can solve the problem:

```
def is_same_tree(p, q):
    if not p and not q:
        return True
    if not p or not q:
        return False
    if p.val != q.val:
        return False
    return is_same_tree(p.left, q.left) and is_same_tree(p.right, q.right)
```

In this function, we check the base cases where both trees are empty or one of them is empty. Then we check if both roots have the same value. Finally, we recursively check if both left and right subtrees are the same. If all conditions are met, we return True, otherwise we return False.