Ctrl K

Mirror-Image Tree

Easy
You are given the root of a binary tree. Check whether the tree looks the same as its reflection across a vertical line through the root. In other words, the root's left subtree must be a mirror copy of its right subtree: mirrored positions hold nodes with equal values, and wherever one side has no node, the other side has none either. Return True if the tree is symmetric and False otherwise. Try solving it both recursively and iteratively.
Example 1
root=tree_node([7, 5, 5, None, 8, 8]) → True

The left 5 has only a right child 8 and the right 5 has only a left child 8. They match in the mirror.

Example 2
root=tree_node([7, 5, 5, 8, None, 8]) → False

Both 8s are left children, so in the mirror one of them lands opposite an empty spot.

Example 3
root=tree_node([4]) → True

A single node is symmetric.

Constraints
  • 1 ≤ number of nodes ≤ 1000
  • -100 ≤ Node.val ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIdentical Trees EasyTree Height EasyNearest Leaf Depth EasyLeaf Path With a Given Total EasyMirror the Tree Easy
loading Python…
Input
root=tree_node([7, 5, 5, None, 8, 8])
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 114 tests on the server, hidden ones included.