查找二叉搜索树中小于某个节点的最大值

Node* rightMost(Node *n)
{
	if (n->right == NULL)
	{
		return n;
	}
	while (n->right)
	{
		n = n->right;
	}

	return n;
}

Node *predecessor(Node *n)
{
	if (NULL == n)
	{
		return NULL;
	}

	if (n->left)
	{
		return rightMost(n->left);
	}

	Node *parent = n->parent;
	if (parent && parent->right != n)
	{
		n = parent;
		parent = parent->parent;
	}

	return parent;
}

你可能感兴趣的:(查找二叉搜索树中小于某个节点的最大值)