Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Input: p = [1,2,3], q = [1,2,3]
Output: true
Input: p = [1,2], q = [1,null,2]
Output: false
Input: p = [1,2,1], q = [1,1,2]
Output: false
[0, 100].The basic idea is to recursively compare the two trees' nodes. If both trees' nodes have the same value, then we recursively check their left children and their right children. If all corresponding nodes in the two trees are equal, then the trees are the same.
Instead of doing it recursively, we can use an iterative method to traverse both trees level by level using a queue. At each node, we check if both nodes are null or if one is null. If both nodes are non-null, we compare their values and enqueue their children.