Show problems as
Classic problem #101EasyTreeDepth-First SearchBreadth-First SearchBinary Tree

Symmetric Tree

Real-world version: Chime Hangs True
Tracked in this browser only
Read Symmetric Tree on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstMirror & Symmetry · Binary Trees & BST, animated step by step

A workshop assembles wind chimes. Everything hangs from one top hook, and each hook or arm may carry up to two arms below it, one on its left side and one on its right side. Every arm has a weight stamped on it.

Because the harness passes plain JSON, the chime arrives as the array joints, listed row by row from the top hook downwards. The first entry is the top hook's weight. After that, entries come in pairs: the next two entries give the left-side arm and the right-side arm of the next piece in that same row order. A side with nothing hanging on it is written null, and a null contributes no pair of its own.

A chime is called true when it is its own reflection: swap left for right everywhere below the top hook and you get the identical assembly, with matching weights and matching empty sides.

Return true if the chime is true, and false if it is not.

Examples

Example 1

Input
joints = [4, 7, 7, 2, 9, 9, 2]
Output
true

The two arms under the hook both weigh 7, and their pairs 2 then 9 on one side face 9 then 2 on the other, so every piece meets its reflection.

Example 2

Input
joints = [8, 3, 3, 7]
Output
false

The left arm carries a 7 on its own left side while the right arm carries nothing at all, so the reflection breaks one row below the hook.

Example 3

Input
joints = [6, 2, 2, 5, null, null, 5]
Output
true

The 5 hangs on the outer side of each 2, which is exactly where a reflection puts it.

Constraints

  • The number of arms in the chime is in the range [1, 1000].
  • -100 <= joints[i] <= 100
  • Entries that are not `null` are integers; `null` marks an empty side.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def chime_hangs_true(joints: list) -> bool:
Java
public boolean chimeHangsTrue(Integer[] joints)