Same Tree

Trees: DFS & BFS, problem 3 of 8

Same Tree

Easy

LC #100

dfsrecursion

Not attempted yet

Given the roots p and q of two binary trees, return True if they are identical (same shape, and every pair of matching nodes holds the same value), otherwise False.

Example 1

Input: p = [1, 2, 3], q = [1, 2, 3]
Output: True

Example 2

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

Same values, different shape.

Example 3

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

Constraints

  • 0 ≤ nodes in each tree ≤ 2 · 10^4
  • -10^5 ≤ Node.val ≤ 10^5

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.