Problem Description
The Lowest Common Ancestor (LCA) is the problem of finding the most recent ancestor node of two nodes. This problem is very important in tree data structures and is a frequently asked topic in coding tests and interviews. In this course, we will cover how to find the LCA using JavaScript and examine the process of solving actual algorithm problems.
Problem Definition
Given a binary tree and two nodes A and B, write a function that finds and returns the lowest common ancestor of the two nodes.
Input Format
- The number of nodes is
N
, where1 ≤ N ≤ 10^4
. - Each node has a unique integer ID.
- The IDs of the two nodes
A
andB
are provided.
Output Format
- Returns the ID of the node representing the lowest common ancestor of the two nodes.
Examples
Example 1
Input: Tree Structure: 1 / \ 2 3 / \ / \ 4 5 6 7 A = 4, B = 5 Output: 2 // Lowest Common Ancestor: 2
Example 2
Input: Tree Structure: 1 / \ 2 3 / \ / \ 4 5 6 7 A = 6, B = 7 Output: 3 // Lowest Common Ancestor: 3
Problem Solving Process
1. Define Tree Node Structure
First, we need to define the node structure to represent a binary tree. Each node must have information about at least the parent and child nodes. In JavaScript, we can define the node structure as follows.
class TreeNode {
constructor(id) {
this.id = id; // Node's ID
this.left = null; // Left child node
this.right = null; // Right child node
}
}
2. Create the Tree
Let’s create the example tree from the problem. The code below shows how to construct the tree.
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);
3. Find the Lowest Common Ancestor
Now we will implement a function to actually find the LCA. The common method for finding the lowest common ancestor in a binary tree is to traverse the tree recursively and return the node when both nodes A and B are found.
function findLCA(root, A, B) {
if (root === null) {
return null;
}
// If the current node is A or B
if (root.id === A || root.id === B) {
return root;
}
const leftLCA = findLCA(root.left, A, B);
const rightLCA = findLCA(root.right, A, B);
// If LCA is found in both children, current node is LCA
if (leftLCA && rightLCA) {
return root;
}
return leftLCA !== null ? leftLCA : rightLCA;
}
4. Test the Function
Now let’s test the LCA function we wrote. You can call the function with the following code to output the result.
const A = 4;
const B = 5;
const lcaNode = findLCA(root, A, B);
console.log(`Lowest Common Ancestor: ${lcaNode.id}`); // Output: Lowest Common Ancestor: 2
Complexity Analysis
The time complexity of this algorithm is O(N), where N is the number of nodes. This is because we need to visit every node once. The space complexity is O(H), where H is the height of the tree. In the worst case, H can be close to N (when the tree is skewed).
Conclusion
In this course, we learned in detail about how to find the lowest common ancestor using JavaScript. This concept is a fundamental basis for solving various tree-related problems and will help in understanding tree traversal algorithms. Since it is frequently asked in coding tests and interviews, make sure to master it. In the next course, we will cover other tree-related problems, so stay tuned!