Skip to content

Commit 5604e49

Browse files
committed
Added problem lowest common ancestor of a binary tree
1 parent 7297039 commit 5604e49

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ Include contains single header implementation of data structures and some algori
111111
|Recursive Level order traveral of Tree | [levelOrderTraversalRecursive.cpp](tree_problems/levelOrderTraversalRecursive.cpp)|
112112
|ZigZag Traversal of Tree | [zigZagTraversal.cpp](tree_problems/zigZagTraversal.cpp)|
113113
|Predecessor and Successor of a given node in Binary Search Tree | [predecessorSuccessor.cpp](tree_problems/predecessorSuccessor.cpp)|
114-
|Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree.| [lowest-common-ancestor.cpp](tree_problems/lowest-common-ancestor.cpp)|
114+
|Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree.| [lowest-common-ancestor.cpp]
115+
(tree_problems/lowest-common-ancestor.cpp)|
116+
|Given a binary tree (unlike binary search tree), find the Lowest Common Ancestor (LCA).|[lowest-common-ancestor-binary-tree.cpp](tree_problems/lowest-common-ancestor-binary-tree.cpp)|
115117
|Given a binary tree, print out all of its root-to-leaf paths one per line.| [printAllRootToLeafPath.cpp](tree_problems/printAllRootToLeafPath.cpp)
116118
|Determine if a tree is sum tree. A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.| [sumTree.cpp](tree_problems/sumTree.cpp)|
117119
|Convert a tree to sumTree, such that each node is sum of left and right subtree of the original tree.| [convert_to_sum_tree.cpp](tree_problems/convert_to_sum_tree.cpp)|
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* We did lowest common ancestor of a binary SEARCH tree in a different problem.
3+
* This will provide lowest common ancestor of a binary tree.
4+
* Example
5+
* 20
6+
* / \
7+
* 24 22
8+
* / \
9+
* 25 12
10+
* / \
11+
* 10 14
12+
*
13+
* In the above tree, LCA of 10 and 14 is 12
14+
* similarly 14 and 25 will have LCA 24
15+
* LCA of 10 and 22 is 20.
16+
*/
17+
18+
#include <iostream>
19+
20+
struct TreeNode {
21+
int data;
22+
TreeNode* left;
23+
TreeNode* right;
24+
TreeNode(int d):
25+
data{ d }, left{ nullptr }, right{ nullptr }{ }
26+
};
27+
28+
void inorder(TreeNode * root)
29+
{
30+
if (root) {
31+
inorder(root->left);
32+
std::cout << root->data << " ";
33+
inorder(root->right);
34+
}
35+
}
36+
37+
TreeNode* lowest_common_ancestor(TreeNode* root, TreeNode* p, TreeNode* q)
38+
{
39+
if (root == nullptr || p == root || q == root) {
40+
return root;
41+
}
42+
43+
TreeNode* leftLCA = lowest_common_ancestor(root->left, p, q);
44+
TreeNode* rightLCA = lowest_common_ancestor(root->right, p, q);
45+
return (!leftLCA ? rightLCA : !rightLCA ? leftLCA : root);
46+
}
47+
48+
int main()
49+
{
50+
// Lets create the tree in example
51+
52+
TreeNode* root = new TreeNode(20);
53+
root->left = new TreeNode(24);
54+
root->right = new TreeNode(22);
55+
root->left->left = new TreeNode(25);
56+
root->left->right = new TreeNode(12);
57+
root->left->right->left = new TreeNode(10);
58+
root->left->right->right = new TreeNode(14);
59+
60+
std::cout << "Inorder Traversal of the tree:" << std::endl;
61+
inorder(root);
62+
std::cout << std::endl;
63+
64+
std::cout << "The LCA of nodes with values 10 and 14 is:"
65+
<< lowest_common_ancestor(root, root->left->right->left, root->left->right->right)->data
66+
<< std::endl;
67+
68+
std::cout << "The LCA of nodes with values 25 and 14 is:"
69+
<< lowest_common_ancestor(root, root->left->left, root->left->right->right)->data
70+
<< std::endl;
71+
72+
std::cout << "The LCA of nodes with values 10 and 22 is:"
73+
<< lowest_common_ancestor(root, root->left->right->left, root->right)->data
74+
<< std::endl;
75+
return 0;
76+
}

0 commit comments

Comments
 (0)