Symmetric Tree
Updated
Problem statement
You get root, the top node of a binary tree. Decide whether this tree is symmetric: return true for a symmetric tree and false for any other tree.
Symmetric means the tree equals its own reflection. Picture a vertical line through the root and swap everything on one side of it with everything on the other, so a node that was a left child turns into a right child, and the other way round. The tree passes when its reflection matches it exactly, in shape and in every value.
The examples write the tree as a level-order list: the root first, then each level from left to right, with null where a child is missing.
Function
isSymmetric(root: TreeNode) → booleanExamples
Example 1
root = [5,8,8,2,6,6,2]return = trueBoth children of the root hold 8. Under the left 8 sit 2 (outer) and 6 (inner); under the right 8 sit 6 (inner) and 2 (outer). Read across, the bottom row is 2, 6, 6, 2, the same backwards, so every node matches its reflection and we return true.
Example 2
root = [4,7,7,1,9,1,9]return = falseEach 7 holds 1 and then 9, reading left to right. The reflection of the left 7 holds 9 and then 1, which does not match the right 7. The bottom row 1, 9, 1, 9 is not the same backwards, so we return false.
Example 3
root = [5,8,8,null,6,null,6]return = falseEach 8 holds a single child 6, and each 6 hangs to the right of its parent. Reflected, the 6 below the right-hand 8 would move to the left of that 8. The values line up but the outlines differ, so we return false.
Example 4
root = [0,-5,-5,null,3,3]return = trueThe left -5 has just one child, 3, hanging to its right; the right -5 has just one child, 3, hanging to its left. Both 3 nodes sit next to the middle line, one on each side, so the reflection is identical and we return true.
Constraints
- The number of nodes in the tree is between
1and1000. -100 <= Node.val <= 100
More Zillow Onsite Interview questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- First Subarray with the Largest SumOA · ONSITE INTERVIEW · Seen Aug 2021
- Count Duplicate CharactersONSITE INTERVIEW · Seen Oct 2020
- Decision Tree Classifier from ScratchONSITE INTERVIEW · Seen Nov 2019
- Uniform to Normal NumbersONSITE INTERVIEW · Seen Nov 2019
- Check Each Word in a Character GridONSITE INTERVIEW · Seen Oct 2018
- Find Median from Data StreamPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2018
- Is SubstringONSITE INTERVIEW · Seen Nov 2017
Similar questions at other companies
- Diameter of Binary TreeeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
- Tree Level with the Most NodesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Mirror Image Binary TreesExpedia · ONSITE INTERVIEWExpedia interview questions
- Mirror a Binary TreeAMD · ONSITE INTERVIEWAMD interview questions
- Find a Node’s Level in an N-ary TreeMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Symmetric Binary TreeCapital One · ONSITE INTERVIEWCapital One interview questions
- Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions