【二叉樹】中序遍歷二叉樹

//遞歸中序遍歷二叉樹
void InOrder1(BinTree *root)
{
    if(root != NULL)
    {
        InOrder1(root->lchild);
        printf("%c",root->data);
        InOrder1(root->rchild);
    }
}

//非遞歸中序遍歷二叉樹 --- 用棧實現
void InOrder2(BinTree *root)
{
    stack<BinTree *> s;

    while(root != NULL || !s.empty())
    {
        if(root != NULL)
        {
            s.push(root);
            root = root->lchild;
        }
        else
        {
            root = s.pop();
            printf("%c",root->data);
            root = root->rchild;
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章