Balanced binary tree into a doubly linked list sorted; the children beg their parents know how

//平衡二叉树转为排序的双向链表
void func(T*& tree,T* prev = nullptr) //这里传的相当于是二级指针,因为内部的修改将会影响外部根指针的结构
{
	if (tree == nullptr)
		return;
	func(tree->left);
	tree->left = prev;
	if (prev)
		prev->right = tree;
	prev = tree;
	func(tree->right);
}

After final completion, tree becomes a point balanced binary tree, a doubly linked list after the final conversion, the maximum junction.
Here Insert Picture Description
However, the maximum right pointer does not point to the node, so after func function completes, should make a right tree = nullptr






//知道孩子如何求双亲
Node* GetParent(Node* pRoot,Node* pNode)
{
	if(pRoot == nullptr || pNode == nullptr && pNode == pRoot)  //如果要求的结点刚好就是根节点的话,那么也是直接返回空
		return nullptr;
	if(pRoot->left == pNode || pRoot->right == pNode)
		return pRoot;
	Node* pRet = GetParent(pRoot->left,pNode);
	if(pRet)
		return pRet;
	Node* pRet = GetParent(pRoot->right,pNode);
	return pRet;
}
Published 230 original articles · won praise 28 · views 9294

Guess you like

Origin blog.csdn.net/weixin_43767691/article/details/103943552