FastPrepInvert a Binary Tree
Problems/Invert a Binary Tree

Invert a Binary Tree

Capital One logoCapital One● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A December 2020 report from a candidate for a Capital One software engineering internship names one question, inverting a binary tree, and the candidate rated it hard. The other problems in that process are mentioned but not named.

The report gives no input format or limits. Here the tree comes in level order, inverting means every node swaps its left and right child, and the tree has at most 100 nodes. These are our choices.

Our version is about 90% the same as the reported question.

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) → TreeNode

Examples

Example 1

root = [8,3,12,1,5,null,14]return = [8,12,3,14,null,5,1]
root
83151214
→
return
81214351
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]
root
-276
→
return
-276
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 = []
root
empty
return
empty

An empty tree has nothing to flip, so the answer is still empty.

Constraints

  • The number of nodes in the tree is between 0 and 100.
  • -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)
All Capital One interview questionsSee Capital One hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        // Write your code here.
    }
}
Checking account…