二叉树的遍历算法之 非递归算法

二叉树的遍历算法之 非递归算法

原理

通过模拟栈来实现
在这里插入图片描述

#include <iostream>
#include <stack>
#include <utility>

struct BtreeNode{
    int data;
    BtreeNode* left;
    BtreeNode* right;
};

class Solution{
public:
	前序
    void inorder(BtreeNode* root){
        // 申请一个栈
        std::stack<std::pair<BtreeNode* , int>> st;
        // 将根节点入栈
        st.push(std::pair<BtreeNode* , int> {root , 0});
        while(!st.empty()){
            std::pair<BtreeNode* , int>temp = st.top();
            st.pop();
            // 如果该节点的左右节点都为nullptr 或者 int 那个数据为 1 的之间访问
            if(temp.first->left == nullptr && temp.first->right == nullptr)
                // 访问节点
                std::cout << temp.first->data<<" ";
            else if(temp.second == 1)
                // 访问根节点
                std::cout << temp.first->data<<" ";
            else{
                temp.second = 1;
                if(temp.first->right != nullptr)
                    st.push(std::pair<BtreeNode* , int> {temp.first->right , 0});
                if(temp.first->left != nullptr)
                    st.push(std::pair<BtreeNode* , int> {temp.first->left , 0}); 
                st.push(temp);
            }
        }
    }
	中序
	void inorder(BtreeNode* root){
        // 申请一个栈
        std::stack<std::pair<BtreeNode* , int>> st;
        // 将根节点入栈
        st.push(std::pair<BtreeNode* , int> {root , 0});
        while(!st.empty()){
            std::pair<BtreeNode* , int>temp = st.top();
            st.pop();
            // 如果该节点的左右节点都为nullptr 或者 int 那个数据为 1 的之间访问
            if(temp.first->left == nullptr && temp.first->right == nullptr)
                // 访问节点
                std::cout << temp.first->data<<" ";
            else if(temp.second == 1)
                // 访问根节点
                std::cout << temp.first->data<<" ";
            else{
                temp.second = 1;
                if(temp.first->right != nullptr)
                    st.push(std::pair<BtreeNode* , int> {temp.first->right , 0});
                st.push(temp);
                if(temp.first->left != nullptr)
                    st.push(std::pair<BtreeNode* , int> {temp.first->left , 0}); 
            }
        }
    }
	后序
	void inorder(BtreeNode* root){
        // 申请一个栈
        std::stack<std::pair<BtreeNode* , int>> st;
        // 将根节点入栈
        st.push(std::pair<BtreeNode* , int> {root , 0});
        while(!st.empty()){
            std::pair<BtreeNode* , int>temp = st.top();
            st.pop();
            // 如果该节点的左右节点都为nullptr 或者 int 那个数据为 1 的之间访问
            if(temp.first->left == nullptr && temp.first->right == nullptr)
                // 访问节点
                std::cout << temp.first->data<<" ";
            else if(temp.second == 1)
                // 访问根节点
                std::cout << temp.first->data<<" ";
            else{
                temp.second = 1;
                st.push(temp);
                if(temp.first->right != nullptr)
                    st.push(std::pair<BtreeNode* , int> {temp.first->right , 0});
                if(temp.first->left != nullptr)
                    st.push(std::pair<BtreeNode* , int> {temp.first->left , 0}); 
            }
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值