Skip to interview prep
Apexed

25. Same Tree

EasyTrees

Return true if two binary trees have the same shape and the same value at every node.

A binary tree is passed as TreeNode objects with val, left and right. The tests write trees in level order, with null for a missing child.

Example 1

Input: a = [1, 2, 3], b = [1, 2, 3]
Output: true

Example 2

Input: a = [1, 2], b = [1, null, 2]
Output: false

Constraints

  • 0 ≤ nodes in each tree ≤ 100

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 5 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Same Tree — coding interview practice — Apexed