Are Two Binary Trees Mirror Images
Updated
Problem statement
Given the roots of two binary trees, root1 and root2, return true if the second tree is the mirror image of the first, and false otherwise.
The mirror image of a tree is what you get by flipping it left to right: every node keeps its value, but its left and right subtrees trade places, all the way down. So two trees are mirror images when their roots have equal values, the left subtree of the first root is the mirror image of the right subtree of the second root, and the right subtree of the first root is the mirror image of the left subtree of the second root.
Function
areMirrors(root1: TreeNode, root2: TreeNode) → booleanExamples
Example 1
root1 = [7,3,9,null,4]root2 = [7,9,3,null,null,4]return = trueFlip the first tree left to right: 9 becomes the left child of 7 and 3 the right child, and 4, the right child of 3, becomes its left child. That is exactly the second tree, so the answer is true.
Example 2
root1 = [7,3,9,null,4]root2 = [7,9,3,null,null,null,4]return = falseThe top two levels match the flipped first tree, but 4 is the right child of 3 in the second tree. In the flipped first tree it must be the left child of 3, so the answer is false.
Example 3
root1 = [10,20]root2 = [10,20]return = falseThe two trees are the same, but they are not mirror images: flipping the first tree moves 20 to the right of 10, while in the second tree 20 is on the left. The answer is false.
Constraints
- Each tree has
1to1000nodes. -1000 <= Node.val <= 1000, and values may repeat
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Left View of a Binary TreeMygate · PHONE SCREENMygate interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Distance Between Two Nodes in a Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
- Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)