Gamified challenge · Recursion
The garden is a binary tree of bushes, given as tree (a TreeNode root; examples are written in level order with null for a missing bush). It is symmetric if its left half is the mirror image of its right half: same shape flipped left to right, and every bush has the same number as its mirrored partner.
Write isMirror(tree) (Python: is_mirror(tree)) that returns true if the garden is symmetric about the root, false otherwise. An empty garden and a single bush are symmetric.
[1, 2, 2, 3, 4, 4, 3] is symmetric. [1, 2, 2, null, 3, null, 3] is not: both 3s hang on the right of their parents, so the shapes don't mirror.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google