[Algorithm Interview Questions] search for the nearest common ancestor node of the two nodes in the binary search tree
Http://geeksforgeeks.org /? P = 1029
Given the value of any two nodes in a binary search tree, you need to write a C/C ++ program to find the closest common ancestor of the two nodes, you can assume that the given value exists in a node of a binary tree.
Function declaration:
Int findlowestcommonancestor (node * root, int value1, int value)
Input: 4 and 14
Output: 8
(The common ancestor of 4 and 14 has {8, 20}, of which 8 is the closest public ancestor node)
Algorithm:
The basic idea is: given the two nodes N1 and N2 in the binary tree (assuming N1 <N2), the value n of the recent common ancestor node should meet the requirements of N1 <n <N2, therefore, we can traverse the Binary Search Tree in the forward order. When we find that the value of a node is between N1 and N2, this node is the requested node. If the node value is greater than N1 and N2, the requested node is in the left subtree of the current node; otherwise, the node is in the right subtree. (The algorithm is very simple. The key to solving the tree is to make good use of the first and then sequential traversal, which makes it difficult again, huh, huh)
#include <stdio.h>
#include <stdlib.h>
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int );
/* Function to find least comman ancestor of n1 and n2 */
int leastCommanAncestor(struct node* root, int n1, int n2)
{
/* If we have reached a leaf node then LCA doesn't exist
If root->data is equal to any of the inputs then input is
not valid. For example 20, 22 in the given figure */
if(root == NULL || root->data == n1 || root->data == n2)
return -1;
/* If any of the input nodes is child of the current node
we have reached the LCA. For example, in the above figure
if we want to calculate LCA of 12 and 14, recursion should
terminate when we reach 8*/
if((root->right != NULL) &&
(root->right->data == n1 || root->right->data == n2))
return root->data;
if((root->left != NULL) &&
(root->left->data == n1 || root->left->data == n2))
return root->data;
if(root->data > n1 && root->data < n2)
return root->data;
if(root->data > n1 && root->data > n2)
return leastCommanAncestor(root->left, n1, n2);
if(root->data < n1 && root->data < n2)
return leastCommanAncestor(root->right, n1, n2);
}
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Driver program to test mirror() */
int main()
{
struct node *root = newNode(2);
root->left = newNode(1);
root->ight = newNode(4);
root->right->left = newNode(3);
root->right->right = newNode(5);
/* Constructed binary search tree is
2
/ \
1 4
/ \
3 5
*/
printf("\n The Least Common Ancestor is \n");
printf("%d", leastCommanAncestor(root, 3, 5));
getchar();
return 0;
}
SELF: http://blog.csdn.net/wangzhi0417/archive/2010/08/11/5803231.aspx