AfterAcademy Tech
•
03 Mar 2020

Asked in: Amazon, Microsoft
Difficulty: Medium
Problem Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
Problem Note
Example 1

The node structure passed to your function will be
class TreeNode
{
int data
TreeNode left
TreeNode right
}
You may try solving the problem here
We can solve this using the approaches as discussed while finding LCA in a binary tree. But, a binary search tree’s property could be utilized, to come up with a better algorithm with lesser complexity.
The lowest common ancestor for the two nodes node1 and node2 would be the last ancestor node common to both of them. Here last is defined in terms of the depth of the node.

If we boil down the above explanation then we could justify it in this form →
LCA is the last root satisfying min(node1, node2) <= root <= max(node1,node2) where node1 and node2 will be in the subtree of the root while traversing top to bottom in the tree.
Solution Steps
node1 and node2 are in the right subtree, then recursively move to the right subtree.node1 and node2 are in the left subtree, then recursively move to the left subtree.Pseudo Code
TreeNode lowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) {
if (node1.val > root.val and node2.val > root.val) {
// If both node1 and node2 are greater than parent
return lowestCommonAncestor(root.right, node1, node2)
}
else
if (node1.val < root.val and node2.val < root.val) {
// If both node1 and node2 are lesser than parent
return lowestCommonAncestor(root.left, p, q)
} else {
// We have found the split point, i.e. the LCA node.
return root
}
}
Complexity Analysis
Time Complexity: O(log n) if the tree is balanced binary search tree otherwise, in the worst case, the complexity could be O(n)
Space Complexity: O(n)
Critical Ideas to Think
If we look closely the recursive solution and try to implement the same logic iteratively then you will find that we don’t need to backtrack to find the LCA node and just have to traverse down the tree and look for the split point of node1 and node2 which could be done without using any stack or queue.
Solution steps
node1 and node2 are in the right subtree, then iteratively move to the right subtree.node1 and node2 are in the left subtree, then iteratively move to the left subtree.Pseudo Code
TreeNode lowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) {
// Traverse the tree
while (root is not null) {
if (node1.val > root.val and node2.val > root.val) {
// If both node1 and node2 are greater than parent
root = root.right
}
else
if (node1.val < root.val and node2.val < root.val) {
// If both node1 and node2 are lesser than parent
root = root.left
}
else {
// We have found the split point, i.e. the LCA node.
return node
}
}
return null
}
Complexity Analysis
Time Complexity: O(log n) if the tree is balanced binary search tree otherwise, in the worst case, the complexity could be O(n)
Space Complexity: O(1)
Critical Ideas to Think

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given a binary tree, write a program to find the lowest common ancestor (LCA) of two given nodes in the tree. The question is asked previously in Amazon, Facebook, Adobe and requires an understanding of tree data structure.

AfterAcademy Tech
Binary Search Trees is one of the most important variation of binary tree and is extremely useful in practical applications. The blog discusses the operations and applications of this powerful data structure

AfterAcademy Tech
Given a Binary Search Tree such that two of the nodes of this tree have been swapped by mistake. You need to write a program that will recover this BST while also maintaining its original structure

AfterAcademy Tech
In this blog, we will see the difference between a binary search tree and a hash table. We will see which data structure should be used when to solve our problems.
