Binary Search Tree (6) and Binary Search Tree
Given two nodes in the binary search tree, find their closest Common Ancestor (Lowest Common Ancestor-LCA ).
Before giving a detailed introduction, you can refer to the next article "binary tree (70)-recent common ancestor [1]".
The function prototype is defined as follows:
Node * getLCA (Node * root, int n1, int n2) Where n1 and n2 are the specified two Node values.
For example, in the above BST, 10 and 14 had 12, while 8 and 14 had 8.
The following is the definition of LCA from Wikipedia:
Assume that a binary tree T exists. The minimum common ancestor of node n1 and n2 is defined as the smallest node in node T, it contains n1 and n2 as future generations (a node can also be its own descendant ).
Node n1 and n2 are the same ancestor and are the farthest from the root node. It is very useful for the calculation of LCA. For example, we can calculate the distance between a pair of nodes in the tree: the distance from n1 to n2 is the distance from n1 to root, plus the root distance from n2, then, subtract two times the distance from their LCA to the root.
Solution:
You can use the features of BST. Recursively traverse data from the root node. The main idea of this method is that when we traverse from top to buttom, we first encounter node n, and n satisfies n1 <n <n2, or n is equal to n1 or n2, then n is the LCA of n1 and n2. Install this method for Recursive traversal. When the node values are larger than n1 and n2, the LCA is located in the left subtree of the node. If the node values are less than n1 and n2, then, the LCA is located in the right subtree of the node. Otherwise, the root is the LCA.
// C ++ program, calculate the LCA # include <iostream> struct Node {int key; Node * left; Node * right;} of the two BST nodes ;}; // calculate the LCA of n1 and n2. Assume that both n1 and n2 are in BST. Node * getLCA (Node * root, int n1, int n2) {if (root = NULL) return NULL; // if n1 and n2 are less than root, in the left subtree, if (root-> key> n1 & root-> key> n2) return getLCA (root-> left, n1, n2 ); // if n1 and n2 are greater than root, if (root-> key <n1 & root-> key <n2) return getLCA (root-> right, n1, n2); return root;} // create a new BST Node * createNewNode (int item) {Node * temp = new Node; temp-> key = item; temp-> left = temp-> right = NULL; return temp;} int main () {/* 20/\ 8 22/\ 4 12/\ 10 14 */Node * root = createNewNode (20); root-> left = createNewNode (8 ); root-> right = createNewNode (22); root-> left = createNewNode (4); root-> left-> right = createNewNode (12 ); root-> left-> right-> left = createNewNode (10); root-> left-> right = createNewNode (14); int n1 = 10, n2 = 14; Node * t = getLCA (root, n1, n2); printf ("LCA of % d and % d is % d \ n", n1, n2, t-> key); n1 = 14, n2 = 8; t = getLCA (root, n1, n2 ); printf ("LCA of % d and % d is % d \ n", n1, n2, t-> key); n1 = 10, n2 = 22; t = getLCA (root, n1, n2); printf ("LCA of % d and % d is % d \ n", n1, n2, t-> key ); return 0 ;}Output:
LCA of 10 and 14 is 12
LCA of 14 and 8 is 8
LCA of 10 and 22 is 20
Time Complexity: O (h), where h is the height of the tree.
In addition, the above program also requires additional memory for recursive function call stacks. You can use the following Traversal method to avoid extra memory space.
// Obtain the LCANode * getLCA (Node * root, int n1, int n2) {while (root! = NULL) {// if n1 and n2 are less than root, then LCA is located in the left subtree if (root-> data> n1 & root-> data> n2) root = root-> left; // If n1 and n2 are greater than root, then, in the right subtree, else if (root-> data <n1 & root-> data <n2) root = root-> right; else break;} return root ;}