二叉搜索树转双向链表

 

void BTreeToDoubleLinkList(Node *pRoot, Node *&pFirst, Node *&pLast)
{
    if (!pRoot)
    {
        pFirst = NULL;
        pLast = NULL;
        return;
    }

    Node *pFirstLeft = NULL;
    Node *pLastLeft = NULL;
    Node *pFirstRight = NULL;
    Node *pLastRight = NULL;

    if (!pRoot->pLeft)
        pFirst = pRoot;
    else {
        BTreeToDoubleLinkList(pRoot->pLeft, pFirstLeft, pLastLeft);
        pFirst = pFirstLeft;
        pRoot->pLeft = pLastLeft;
        pLastLeft->pRight = pRoot;
    }

    if (!pRoot->pRight)
        pLast = pRoot;
    else {
        BTreeToDoubleLinkList(pRoot->pRight, pFirstRight, pLastRight);
        pLast = pLastRight;
        pRoot->pRight = pFirstLeft;
        pFirstRight->pLeft = pRoot;
    }
}

 

 

 

 

 

 

 

EOF

posted on 2012-12-07 15:35  kkmm  阅读(252)  评论(0编辑  收藏  举报