Level of a Node in a Binary Tree
Updated
Problem statement
You are given the root of a binary tree and an integer target. Every node holds a different value, and exactly one node holds target.
The root is on level 1. Its children are on level 2, their children are on level 3, and so on: a node's level is one more than its parent's level.
Return the level of the node whose value is target. The tree is not a binary search tree, so the values can be in any order.
Function
levelOfNode(root: TreeNode, target: int) → intExamples
Example 1
root = [5,2,9,null,4,7,null,null,null,6]target = 6return = 4Level 1 holds the root 5. Level 2 holds 2 and 9. Level 3 holds 4 (the right child of 2) and 7 (the left child of 9). Level 4 holds 6, the left child of 7. So the answer is 4.
Example 2
root = [1,2,3,4,5]target = 3return = 23 is a child of the root 1, so it is on level 2.
Example 3
root = [10,-3,null,20]target = 20return = 3The path from the root is 10, then -3, then 20. That is three nodes, so the answer is 3. Notice that 20 is larger than the root 10 but sits on its left side. The values are not in search-tree order, so you cannot use their size to choose a direction.
Constraints
- The tree has between
1and500nodes. -10000 <= Node.val, target <= 10000- All node values are different, and exactly one node has the value
target.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 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
- Left View of a Binary TreeMygate · PHONE SCREENMygate interview questions
- Invert a Binary TreeCapital One · PHONE SCREENCapital One 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
Bloomberg LP interview experiences
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg SWE Phone Screens with Two Teams: 1 Experience
- Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
- Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
- Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
- Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience