Mirror Image Binary Trees
Updated
Problem statement
You are given the roots of two binary trees, first and second. Return true if the two trees are mirror images of each other, and false otherwise.
Think of holding first up to a mirror. You get its mirror image by swapping the left and right child of every node. The two trees are mirror images when that mirror image is exactly second: the same shape and the same value in every matching place.
Put another way:
- Two empty trees are mirror images.
- An empty tree and a non-empty tree are not.
- Two non-empty trees are mirror images when their roots have the same value, the left subtree of
firstmirrors the right subtree ofsecond, and the right subtree offirstmirrors the left subtree ofsecond.
Function
areMirrors(first: TreeNode, second: TreeNode) → booleanExamples
Example 1
first = [1,2,3,4,5]second = [1,3,2,null,null,5,4]return = trueSwap the left and right children of every node in first. Node 1 then has 3 on the left and 2 on the right, and node 2 has 5 on the left and 4 on the right. That is exactly second, so the answer is true.
Example 2
first = [1,2,3]second = [1,2,3]return = falseThe two trees are the same, but they are not mirror images. The mirror of first has 3 as the left child of the root, while second has 2 there, so the answer is false.
Example 3
first = [7,7]second = [7,7]return = falseEvery value is 7, but the shapes do not mirror each other. In first the second node is a left child, so in a mirror image it would have to be a right child. In second it is also a left child, so the answer is false.
Constraints
- Each tree has between
0and500nodes. -10^4 <= Node.val <= 10^4
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Maximum Supplier ProfitONSITE INTERVIEW · Seen Aug 2024
Similar questions at other companies
- 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 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
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Vertical Order Traversal of a Binary TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
Interview reports that mention this question
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)