AfterAcademy Tech
•
03 Mar 2020

Difficulty: Medium
Asked in: Amazon, Facebook, Adobe
Problem Description
Given a binary tree, write a program to find the lowest common ancestor (LCA) of two given nodes in the tree.
Problem Note
Example 1

Example 2

The node structure passed to your function will be
class TreeNode
{
int data
TreeNode left
TreeNode right
}
We will be discussing recursive and iterative approaches
A pretty intuitive approach is to traverse the tree in a depth-first manner. The moment you encounter either of the nodes node1 or node2, return the node. The least common ancestor would then be the node for which both the subtree recursions return a non-NULL node. It can also be the node which itself is one of node1 or node2 and for which one of the subtree recursions returns that particular node.
Solution steps
node1 or node2, we would return that node.node1 and node2.Pseudo Code
TreeNode lowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) {
if(not root)
return NULL
if (root == node1 or root == node2)
return root
TreeNode left = lowestCommonAncestor(root.left, node1, node2)
TreeNode right = lowestCommonAncestor(root.right, node1, node2)
if(not left)
return right
else if(not right)
return left
else
return root
}
Complexity Analysis
Time Complexity: O(n), where n is the total number of nodes in the tree.
Space Complexity: O(n)
Critical ideas to think
An intuition for an iterative approach is if we have parent pointers for each node we can traverse back from node1 and node2 to get their ancestors. The first common node we get during this traversal would be the LCA node. So, We can save the parent pointers in a dictionary as we traverse the tree.
Solution step
node1 and node2 both, keep storing the parent pointers in a dictionary.node1 and node2, we get all the ancestors for node1 using the parent dictionary and add to a set called ancestors.node2. If the ancestor is present in the ancestors set for node1, this means this is the first ancestor common between node1 and node2 (while traversing upwards) and hence this is the LCA node.Pseudo Code
TreeNode lowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) {
// create a parent map that maps the childnode with its parent
map parent({{root,NULL}})
// a stack to make a post order traversal iteratively
stack stk({root})
while(not node1 exist in parent or not node2 exist in parent){
TreeNode n = stk.top()
stk.pop()
if(n.left) {
parent[n.left] = n
stk.push(n.left)
}
if(n.right) {
parent[n.right] = n
stk.push(n.right)
}
}
// ancestor set for node1
set path
while(node1 is not NULL) {
path.insert(node1)
node1 = parent[node1]
}
// The first ancestor of node2 which appears in
// node1's ancestor set is their lowest common ancestor.
while(node2 not exist in path)
node2 = parent[node2]
return node2
}
Complexity Analysis
Time Complexity: O(n)
Space complexity: O(n). In the worst-case space utilized by the stack, the parent pointer dictionary and the ancestor set would be n each, since the height of a skewed binary tree, could be n.
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 search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. This is a very famous interview problem and previously asked in Microsoft and Amazon.

AfterAcademy Tech
Given a binary tree, invert the binary tree and return it. An inverted form of a Binary Tree is another Binary Tree with left and right children of all non-leaf nodes interchanged. You may also call it the mirror of the input tree.

AfterAcademy Tech
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

AfterAcademy Tech
Given a binary tree and a sum, write a program to determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Its an easy problem based on Binary tree traversal and a famous interview question
