FastPrepRemove the Leaves of a Binary Tree
Problems/Remove the Leaves of a Binary Tree

Remove the Leaves of a Binary Tree

Capital One logoCapital One● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2021 Capital One report from a software engineer candidate in McLean lists a single coding question. It is about a binary tree whose end nodes, the leaves, are to be removed.

It does not say whether to stop after one pass. Here only the leaves of the given tree are removed, once, so nodes that become leaves afterwards stay. A tree that is a single node becomes empty. Trees come in level order with up to 1000 nodes. These are our choices.

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

A leaf is a node with no children. Remove every leaf of the binary tree whose root is root, and return the root of what is left.

Remove only the leaves of the tree as it is given. A node that has no children after this step stays. If the root itself is a leaf, the result is an empty tree.

Function

removeLeaves(root: TreeNode) → TreeNode

Examples

Example 1

root = [10,4,15,2,7,null,20]return = [10,4,15]
root
104271520
Removed
→
return
10415

The leaves are 2, 7 and 20: none of them has a child. Removing them leaves 10 with children 4 and 15. 4 and 15 are leaves now, but they stay, because only the leaves of the original tree are removed.

Example 2

root = [6,6,null,6,null,6]return = [6,6,null,6]
root
6666
Removed
→
return
666

The tree is a path of four 6s going left. Only the bottom 6 has no child, so only it goes, and the path keeps three nodes.

Example 3

root = [-3]return = []
root
-3
return
empty

The root has no children, so it is itself a leaf. Removing it leaves an empty tree.

Constraints

  • The number of nodes in the tree is between 1 and 1000.
  • -1000 <= Node.val <= 1000

More Capital One Onsite Interview questions

  • Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
  • Sort ColorsONSITE INTERVIEW · Seen Oct 2026
  • Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
  • Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
  • Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
  • Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
  • Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
  • Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
  • Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025

Similar questions at other companies

  • Count One-Valued Regions in a Binary TreeGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Path SumSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
  • Root-to-Node Path in a Binary TreeAmazon · ONSITE INTERVIEW
  • Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
  • Tree Level with the Most NodesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Diameter of a Binary TreeMeta · ONSITE INTERVIEWMeta interview questions
  • Check That Every Node Equals Its Descendant SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Mirror Image Binary TreesExpedia · ONSITE INTERVIEWExpedia interview questions

Capital One interview experiences

  • Capital One Coding OA Questions: 3 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
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 removeLeaves(TreeNode root) {
        // Write your code here.
    }
}
Checking account…