FastPrepShortest Path Between BST Nodes
Problems/Shortest Path Between BST Nodes

Shortest Path Between BST Nodes

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for the length of the shortest path between two BST nodes, including what to do with a missing node. It then asks how the approach changes for a general binary tree. It does not give a function signature or input limits. Our version is about 97% the same as the reported question.

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) → int

Examples

Example 1

root = [6,2,8,0,4,7,9,null,null,3,5]first = 2second = 8return = 2
root
620435879

The 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 = 2
root
620435879

The 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 = -1
root
620435879

Value 42 is absent, so no path between two existing target nodes can be returned.

Constraints

  • The tree contains between 0 and 100000 nodes.
  • -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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int shortestPathLength(TreeNode root, int first, int second) {
        // Write your code here.
    }
}
Checking account…