Skip to content
Decoder

Same Tree

Easy

Problem

Given the roots of two binary trees p and q, return True if they are identical: same shape, and the same value in every matching position. Otherwise return False.

Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.

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
Why
2 is on different sides

Example 3

Input
p = [1, 2, 1], q = [1, 1, 2]
Output
False

Constraints

  • 0 <= number of nodes in each tree <= 100
  • -10^4 <= Node.val <= 10^4

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.