Skip to the code
cf.
+10 XP

#318 Identical Layout Snapshots

easy

Before saving a workspace, your editor compares the current pane layout with the last saved snapshot. Both are binary trees of pane ids.

Write sameLayout(a, b) that returns true when the two trees have exactly the same shape and the same id in every matching position, and false otherwise. Two empty trees are the same.

Example 1

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

Same shape, same ids.

Example 2

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

In a, 2 is a left child; in b, it is a right child.

Constraints

  • 0 <= number of nodes in each tree <= 10000

Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).