Distance Between Two Nodes in a Binary Tree
Updated
Problem statement
You are given the root of a binary tree and two integers p and q. Every node has a different value, and both p and q are values of nodes in the tree.
The distance between two nodes is the number of edges on the path between them. An edge is the link between a node and one of its children. The path may go up toward the root and then down again.
Return the distance between the node with value p and the node with value q. If p == q, they are the same node and the distance is 0.
Function
findDistance(root: TreeNode, p: int, q: int) → intExamples
Example 1
root = [8,3,11,6,2,null,4,null,null,9,15]p = 6q = 15return = 3The path from 6 to 15 is 6 - 3 - 2 - 15. It goes up from 6 to 3, then down through 2 to 15. That is 3 edges.
The path does not use the root 8, because 3 is the lowest node that has both 6 and 15 below it.
Example 2
root = [8,3,11,6,2,null,4,null,null,9,15]p = 9q = 4return = 5The path is 9 - 2 - 3 - 8 - 11 - 4: up three edges to the root 8, then down two edges to 4. That is 5 edges.
Example 3
root = [5,1,null,7,null,12]p = 12q = 5return = 3Node 5 is above node 12, so the path only goes up: 12 - 7 - 1 - 5, which is 3 edges.
Example 4
root = [20,10,30]p = 30q = 30return = 0Both values name the same node, so the distance is 0.
Constraints
- The tree has between
1and500nodes. 0 <= Node.val <= 1000000000- All node values are different.
pandqare both values of nodes in the tree.
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
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
- Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
- Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Time to Burn a Binary Tree From One NodeAdobe · PHONE SCREENAdobe interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
- Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign 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