- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to Find Lowest Common Ancestor in a Binary Search Tree

A binary tree which has at most two children, specified as left child and right child. This is a C++ Program to find the lowest common ancestor in a Binary Tree.

## Algorithm

Begin Create a structure n to declare data d, a left child pointer l and a right child pointer r. Create a function to create newnode. Call a function LCA() to Find lowest common ancestor in a binary tree: Assume node n1 and n2 present in the tree. If root is null, then return. If root is not null there are two cases. a) If both n1 and n2 are smaller than root, then LCA lies in left. b) If both n1 and n2 are greater than root, then LCA lies in right. End.

## Example Code

#include<iostream> using namespace std; struct n { int d; struct n* l, *r; }*p = NULL; struct n* newnode(int d) { p = new n; p->d= d; p->l = p->r = NULL; return(p); } struct n *LCA(struct n* root, int n1, int n2) { if (root == NULL) return NULL; if (root->d > n1 && root->d > n2) return LCA(root->l, n1, n2); if (root->d< n1 && root->d < n2) return LCA(root->r, n1, n2); return root; } int main() { n* root = newnode(9); root->l = newnode(7); root->r = newnode(10); root->l->l = newnode(6); root->r->l= newnode(8); root->r->r = newnode(19); root->r->l->r = newnode(4); root->r->r->r = newnode(20); int n1 = 20, n2 = 4; struct n *t = LCA(root, n1, n2); cout<<"Lowest Common Ancestor of 20 and 4 is:" <<t->d<<endl; n1 = 7, n2 = 6; t = LCA(root, n1, n2); cout<<"Lowest Common Ancestor of 7 and 6 is:" << t->d<<endl; }

## Output

Lowest Common Ancestor of 20 and 4 is:9 Lowest Common Ancestor of 7 and 6 is:7

- Related Questions & Answers
- Lowest Common Ancestor of a Binary Search Tree in Python
- Lowest Common Ancestor of a Binary Tree in Python
- Program to find out the lowest common ancestor of a binary tree using Python
- Program to find out the lowest common ancestor of a binary tree using parent pointers using Python
- Program to find out the lowest common ancestor of a binary tree of given nodes using Python
- Program to find an ancestor which is common of two elements in a binary tree in Python
- Lowest Common Ancestor of Deepest Leaves in Python
- Program to find Kth ancestor of a tree node in Python
- Program to find Inorder Successor of a binary search tree in C++
- Binary Tree to Binary Search Tree Conversion in C++
- C++ Program to Search for an Element in a Binary Search Tree
- Python Program to Sort using a Binary Search Tree
- Program to find largest binary search subtree from a given tree in Python
- C++ Program to Find the Minimum value of Binary Search Tree
- C++ Program to Check Whether a Given Tree is Binary Search Tree

Advertisements