For the above BST:
‘NODE1’ = 6, ‘NODE2’ = 14
Distance between 6 and 14 = (Number of nodes in the path from 6 to 14) + 1.
So the path from 6 to 14 is : ( 6 -> 3 -> 8 -> 10 -> 14).
Distance between 6 and 14 = 3 ( i.e. 3, 8, 10 are in path) + 1 = 4.
The first line of input contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first line of input contains the elements of the tree in the level order form separated by a single space.
If any node does not have a left or right child, take -1 in its place. Refer to the example below.
The second line of each test case contains two space-separated integers, ‘NODE1’ and ‘NODE2’, denoting the keys of the two given nodes.
Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 in its place.
For example, the input for the tree (not a BST) depicted in the below image would be :
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
For each test case, return an integer denoting the distance between two keys, i.e., ‘NODE1’ and ‘NODE2’ in the given BST.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10^2
1 <= N <= 10^3
1 <= Node.data <= 10^9
Where 'N' denotes the number of nodes in the BST and 'Node.data' represents the keys of nodes in BST.
The keys ‘NODE1’ and ‘NODE2’ are present in the given BST.
Time Limit: 1 sec
Lowest common ancestor: Let ‘T’ be a rooted tree. The lowest common ancestor of the two nodes, ‘NODE1’ and ‘NODE2’, is the lowest node in ‘T’ with both ‘NODE1’ and ‘NODE2’ as its descendants (here, a node can be a descendant of itself).
We store the path from ‘ROOT’ to ‘NODE1’ in ‘PATH1’ and ‘ROOT’ to ‘NODE2’ in ‘PATH2’. Iterate the arrays ‘PATH1’ and ‘PATH2’ until they have the same values, the LCA will be the node just before the mismatch. The following function finds the path from ‘ROOT’ to the node with the given ‘KEY’ and stores it in the array ‘PATH’ (passed by reference).
‘Boolean pathFromRoot(BinaryTreeNode ROOT, array PATH, integer KEY)’:
The shortest distance between ‘NODE1’ and ‘NODE2’ = (Distance from ‘LCA’ to ‘NODE1’) + (Distance from ‘LCA’ to ‘NODE2’).
The shortest distance between ‘NODE1’ and ‘NODE2’ = (Distance from ‘LCA’ to ‘NODE1’) + (Distance from ‘LCA’ to ‘NODE2’).
The distance between an ancestor node(in this case, the ‘LCA’ of ‘NODE1’ and ‘NODE2’) and its descendant nodes can be found recursively using the BST property. The following function calculates the distance between the ancestor node ‘ROOT’ and the descendant node with key as ‘X’:
‘distanceFromRoot(BinaryTreeNode ROOT, integer X)’:
The LCA of ‘NODE1’ and ‘NODE2’ can also be found recursively using the BST property. Start from the ‘ROOT’ node. If the key at the ‘ROOT’ node is greater than the key at ‘NODE1’ and ‘NODE2’, then the LCA is present in the left subtree of ‘ROOT’. Similarly, if both the keys are greater than the key at ‘ROOT’, then the LCA is present in the right subtree of the ‘ROOT’. If one of the keys is in the left subtree and the other in the right subtree of ‘ROOT’ or one of the keys is equal to the key at ‘ROOT’, then the ‘ROOT’ node is the LCA.
The given function ‘distanceBetween2’ can be called recursively to find both the LCA and the final answer.