二叉树的递归遍历和非递归遍历的实现

struct BinaryTreeNode
{
    int m_nValue;
    BinaryTreeNode *m_pLeft;
    BinaryTreeNode *m_pRight;
};

//递归实现二叉树的遍历。递归算法比较简洁易懂这一就不做解释

void Preorder(BinaryTreeNode *pRoot)
{
    if (pRoot == NULL)
    {
        return;
    }
    cout<<pRoot->m_nValue<<endl;
    Preorder(pRoot->m_pLeft);
    Preorder(pRoot->m_pRight);
}

void Inorder(BinaryTreeNode *pRoot)
{
    if (pRoot == NULL)
    {
        return;
    }
    Inorder(pRoot->m_pLeft);
    cout<<pRoot->m_nValue<<endl;
    Inorder(pRoot->m_pRight);
}

void Postorder(BinaryTreeNode *pRoot)
{
    if (pRoot == NULL)
    {
        return;
    }
    Postorder(pRoot->m_pLeft);
    Postorder(pRoot->m_pRight);
    cout<<pRoot->m_nValue<<endl;
}

//非递归遍历的实现。。。。。。。。。未完待续

生命在于折腾,生活就是如此的丰富多彩
原文地址:https://www.cnblogs.com/Mr-Zhong/p/4120363.html