1、二叉树是个搜索二叉树


2、二叉树带有指向parent的指针

    可转换成两个链表的相交节点


3、普通二叉树

保存从根节点分别到这两个节点的路径到list1和list2中

从list1和list2中找第一个不相等的节点即为最近公共祖先节点

template
BinaryTreeNode*  BinaryTree::lastCommnParent(BinaryTreeNode*& node1, BinaryTreeNode*& node2)
{
	if (_root == NULL || node1 == NULL || node2 == NULL)return NULL;
	std::list*> list1, list2;
	GetNodePath(node1, list1);
	GetNodePath(node2, list2);
	BinaryTreeNode* ret = _root;
	std::list*>::iterator it1 = list1.begin();
	std::list*>::iterator it2 = list2.begin();
	while (it1 != list1.end() && it2 != list2.end()){
		if (*it1 == *it2){
			ret = *it1;
			++it1, ++it2;
		}
		else
			break;
	}
	return ret;
}

template
void BinaryTree::GetNodePath(BinaryTreeNode*& node, std::list*>& listpath)
{
	if (node == NULL)return;
	BinaryTreeNode* cur = _root;
	BinaryTreeNode* prev = cur;
	listpath.push_back(cur);
	cur = cur->_leftchild;
	while (cur != NULL || !listpath.empty()){
		while (cur != NULL){
			listpath.push_back(cur);
			if (cur == node)return;
			cur = cur->_leftchild;
			prev = cur;
		}
		BinaryTreeNode* top = listpath.back();
		if (top->_rightchild == NULL || top->_rightchild == prev){
			prev = top;
			listpath.pop_back();
		}
		else
			cur = top->_rightchild;
	}
}



《完》