二叉树的遍历(递归&非递归)

#include <iostream>
#include <vector>
#include <stack>
using namespace std;
//tree
class TreeNode{
    public:
        TreeNode(int data){
            value = data;
            left = NULL;
            right = NULL;
        }
    public:
        int value;
        TreeNode* left;
        TreeNode* right;
};

/*
class Solution{
    public: int sum(int i, int k){
        return i+k;
    }
};*/
void print(TreeNode* head){
    cout << head->value << " ";
}
//第一个是先序,第二个是中序,第三个是后序
void readorder(TreeNode* head){
    if (head == NULL)
        return;
    //print(head);
    readorder(head->left);
    //print(head);
    readorder(head->right);
    print(head);
}
///***********************************************/unrecur
void preOrderUnrecur(TreeNode* head){
    if (head != NULL){
        stack<TreeNode*> s;
        s.push(head);
        while (!s.empty()){
            head = s.top();
            s.pop();
            print(head);
            if (head->right != NULL){
                s.push(head->right);
            }
            if (head->left != NULL){
                s.push(head->left);
            }
        }
    }
    cout << endl;
    return;
}
//当前节点先把左边界压进去,找到最左边
//当前节点为空,从站拿一个打印,往右跑
//当前节点不为空,当前节点压入,往左跑
void inOrderUnrecur(TreeNode* head)
{
    if (head != NULL){
        stack<TreeNode*> s;
        while (!s.empty() || head != NULL){
            if (head != NULL){
                s.push(head);
                head = head->left;
            }
            else{
                head = s.top();
                s.pop();
                print(head);
                head = head->right;
            }
        }
    }
    return;
}

//先中右左,将打印的信息压入s2,然后出站左右中
void posOrderUnrecur(TreeNode* head){
    if (head != NULL){
        stack<TreeNode*> s1;
        stack<TreeNode*> s2;
        s1.push(head);
        while (!s1.empty()){
            head = s1.top();
            s1.pop();
            s2.push(head);
            if (head->left != NULL){
                s1.push(head->left);
            }
            if (head->right != NULL){
                s1.push(head->right);
            }
        }
        while (!s2.empty()){
            print(s2.top());
            s2.pop();
        }
    }
    return;
}

//arr creat tree
void creatTree(TreeNode** head, int arr[], int len, int index = 0){
    if (index > len - 1)
        return;
    (*head) = new TreeNode(arr[index]);
    creatTree(&((*head)->left), arr, len, 2 * index + 1);
    creatTree(&((*head)->right), arr, len, 2 * index + 2);
}

int main(){
    int arr[] = {1,2,3,4,5};
    TreeNode* head = NULL;
    creatTree(&head, arr, 5);
    readorder(head);
    //preOrderUnrecur(head);
    //inOrderUnrecur(head);
    posOrderUnrecur(head);
    //system();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值