Given a binary tree, the task is to find the Lowest Common Ancestor of the given two nodes in the tree.
Let G be a tree then LCA of two nodes u and v is defined as the node w in the tree which is an ancestor of both u and v and is farthest from the root node.If one node is the ancestor of another one than that particular node is the LCA of those two nodes.
The LCA of 6 and 9 is 1.
The LCA of 5 and 9 is 1.
The LCA of 6 and 8 is 3.
The LCA of 6 and 1 is 1.
Approach: The article describes an approach known as Binary Lifting to find the Lowest Common Ancestor of two nodes in a tree. There can be many approaches to solve the LCA problem. We are discussing the Binary Lifting Technique, the others can be read from here and here.
Binary Lifting is a dynamic programming approach where we pre-compute an array memo[1, n][1, log(n)] where memo[i][j] contains 2^j-th ancestor of node i. For computing the values of memo, the following recursion may be used
memo[i][j] = parent[i] if j = 0 and
memo[i][j] = memo[memo[i][j – 1]][j – 1] if j > 0.
We first check whether a node is an ancestor of other or not and if one node is ancestor of other then it is the LCA of these two nodes otherwise we find a node which is not the common ancestor of both u and v and is highest(i.e. a node x such that x is not the common ancestor of u and v but memo[x] is) in the tree. After finding such a node (let it be x), we print the first ancestor of x i.e. memo[x] which will be the required LCA.
Below is the implementation of the above approach:
The LCA of 6 and 9 is 1 The LCA of 5 and 9 is 1 The LCA of 6 and 8 is 3 The LCA of 6 and 1 is 1
Time Complexity: The time taken in pre-processing is O(NlogN) and every query takes O(logN) time. So the overall time complexity of the solution is O(NlogN).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Kth ancestor of a node in an N-ary tree using Binary Lifting Technique
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Find LCA in Binary Tree using RMQ
- Query to find the maximum and minimum weight between two nodes in the given tree using LCA.
- Sqrt (or Square Root) Decomposition | Set 2 (LCA of Tree in O(sqrt(height)) time)
- LCA for n-ary Tree | Constant Query O(1)
- LCA for general or n-ary trees (Sparse Matrix DP approach )
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion using STL set
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
- Check whether a binary tree is a full binary tree or not
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check whether a given binary tree is skewed binary tree or not?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.