Find the nearest common ancestor of any two nodes in a binary search tree
2011-06-20 15:16
507 查看
自己写的,贴出来show一下,接受拍砖.
1. creat binary tree
static void CreateBinaryTree(int i, int? isOut = null)
{
if (binaryRootNode == null)
{
BinaryNode node = new BinaryNode();
node.data = i;
}
else
{
BinaryNode currentNode = binaryRootNode;
BinaryNode addNode = new BinaryNode();
while (true)
{
if (i < currentNode.data)
{
if (currentNode.left == null)
{
addNode.data = i;
addNode.parent = currentNode;
currentNode.left = addNode;
break;
}
else
{
currentNode = currentNode.left;
}
}
else if (i > currentNode.data)
{
if (currentNode.right == null)
{
addNode.data = i;
addNode.parent = currentNode;
currentNode.right = addNode;
break;
}
else
{
currentNode = currentNode.right;
}
}
else
{
break;
}
}
if (isOut != null)
{
findedNode = addNode;
}
}
}
2.get the nearest Common Ancestor
static int GetNearestCommonAncestor(BinaryNode childNode1, BinaryNode childNode2)
{
BinaryNode currentNode = binaryRootNode;//root node
if ((childNode1.data < currentNode.data && childNode2.data > currentNode.data) || (childNode1.data > currentNode.data && childNode2.data < currentNode.data))
{
return currentNode.data;
}
else if (childNode1.data == currentNode.data || childNode2.data == currentNode.data)
{
return currentNode.data; //in this situation, I think there is not exist ancestor
}
//get ancestor for childNode1
List<int> ancestorListForChildNode1 = new List<int>();
int rootData=currentNode.data;
currentNode = childNode1;
while (rootData != currentNode.data)
{
if (currentNode.parent.data > currentNode.data)
{
ancestorListForChildNode1.Add(currentNode.parent.data);
}
else
{
ancestorListForChildNode1.Insert(0, currentNode.parent.data);
}
currentNode = currentNode.parent;
}
//find ancerstor for childNode2 and check the parent node if exist in the ancestorListForChildNode1
currentNode = childNode2;
while (rootData != currentNode.data)
{
int low = 0;
int high = ancestorListForChildNode1.Count;
int tempParent = currentNode.parent.data;
while (low < high)
{
int mid=(low + high) / 2;
if (ancestorListForChildNode1[mid] > tempParent)
{
high = (low + high) / 2;
}
else if (ancestorListForChildNode1[mid] < tempParent)
{
low = (low + high) / 2;
}
else
{
return tempParent;
}
if (mid == 0 || mid == ancestorListForChildNode1.Count - 1)
{
break;
}
}
currentNode = currentNode.parent;
}
return rootData;
}
3. main method
static
void Main(string[] args)
{
binaryRootNode.data = 30;
for (int
i = 2; i < 60; i += 4)
{
CreateBinaryTree(i);
}
CreateBinaryTree(12);
CreateBinaryTree(11);
CreateBinaryTree(13,25);
BinaryNode
childNode1 = findedNode;
CreateBinaryTree(25, 3);
BinaryNode
childNode2 = findedNode;
int
nAncestor= GetNearestCommonAncestor(childNode1, childNode2);
}
1. creat binary tree
static void CreateBinaryTree(int i, int? isOut = null)
{
if (binaryRootNode == null)
{
BinaryNode node = new BinaryNode();
node.data = i;
}
else
{
BinaryNode currentNode = binaryRootNode;
BinaryNode addNode = new BinaryNode();
while (true)
{
if (i < currentNode.data)
{
if (currentNode.left == null)
{
addNode.data = i;
addNode.parent = currentNode;
currentNode.left = addNode;
break;
}
else
{
currentNode = currentNode.left;
}
}
else if (i > currentNode.data)
{
if (currentNode.right == null)
{
addNode.data = i;
addNode.parent = currentNode;
currentNode.right = addNode;
break;
}
else
{
currentNode = currentNode.right;
}
}
else
{
break;
}
}
if (isOut != null)
{
findedNode = addNode;
}
}
}
2.get the nearest Common Ancestor
static int GetNearestCommonAncestor(BinaryNode childNode1, BinaryNode childNode2)
{
BinaryNode currentNode = binaryRootNode;//root node
if ((childNode1.data < currentNode.data && childNode2.data > currentNode.data) || (childNode1.data > currentNode.data && childNode2.data < currentNode.data))
{
return currentNode.data;
}
else if (childNode1.data == currentNode.data || childNode2.data == currentNode.data)
{
return currentNode.data; //in this situation, I think there is not exist ancestor
}
//get ancestor for childNode1
List<int> ancestorListForChildNode1 = new List<int>();
int rootData=currentNode.data;
currentNode = childNode1;
while (rootData != currentNode.data)
{
if (currentNode.parent.data > currentNode.data)
{
ancestorListForChildNode1.Add(currentNode.parent.data);
}
else
{
ancestorListForChildNode1.Insert(0, currentNode.parent.data);
}
currentNode = currentNode.parent;
}
//find ancerstor for childNode2 and check the parent node if exist in the ancestorListForChildNode1
currentNode = childNode2;
while (rootData != currentNode.data)
{
int low = 0;
int high = ancestorListForChildNode1.Count;
int tempParent = currentNode.parent.data;
while (low < high)
{
int mid=(low + high) / 2;
if (ancestorListForChildNode1[mid] > tempParent)
{
high = (low + high) / 2;
}
else if (ancestorListForChildNode1[mid] < tempParent)
{
low = (low + high) / 2;
}
else
{
return tempParent;
}
if (mid == 0 || mid == ancestorListForChildNode1.Count - 1)
{
break;
}
}
currentNode = currentNode.parent;
}
return rootData;
}
3. main method
static
void Main(string[] args)
{
binaryRootNode.data = 30;
for (int
i = 2; i < 60; i += 4)
{
CreateBinaryTree(i);
}
CreateBinaryTree(12);
CreateBinaryTree(11);
CreateBinaryTree(13,25);
BinaryNode
childNode1 = findedNode;
CreateBinaryTree(25, 3);
BinaryNode
childNode2 = findedNode;
int
nAncestor= GetNearestCommonAncestor(childNode1, childNode2);
}
相关文章推荐
- Find the common ancestor of two nodes in a binary tree
- Lowest Common Ancestor of The Two Nodes in a Binary Tree
- **Lowest Common Ancestor of Two Nodes in a Binary Tree
- Microsoft Interview: In binary tree, find two nodes' nearest common father
- Lowest Common Ancestor of Two Nodes in a Binary Tree
- ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
- [geeks]Two of the nodes of a Binary Search Tree (BST) are swapped. Fix (or correct) the BST
- leetcode 235 Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree
- LeetCode-Lowest Common Ancestor of a Binary Search Tree
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree
- LeetCode 235 Lowest Common Ancestor of a Binary Search Tree
- [CTCI] 4.6 First Common Ancestor for Two Nodes On a Binary tree
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- leetcode--Lowest Common Ancestor of a Binary Search Tree
- 235. Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor (LCA) of two nodes in graph
- Lowest Common Ancestor of a Binary Search Tree
- Lowest Common Ancestor of a Binary Search Tree