java中bst
Problem statement:
问题陈述:
Given a Binary Search Tree and 2 nodes value n1 and n2, your task is to find the lowest common ancestor of the two nodes. Assume that n1 and n2 both existing node value of the tree.
给定二叉搜索树和2个节点的值n1和n2,您的任务是找到两个节点中最低的公共祖先。 假设n1和n2都是树的现有节点值。
Example:
例:
Input BST:
8
/ \
4 10
/ \ / \
3 5 9 11
n1: 9
n2: 11
Output:10
n1: 4
n2: 10
Output:8
Solution
解
The key idea of the solution is while traversing BST from root to bottom, the first node that is encountered with a value between n1 and n2, i.e., n1<node->data<n2, is the Least Common Ancestor(LCA) of those two nodes having values n1, n2 respectively.
该解决方案的关键思想是从根到底部遍历BST时,遇到的第一个节点的n1和n2之间的值(即n1 <node-> data <n2 )是这些节点的最小公共祖先(LCA) 。两个节点分别具有值n1 , n2 。
So, traverse the BST using pre-order traversal. If we find a node with value in between n1 and n2, the node is the LCA. If its (the currently processed node) value is greater than both n1 and n2, then the LCA lies in the left subtree of the currently processed node and if its (the currently processed node) value is smaller than both n1 and n2 then the LCA must be on the right subtree of the currently processed node.
因此,请使用顺序遍历遍历BST。 如果找到一个值在n1到n2之间的节点,则该节点为LCA。 如果其(当前处理的节点)值大于n1和n2 ,则LCA位于当前处理的节点的左子树中;如果其(当前处理的节点)值小于n1和n2,则LCA必须在当前已处理节点的右侧子树上。
Algorithm:
算法:
FUNCTION LCA(root, n1, n2)
While (true)
//if root->data lies between n1 and n2 both
IF ((root->data>=n1 && root->data<=n2)||(root->data<=n1 && root->data>=n2))
return root; //LCA found
IF(n1
data) //root->data > both n1 & n2
root=root->left; //go to left
Else//root->data < both n1 and n2
root=root->right; //go to right
END While
END FUNCTION
Example with explanation:
带有说明的示例:
Input BST:
8
/ \
4 10
/ \ / \
3 5 9 11
n1: 9
n2: 11
Nodes are represented by their respective values
In main we call LCA (8,9,11)
LCA(8,9,11)
root->data<n1 //8<9 ensures root->data smaller than both n1 & n2
root=root->right
LCA(10,9,11)
root->data>n1&& root->data<n2 //10>9&& 10<11 ensures root->data in between n1 & n2
return root//10
Program terminates..
10 is LCA
C++ implementation
C ++实现
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data; //value
Node *left; //pointer to left child
Node *right; //pointer to right child
};
// creating new node
Node* newnode(int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
Node* LCA(Node *root, int n1, int n2)
{
while(true){
if((root->data>=n1 && root->data<=n2)||(root->data<=n1 && root->data>=n2))
return root;
if(n1<root->data)
root=root->left;
else
root=root->right;
}
}
int main(){
cout<<"tree is built as per 1st example\n";
Node *root=newnode(8);
root->left= newnode(4);
root->right= newnode(10);
root->right->right=newnode(11);
root->right->left=newnode(9);
root->left->left=newnode(3);
root->left->right=newnode(5);
int n1=9, n2=11;
cout<<"Lowest common ancestor of "<<n1<<" & "<<n2;
cout<<" is:"<<LCA(root,n1,n2)->data<<endl;
return 0;
}
Output
输出量
tree is built as per 1st example
Lowest common ancestor of 9 & 11 is:10
翻译自: https://www.includehelp.com/icp/lowest-common-ancestor-in-a-bst.aspx
java中bst