same-tree.sh — zsh
binary-treedepth-first-searchrecursion

Return true when binary trees p and q have the same structure and equal values.

Constraints

  • 0 <= total nodes <= 2000
  • -10000 <= node value <= 10000

Follow-up

How would an iterative queue-based comparison work?

Examples
Example 1
Input: p = [1,2,3], q = [1,2,3]
Output: true
Example 2
Input: p = [1,2], q = [1,null,2]
Output: false
Example 3
Input: p = [1,2,1], q = [1,1,2]
Output: false
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.