Remove the Leaves of a Binary Tree
Updated
Problem statement
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) → TreeNodeExamples
Example 1
root = [10,4,15,2,7,null,20]return = [10,4,15]Removed
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]Removed
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 = []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
1and1000. -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