Find distance between two nodes of a Binary Search Tree
Given a Binary Search Tree and two keys in it. Find the distance between two nodes with given two keys. It may be assumed that both keys exist in BST.
Input : Root of above tree a = 3, b = 9 Output : 4 Distance between 3 and 9 in above BST is 4. Input : Root of above tree a = 9, b = 25 Output : 3 Distance between 9 and 25 in above BST is 3.
We have discussed distance between two nodes in binary tree. The time complexity of this solution is O(n)
In case of BST, we can find distance faster. We start from root and for every node, we do following.
 If both keys are greater than current node, we move to right child of current node.
 If both keys are smaller than current node, we move to left child of current node.
 If one keys is smaller and other key is greater, current node is Lowest Common Ancestor (LCA) of two nodes. We find distances of current node from two keys and return sum of the distances.

// CPP program to find distance between
// two nodes in BST
#include <bits/stdc++.h>
using
namespace
std;
struct
Node {
struct
Node* left, *right;
int
key;
};
struct
Node* newNode(
int
key)
{
struct
Node* ptr =
new
Node;
ptr>key = key;
ptr>left = ptr>right = NULL;
return
ptr;
}
// Standard BST insert function
struct
Node* insert(
struct
Node* root,
int
key)
{
if
(!root)
root = newNode(key);
else
if
(root>key > key)
root>left = insert(root>left, key);
else
if
(root>key < key)
root>right = insert(root>right, key);
return
root;
}
// This function returns distance of x from
// root. This function assumes that x exists
// in BST and BST is not NULL.
int
distanceFromRoot(
struct
Node* root,
int
x)
{
if
(root>key == x)
return
0;
else
if
(root>key > x)
return
1 + distanceFromRoot(root>left, x);
return
1 + distanceFromRoot(root>right, x);
}
// Returns minimum distance beween a and b.
// This function assumes that a and b exist
// in BST.
int
distanceBetween2(
struct
Node* root,
int
a,
int
b)
{
if
(!root)
return
0;
// Both keys lie in left
if
(root>key > a && root>key > b)
return
distanceBetween2(root>left, a, b);
// Both keys lie in right
if
(root>key < a && root>key < b)
// same path
return
distanceBetween2(root>right, a, b);
// Lie in opposite directions (Root is
// LCA of two nodes)
if
(root>key >= a && root>key <= b)
return
distanceFromRoot(root, a) +
distanceFromRoot(root, b);
}
// This function make sure that a is smaller
// than b before making a call to findDistWrapper()
int
findDistWrapper(Node *root,
int
a,
int
b)
{
if
(a > b)
swap(a, b);
return
distanceBetween2(root, a, b);
}
// Driver code
int
main()
{
struct
Node* root = NULL;
root = insert(root, 20);
insert(root, 10);
insert(root, 5);
insert(root, 15);
insert(root, 30);
insert(root, 25);
insert(root, 35);
int
a = 5, b = 55;
cout << findDistWrapper(root, 5, 35);
return
0;
}
Output:
2
Time Complexity : O(h) where h is height of Binary Search Tree.
Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growthoriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at
http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide