Skip to content

Commit

Permalink
Add the_nearest_common_ancestor_of_BT algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
theonlybigmiao committed Nov 9, 2024
1 parent 986c8de commit c9dbc07
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
This section describes the implementation of a function in C that verifies how to find the nearest common ancestor of a Binary Tree (BT).I can only offer the recursive way to implement it.If someone has non-recursive ideas,welcome to add.

## Problem Statement ##
Given a binary tree, find the nearest common ancestors of the two specified nodes in that tree.
For the two nodes p and q of the root tree T, the nearest common ancestor is represented as one node x, satisfying x is the ancestor of p and q and the depth of x is as large as possible.A node can also be its own ancestor.

## Solution ##
if root be p,q The most recent common ancestor is only possible in one of the following cases:
1. p and q are in root's subtree and on different sides;
2. p=root moreover q at root left or right subtree;
3. q=root moreover p at root left or right subtree;

#### *find_LCA*:

Consider performing a pre-order traversal of the binary tree by recursively when encountering nodes p or q. When nodes p,q are on the opposite sides of node root, the node root is the nearest common ancestor, it is returned upwards.

When the leaf node is crossed, it returns directly null; While the data of root is equal to p or q , go back directly root;

Then we can begin recursive works:

Turn on the recursive left child node, and the return value is denoted as left.

Turn on the recursive right child node, and the return value is denoted as right.

When left and right are also empty, this means the left and right subtrees of root don't contain p or q, return NULL. This situation can be combined with the situation where either side of the left and right subtree is empty.

When left or right is empty, this means nothing on the empty side. P,q are both on the opposite side, go to the opposite side.

When left and right are also not empty, which meaning p,q are on both sides of root,the root is what we want.






Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
int data;
struct Node* left;
struct Node* right;
}Node;

// Function to create a new node
Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}

//Recursive-search for LCA
Node* find_LCA(Node* root, Node* p, Node* q) {
if(root == NULL || root->data == p->data || root->data == q->data){
return root;
}
//Turn on the recursive left child node, the return value is denoted as left
Node *left = find_LCA(root->left, p, q);
//Turn on the recursive right child node, the return value is denoted as right
Node *right = find_LCA(root->right, p, q);
if(left == NULL){
//This means nothing on the left. P,q are both on the right, go right
return right;
}
//This means nothing on the right. P,q are both on the left, go left
if(right == NULL){
return left;
}
//Left and right are not empty, which meaning p,q are on both sides of root,the root is what we want
return root;
}


int main() {
Node* root = newNode(3);
root->left = newNode(5);
root->right = newNode(1);
root->left->left = newNode(6);
root->left->right = newNode(2);
root->right->left = newNode(0);
root->right->right = newNode(8);
root->left->right->left = newNode(7);
root->left->right->right = newNode(4);
Node* p=newNode(5);
Node* q=newNode(4);
Node* resultNode = find_LCA(root, p, q);
if (resultNode != NULL) {
printf("Node with value %d found.\n", resultNode->data);
} else {
printf("One or both nodes are not present in the tree\n");
}
return 0;
}

0 comments on commit c9dbc07

Please sign in to comment.