The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth


Problem :-
Find the Least Common Ancestor ( LCA ) of two nodes in a Binary Tree .
LCA of two nodes x and y in a Binary Tree is the lowest node in the tree which contain both x and y as descendants. For the Binary Tree shown below, LCA of nodes LCA of nodes 2 and 20 is 15 .
Similarly, LCA of nodes 5 and 30 is 25 .

LCA of two nodes in a Binary Tree

Solution :-
Suppose we want to find the LCA of two nodes x and y.
We have seen previously how LCA in Binary Search Tree ( BST ) is computed utilizing the properties of BST but a normal binary tree may not follow those properties. So, the technique is different here.
Since our intent is to find the lowest node which is the ancestor of nodes x and y, we will starting searching that node from bottom to up in the tree. For each node starting from the bottom-most level, we will check recursively if we can find a node P suct that x lies in left subtree of P and y lies in right subtree or x in right and y in left subtree. See the implementation below.

using namespace std;

// node structure
typedef struct tree_node
   int value;
   struct tree_node *left, *right;

// create a new node
node *getNewNode(int value) {
   node *new_node = new node;
   new_node->value = value;
   new_node->left = NULL;
   new_node->right = NULL;
   return new_node;

// create the tree
node *createTree() {
   node *root = getNewNode(25);
   root->left = getNewNode(15);
   root->right = getNewNode(30);
   root->left->left = getNewNode(5);
   root->left->right = getNewNode(20);
   root->left->left->left = getNewNode(2);
   root->right->right = getNewNode(40);
   return root;

// check if a target node is present in a subtree
// starting at a particular node
bool isNodePresent(node *temp, int data) {
   if (temp == NULL) return false;
   if (temp->value == data) return true;
      return isNodePresent(temp->left,data) | isNodePresent(temp->right,data);

//computes the LCA
int findLCA(node *root, int n1, int n2) {
   static bool lca_found = false;
   static int lca = -1;
   if (root == NULL)
      return -1;
   // start traversing from the leaf nodes towards root
   findLCA(root->left,n1,n2); //check if lca is in left subtree
   findLCA(root->right,n1,n2); //check if lca is in right subtree
   if(!lca_found && (isNodePresent(root->left,n1) && isNodePresent(root->right,n2))
       || (isNodePresent(root->left,n2) && isNodePresent(root->right,n1))) {
      lca_found = true;
      lca = root->value;
   return lca;

int main() {
   node *root = createTree();
   int n1 = 30, n2 = 20;
   int lca = findLCA(root,n1,n2);
   cout<<"\nLCA of "<<n1<<" & "<<n2<<" is "<<lca;
   return 0;

Back | Next

All problems on Trees
* Implement the inorder , preorder and postorder traversal mechanisms of a tree
* Implement an algorithm to insert a node in a Binary Search Tree ( BST )
* Implement an algorithm to find the height of a Binary Tree
* Implement an algorithm to get the level of a node in a Binary Tree assuming root node to be at level 1
* Get the root to leaf path in a Binary Tree such that the sum of the node values in that path is minimum among all possible root to leaf paths
* Print all the ancestors of a given node in a Binary Tree
* Replace all the node values with the sum of the node values of ancestral nodes in a Binary Tree
* Check if the given tree is a sum tree i.e value at each node is equal to the value of all elements in its left subtree and right subtree
* Given a Binary Tree, create another tree which is a mirror image of the given tree
* Construct a tree, given its inorder and preorder traversals
* Find the Least Common Ancestor ( LCA ) of two nodes in a Binary Search Tree
* Find the Least Common Ancestor ( LCA ) of two nodes in a Binary Tree
* Compute the Diameter of a given Binary Tree
* Check if a given tree is a Binary Search Tree ( BST )