Invert a Binary Tree
Updated
Problem statement
Turn the binary tree at root into its mirror image: at every node, the left child and the right child trade places. Do this for the whole tree, not just the root, and return the root.
Function
invertTree(root: TreeNode) → TreeNodeExamples
Example 1
root = [8,3,12,1,5,null,14]return = [8,12,3,14,null,5,1]Moved or new
Every node trades its left and right child. The root 8 now has 12 on the left and 3 on the right; 12 keeps its only child 14, which moves to its left; 3 now has 5 on the left and 1 on the right.
Example 2
root = [-2,null,7,6]return = [-2,7,null,null,6]Moved or new
-2 had only a right child 7, and 7 had only a left child 6. After the flip, 7 is the left child of -2 and 6 is the right child of 7.
Example 3
root = []return = []An empty tree has nothing to flip, so the answer is still empty.
Constraints
- The number of nodes in the tree is between
0and100. -100 <= Node.val <= 100
More Capital One interview questions
- Count Records Present in A but Not BPHONE SCREEN · Seen Oct 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Maximum Subarray SumPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2025
- Ten-Pin Bowling Frame ScoresPHONE SCREEN · Seen Nov 2023
- Min StackPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2023
- Count How Often a Word Appears in a ParagraphPHONE SCREEN
- In-Memory File System With Directory SizesPHONE SCREEN · ONSITE INTERVIEW
- Pick the K Nearest Grid Points to (0, 0)PHONE SCREEN · ONSITE INTERVIEW
- The Nth Number in the Fibonacci SequencePHONE SCREEN · ONSITE INTERVIEW
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Nodes at a Given N-ary Tree LevelAmazon · PHONE SCREEN
- Binary Tree Level-Order Traversal by LevelsAdobe · PHONE SCREEN · ONSITE INTERVIEWAdobe interview questions
- Level of a Node in a Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Left View of a Binary TreeMygate · PHONE SCREENMygate interview questions
- Maximum-Scoring Consecutive Tree LevelsJane Street · PHONE SCREENJane Street interview questions
- Minimum Edits to a Perfect Binary TreeSnowflake · PHONE SCREENSnowflake interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
Capital One interview experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)