昨天想了好久还是没有想明白,今天终于想明白了。其实我们只需要中序遍历一下二叉树,同时保留当前链表的最后一个指针pLast,以及当前指针pCurrent,每访问一个节点更新一下两者的值。还是需要充分理解中序遍历的含义。
struct Node{
int data;
Node* left,*right;
};
void transfer(Node* root,Node** pLast){
if(root==NULL) return;
Node* pCurrent=root;
if(pCurrent->left!=NULL)
transfer(pCurrent->left,pLast);
pCurrent->left=*pLast;
if(*pLast!=NULL) (*pLast)->right=pCurrent;
*pLast=pCurrent;
if(pCurrent->right!=NULL)
transfer(pCurrent->right,pLast);
}