二叉树的前中后序非递归遍历

// 前序和中序比较简单,不做解释,后序遍历做了一定解释
#include <iostream>
#include <stack>
using namespace std;
struct TreeNode 
{
    int m_data;
    TreeNode * leftChild;
    TreeNode * rightChild;
    TreeNode(int data) : m_data(data), leftChild(NULL), rightChild(NULL) {}
};
 
void preOrderTravel(TreeNode * root)
{
    stack< TreeNode * > st;
    while( !st.empty() || root != NULL )
    {
        while( root )
        {
            st.push( root );
            cout << root->m_data << " ";
            root = root->leftChild;
        }
        root = st.top();
        st.pop();
        root = root->rightChild;
    }
}
void inOrderTravel(TreeNode * root)
{
     stack< TreeNode * > st;
     while( !st.empty() || root != NULL )
     {
         while( root )
         {
             st.push( root );
             root = root->leftChild;
         }
         root = st.top();
         st.pop();
         cout << root->m_data << " ";
         root = root->rightChild;
    }
}
// 后序遍历由于其有一定复杂性,需要构造一个辅助节点来
// 帮助,此节点会多2个标记,标记该结点是否已经遍历了左
// 右子树,可以输出该节点
struct Node 
{
    TreeNode * m_node;
    int leftTag;
    int rightTag;
 
    // 要是该节点有左结点,则该节点标记置1,否则置0
    Node(TreeNode * root)
    {
         m_node = root;
         leftTag = root->leftChild ? 1 : 0;
         rightTag = root->rightChild ? 1 : 0;
    }
    // 重载类型转换运算符,只是为了操作简便
    operator TreeNode* ()
    {
         return m_node;
    }
    // 重载->运算符,只是为了操作简便
    TreeNode * operator->()
    {
         return m_node;
    }
};
void postOrderTravel(TreeNode * root)
{
    if( root == NULL ) return;
    Node node = root;
    stack< Node > stn;
    while( true )
    {
         // 若此节点有左子树,且左子树尚未被遍历,则遍历
         // 这里不需要考虑 node 节点为空的情况,因为叶子
         // 节点的 leftTag/rightTag 已经置为0啦
         while( node.leftTag )
         {
             // 一旦遍历则直接令其为0,表示已经遍历
             node.leftTag = 0;
             stn.push( node );
             node = node->leftChild;
         }
         // 进入这一步,表示左节点已经到了最左,只需判断该
         // 节点是否有有右节点,因为这是后序遍历,根节点一定
         // 在左右遍历完之后才输出
         if( node.rightTag )
         {
             node.rightTag = 0;
             stn.push( node );
             node = node->rightChild;
         }
         else 
         {
             cout << node->m_data << " ";
             // 当最后一个节点输出之后,这里就可跳出
             if( stn.empty() ) break;
             node = stn.top();
             stn.pop();
         }
    }
}
int main()
{
     // 测试数据
     TreeNode * root  = new TreeNode( 1 );
     TreeNode * node1 = new TreeNode( 2 );
     TreeNode * node2 = new TreeNode( 3 );
     TreeNode * node3 = new TreeNode( 4 );
     TreeNode * node4 = new TreeNode( 5 );
     TreeNode * node5 = new TreeNode( 6 );
     TreeNode * node6 = new TreeNode( 7 );
     TreeNode * node7 = new TreeNode( 8 );
     root->leftChild   = node1;
     root->rightChild  = node4;
     node1->leftChild  = node2;
     node1->rightChild = node3;
     node4->leftChild  = node5;
     node4->rightChild = node6;
     node6->rightChild = node7;
     cout << "前序遍历结果:" << endl;
     preOrderTravel( root );
     cout << endl;
     cout << "中序遍历结果:" << endl;
     inOrderTravel( root );
     cout << endl;
     cout << "后序遍历结果:" << endl;
     postOrderTravel( root );
     cout << endl;
     return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 后序非递归遍历二叉树有两种方法。方法一是将后序遍历的访问顺序变成根右左,然后再逆置顺序。具体实现可以使用栈来辅助遍历,首先将根节点入栈,然后循环执行以下操作:如果栈不为空,取出栈顶节点,将其值加入结果数组,并将其右子节点入栈,再将其左子节点入栈。最后将结果数组逆置即可得到后序遍历的结果。方法二与遍历的方法类似,只是在访问节点时将节点值插入结果数组的头部,最后不需要逆置结果数组。具体实现可以使用栈来辅助遍历,首先将根节点入栈,然后循环执行以下操作:如果栈不为空,取出栈顶节点,将其值插入结果数组的头部,并将其左子节点入栈,再将其右子节点入栈。最后得到的结果数组就是后序遍历的结果。\[1\]\[3\] #### 引用[.reference_title] - *1* [如何使用非递归的方式后序遍历二叉树](https://blog.csdn.net/Sunny5106/article/details/119249405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【数据结构】二叉树非递归遍历](https://blog.csdn.net/qq_66314292/article/details/129038995)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值