https://blog.youkuaiyun.com/mmwwxx123/article/details/81331893
在搜索二叉树中,左子结点的值总是小于父结点的值,右子结点的值总大于父结点的值,因此我们在转换成排序双向链表时,原先指向左子结点的指针调整为链表中前一个结点的指针,原先指向右子结点的指针调整为链表中指向后一个结点指针。
中序遍历二叉树有序,把中序遍历的结果链接成双向链表。
void convert(tree* root, tree **lastNode) {
if (root->left) {
convert(root->left,lastNode);
}
tree *curRoot = root;
curRoot->left = *lastNode;
if (*lastNode != NULL) {
(*lastNode)->right = curRoot;
}
*lastNode = curRoot;
if (root->right) {
convert(root->right,lastNode);
}
}
tree* convertTree(tree* root) {
if (NULL == root) {
return NULL;
}
tree *lastNode = NULL;
convert(root, &lastNode);
while (lastNode->left!=NULL) {
lastNode = lastNode->left;
}
return lastNode;
}