# Lowest Common Ancestor(LCA) in Binary Tree in C++

## Lowest Common Ancestor in Binary Tree in C++

The lowest common ancestor between two nodes n1 and n2 is defined as the lowest node in T that has both n1 and n2 as descendants (where we allow a node to be a descendant of itself). Thus lca is guaranteed to exist if both the nodes are present in tree. In any other case it is NULL. In this article, we will learn about how to find the lowest common ancestor in binary tree in C++.

### Algorithm :

• Create a recursive function that takes a node and the two values n1 and n2.
• If the value of the current node is less than both n1 and n2, then LCA lies in the right subtree. Call the recursive function for the right subtree.
• If the value of the current node is greater than both n1 and n2, then LCA lies in the left subtree. Call the recursive function for the left subtree.
• If both the above cases are false then return the current node as LCA.

Let T be a rooted tree. The lowest common ancestor between two nodes
n1 and n2 is defined as the lowest node in T that has both n1 and n2 as
descendants (where we allow a node to be a descendant of itself).

Thus LCA is guaranteed to exist if both the nodes are present in tree.  In any other case it is NULL.

Traverse the tree from bottom, and if node value matches any of two nodes, we pass that to its parent, parent would now check it’s left and right subtree,if it finds, node in each one of them , then parent is the LCA and we pass it to its parent up to the root. if not, we pass the lower node which contains either one of the two nodes (if the left or right subtree contains either n1 or n2), or NULL (left and right subparts do not contain either of n1 & n2) up.

Readers are requested to submit their codes in different languages and using different ways in comment section below. Also it is advised to try it yourself before looking at the solution .

The overall time complexity of this approach is O(n).

## Code Implementation for constructing tree from postorder and preorder traversals in C++

Run
```#include<bits/stdc++.h>
using namespace std;

struct Treenode// definition of structure of tree node which has lchild child and rchild child
{
int val;
Treenode* lchild;
Treenode* rchild;
};

struct Treenode* newNode(int key)// function for new node creation
{ /* dynamically allocate memory for a new node */
struct Treenode* newnode = (struct Treenode*)malloc(sizeof(struct Treenode));
newnode->val = key;
newnode->lchild = NULL;
newnode->rchild = NULL;

return(newnode);
}

struct Treenode *findLCA(struct Treenode* root, int node1, int node2)
{
// Base case
if (!root) return NULL;

// if root value equals any of two nodes data then lca is root.
if (root->val == node1 || root->val == node2)
return root;

// otherwise we need to look in left pr right parts
Treenode *ls = findLCA(root->lchild, node1, node2);
Treenode *rs= findLCA(root->rchild, node1, node2);

if (ls && rs) return root;

// check if left subtree or right subtree is LCA
return (ls != NULL)? ls: rs;
}

int main()
{
Treenode *root1 = newNode(232);
Treenode *root2 = newNode(232);// these part is concerned with creation of tree
// user can edit this as per wish
root1->lchild = newNode(231);
root1->rchild = newNode(231);
root1->lchild->lchild = newNode(431);
root1->lchild->rchild = newNode(531);

root2->lchild = newNode(231);
root2->rchild = newNode(331);
root2->lchild->lchild = newNode(431);
root2->lchild->rchild = newNode(513);

cout<< LCA(root)<< endl;

getchar();
return 0;
}
```
```Output:

Postorder traversal
60 70 40 50 20 30 10```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

### Traversals

• Traversal in Trees
• Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
• Tree Traversals: Depth First Search (DFS) : C | C++ | Java
• Construct a Binary Tree from Postorder and Inorder

### AVL Trees

• AVL Trees
• AVL Trees: Introduction
• AVL Tree Insertion : C | C++ | Java
• AVL Tree Deletion : C | C++ | Java
• Insertion in a Binary Tree (Level Order) – C | C++ | Java
• Searching in Binary Tree – C | C++ | Java
• Searching in a Binary Search Tree – C | C++ | Java

### Complete Programs for Trees

• Depth First Traversals – C | C++ | Java
• Level Order Traversal – C | C++Java
• Construct Tree from given Inorder and Preorder traversals – C | C++Java
• Construct Tree from given Postorder and Inorder traversals – C | C++Java
• Construct Tree from given Postorder and Preorder traversal – C | C++Java
• Find size of the Binary tree – C | C++Java
• Find the height of binary tree – C | C++Java
• Find maximum in binary tree – C | C++Java
• Check whether two tree are identical- CC++Java
• Spiral Order traversal of Tree- CC++Java
• Level Order Traversal Line by Line – C | C++Java
• Hand shaking lemma and some Impotant Tree Properties.
• Check If binary tree if Foldable or not.- CC++Java
• check whether tree is Symmetric – C| C++Java.
• Check for Children-Sum in Binary Tree- C|C++Java
• Sum of all nodes in Binary Tree- CC++ | Java
• Lowest Common Ancestor in Binary Tree- CC++ | Java

### Traversals

• Traversal in Trees
• Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
• Tree Traversals: Depth First Search (DFS) : C | C++ | Java
• Construct a Binary Tree from Postorder and Inorder

### AVL Trees

• AVL Trees
• AVL Trees: Introduction
• AVL Tree Insertion :  C | C++ | Java
• AVL Tree Deletion : C | C++ | Java
• Insertion in a Binary Tree (Level Order) – C | C++ | Java
• Searching in Binary Tree – C | C++ | Java
• Searching in a Binary Search Tree – C | C++ | Java

### Complete Programs for Trees

• Depth First Traversals – C | C++ | Java
• Level Order Traversal – C | C++Java
• Construct Tree from given Inorder and Preorder traversals – C | C++Java
• Construct Tree from given Postorder and Inorder traversals – C | C++Java
• Construct Tree from given Postorder and Preorder traversal – C | C++Java
• Find size of the Binary tree – C | C++Java
• Find the height of binary tree – C | C++Java
• Find maximum in binary tree – C | C++Java
• Check whether two tree are identical- CC++Java
• Spiral Order traversal of Tree- CC++Java
• Level Order Traversal LIne by Line – C | C++Java
• Hand shaking lemma and some Impotant Tree Properties.
• Check If binary tree if Foldable or not.- CC++Java
• check whether tree is Symmetric  C| C++Java.
• Check for Children-Sum in Binary Tree- C|C++Java
• Sum of all nodes in Binary Tree- CC++ | Java
• Lowest Common Ancestor in Binary Tree. CC++ | Java