二叉搜索树与双向链表

void convertNode(BSTreeNode *root, BSTreeNode ** pLastNodeInList)
{
    if(!root)
        return ;
    if(root->left)
    {
        convertNode(root->left, pLastNodeInList);
    }
    root->left = *pLastNodeInList;
    if(*pLastNodeInList != NULL)
        (*pLastNodeInList)->right = root;

    *pLastNodeInList = root;

    if(root->right)
    {
        convertNode(root->right, pLastNodeInList);
    }
}

BSTreeNode *convert(BSTreeNode* root)//true = right, false = left
{
    if(!root)
        return root;
    BSTreeNode* pLastNodeInList = NULL;
    convertNode(root, &pLastNodeInList);

    while(pLastNodeInList && pLastNodeInList->left)
    {
        pLastNodeInList = pLastNodeInList->left;
    }

    return pLastNodeInList;
}

输入一个二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。不能创建任何新的结点,只能调整树中结点指针的指向。

posted @ 2016-08-14 22:10  genidong  阅读(133)  评论(0编辑  收藏  举报