Shortest Path Between BST Nodes
Updated
Problem statement
You are given the root of a binary search tree with distinct integer values and two target values, first and second. Return the number of edges on the unique shortest path between their nodes.
If either target value does not occur in the tree, return -1. If first == second and that value exists, return 0.
Use the binary-search-tree ordering to locate where the two search paths split, then measure the distance from that split node to each target.
Interview extension
If the tree were an ordinary binary tree, the ordering shortcut would disappear. A general solution would find the lowest common ancestor while tracking whether both targets exist, then add the two downward distances.
Function
shortestPathLength(root: TreeNode, first: int, second: int) → intExamples
Example 1
root = [6,2,8,0,4,7,9,null,null,3,5]first = 2second = 8return = 2The shortest path is 2 -> 6 -> 8, which contains two edges.
Example 2
root = [6,2,8,0,4,7,9,null,null,3,5]first = 2second = 5return = 2The shortest path is 2 -> 4 -> 5, which contains two edges.
Example 3
root = [6,2,8,0,4,7,9,null,null,3,5]first = 2second = 42return = -1Value 42 is absent, so no path between two existing target nodes can be returned.
Constraints
- The tree contains between
0and100000nodes. -10^9 <= node.val, first, second <= 10^9.- All node values are distinct.
- The input tree satisfies the binary-search-tree ordering invariant.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Validate Binary Search TreeTokopedia · ONSITE INTERVIEWTokopedia interview questions
- Check the Order of a Binary Search TreeCapital One · ONSITE INTERVIEWCapital One interview questions
- Add All Larger Values in a BSTAdobe · PHONE SCREENAdobe interview questions
- Inorder Predecessor in a BSTNutanix · PHONE SCREENNutanix interview questions
- Sum Dynamic Weighted Tree DistancesJuspay · OA
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions