← DiffPush

Same Tree

Standard Bar Binary Trees · Medium Problems O(n) · O(h)

The Twin Chandelier Inspection

A glasswork atelier hand-builds chandeliers as binary branch trees, and a wealthy client ordered two supposedly identical pieces. Before shipping, the quality clerk hangs both on rigging and compares them joint by joint: every joint must hold the same crystal grade, and every branch must fork the same way. A single mismatched fork or swapped child disqualifies the pair.

Input: Two binary trees p and q, each given as a level-order array where null marks a missing child.

Output: Return true if the trees are structurally identical with matching values at every position, false otherwise.

Constraints

Examples

Example 1

Input: {"p":[1,2,3],"q":[1,2,3]}
Output: true
Same shape, same values at every joint — identical pieces.

Example 2

Input: {"p":[1,2],"q":[1,null,2]}
Output: false
The branch hangs left in p but right in q — a structural mismatch.

Solve this in your browser →

Also on LeetCode ↗