cheetcode

50. Same TreeEasy

Determine whether two binary trees are identical.

Given the roots of two binary trees p and q, write a function to check if they are the same.

Two binary trees are considered the same if they are structurally identical and the nodes have the same values.

Reason about the value returned from each subtree and the state passed down from ancestors. Empty children and single-node trees should be handled naturally.

Examples

Input: p = [1,2,3], q = [1,2,3]
Output: true
Input: p = [1,2], q = [1,null,2]
Output: false

Constraints

  • The number of nodes in both trees is in the range [0, 100].
  • -10,000 <= Node.val <= 10,000