binary-tree
depth-first-search
recursion

Given the root of a binary tree, return true if it is a mirror of itself (symmetric around its center), or false otherwise.

Input / output

  • Input: root: TreeNode (JSON test fixture is a LeetCode-style level-order array, e.g. [1, 2, 2, 3, 4, 4, 3], with null for a missing child)
  • Output: boolean

Constraints

  • 0 <= number of nodes <= 1,000
  • -100 <= node value <= 100

Follow-up

Can you solve it both recursively and iteratively with a queue?

Examples

Example 1

Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2

Input: root = [1,2,2,null,3,null,3]
Output: false

Example 3 (empty tree)

Input: root = []
Output: true
🔒 6 hidden

Running will execute all 9 cases, including 6 hidden ones.