Find a Value in a Binary Search Tree
Updated
Problem statement
root is a binary search tree with distinct values: at every node, all values in the subtree under its left child are smaller than the node's value, and all values in the subtree under its right child are larger.
Find the node whose value is val and return it, which returns its whole subtree. If no node holds val, return an empty tree.
Function
searchBST(root: TreeNode, val: int) → TreeNodeExamples
Example 1
root = [15,8,23,4,11,null,30,null,6]val = 8return = [8,4,11,null,6]Removed
Moved or new
8 is smaller than the root 15, so go left and find it right away. The answer is 8 with everything below it: 4, 11 and 6.
Example 2
root = [15,8,23,4,11,null,30,null,6]val = 9return = []From 15 go left to 8, then right to 11, then left, which is empty. 9 is not in the tree, so the answer is empty.
Example 3
root = [7]val = 7return = [7]The only node holds the value, so the whole tree is the answer.
Constraints
- The number of nodes in the tree is between
1and500. 1 <= Node.val, val <= 10^7rootis a binary search tree with distinct values.
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
- Insert into a Left-Complete Binary TreeRubrik · ONSITE INTERVIEWRubrik interview questions
- Kth Closest Element in a BSTLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Shortest Path Between BST NodesGoogle · ONSITE INTERVIEWGoogle interview questions
- Repeated BST Range Sum and AverageMeta · ONSITE INTERVIEWMeta interview questions
- Lowest Common Ancestor in a BSTPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Delete Node in a BSTBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Validate Binary Search TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
- Build a Perfect Binary Search TreeExpedia · ONSITE INTERVIEWExpedia interview questions
- Next Larger Value in a BSTMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Lowest Common Ancestor in an Inserted BSTAmazon · PHONE SCREEN
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