Shortest Distance in a Binary Search Tree

Your task is to determine the shortest distance between two nodes with given keys in a Binary Search Tree (BST).

It is assured that both keys exist within the BST.

Input:

The first input line contains an integer 'T' denoting the number of test cases.
For each test case:
- The first line provides the BST elements in level order, separated by spaces. Use -1 for null nodes.
- The second line contains two space-separated integers, 'NODE1' and 'NODE2', which are the keys of the two specified nodes.

Output:

Output the shortest distance as an integer for each test case, representing the number of nodes in the path between 'NODE1' and 'NODE2', inclusive of both nodes.

Example:

Input:
1
6 3 8 2 4 7 10 -1 -1 -1 -1 -1 -1 9 14
6 14
Output:
4
Explanation:

The path from 6 to 14 in the BST is 6 -> 3 -> 8 -> 10 -> 14, so the distance is 4 nodes.

Constraints:

  • 1 <= T <= 102
  • 1 <= N <= 103 where N is the number of nodes in the BST.
  • 1 <= Node.data <= 109
  • The keys 'NODE1' and 'NODE2' are guaranteed to exist in the tree.
  • Time Limit: 1 second
Note:
Implement the function; do not print the results.
AnswerBot
4mo

Find the shortest distance between two nodes in a Binary Search Tree.

  • Traverse the BST to find the paths from the root to both nodes.

  • Compare the paths to find the common ancestor node.

  • Calculate the dis...read more

Help your peers!
Select
Add answer anonymously...

Blackrock Software Developer interview questions & answers

A Software Developer was asked Q. Validate Binary Search Tree Problem Statement Your task is to determine if a giv...read more
A Software Developer was asked Q. Bridge in Graph Problem Statement Given an undirected graph with V vertices and ...read more
A Software Developer was asked Q. Shortest Distance in a Binary Search Tree Your task is to determine the shortest...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Validate Binary Search Tree Problem Statement Your task is to determine if a giv...read more
A Software Developer was asked Q2. Bridge in Graph Problem Statement Given an undirected graph with V vertices and ...read more
A Software Developer was asked Q3. Shortest Distance in a Binary Search Tree Your task is to determine the shortest...read more
Blackrock Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits