zoukankan      html  css  js  c++  java
  • 二叉树的递归遍历和非递归遍历的实现

    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
Copyright © 2011-2022 走看看