AceYourInterviews
Checking sign-in…

80. Symmetric Tree

CorePair opposite positions across a mirror axis

Determine whether the left and right sides of the tree are mirror images around the root. Corresponding values must match when left and right directions are exchanged.

A symmetric tree is defined by mirrored positions, not by having the same values on both sides in arbitrary order.

Example 1

Input

[1,2,2,3,4,4,3]

Output

true

Explanation: Opposite positions match through both levels.

Example 2

Input

[1,2,2,null,3,null,3]

Output

false

Explanation: Both 3s lie on right edges of their parents; their null positions do not mirror.

Constraints

  • 0 <= node count per tree <= 105
  • -231 <= node.value <= 231 - 1
  • Each input is a proper binary tree: no cycles or shared child nodes. Null trees are permitted.
Hints
Hint 1

Which child of the right subtree mirrors the left child of the left subtree?

Hint 2

A missing position has to mirror another missing position.

Hint 3

Start with the root children and repeatedly compare crossed child pairs.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Outer and inner mirror pairs

[1,2,2,3,4,4,3]
true