二叉树遍历(递归+非递归+层序)看这个一个就够了

#include<iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left,*right;
    TreeNode(int x):val(x),left(NULL),right(NULL){}
};
int nums[13]={1,3,5,-1,-1,6,-1,-1,4,7,-1,-1,-1};

TreeNode* Creat()
{
    static  int index=0;
    int n=nums[index++];
    if(n==-1)
        return  NULL;
    TreeNode *root=new TreeNode(n);
    root->left=Creat();
    root->right=Creat();
    return root;

}

void PreVisitCur(TreeNode* r)
{
    if(r)
    {
        cout<<r->val<<" ";
        PreVisitCur(r->left);
        PreVisitCur(r->right);
    }
}
void MidVisitCur(TreeNode *r)
{
    if(r)
    {
        MidVisitCur(r->left);
        cout<<r->val<<" ";
        MidVisitCur(r->right);
    }
}
void PostVisitCur(TreeNode *r)
{
    if(r)
    {
        PostVisitCur(r->left);
        PostVisitCur(r->right);
        cout<<r->val<<" ";
    }
}
void PreVisitNoCur(TreeNode *r)
{
    if(!r)
        return;
    stack<TreeNode*>s;
    s.push(r);
    TreeNode *p=NULL;
    while(!s.empty())
    {
        p=s.top();
        s.pop();
        if(p)
        {
            cout<<p->val<<" ";
            s.push(p->right);
            s.push(p->left);
        }
    }
}
void MidVisitNoCur(TreeNode *r)
{
    stack<TreeNode*>s;
    if(!r)
        return;
    TreeNode *p=r;
    while(p!=NULL||!s.empty())
    {
        while(p)
        {
            s.push(p);
            p=p->left;
        }
        if(!s.empty())
        {
            p=s.top();
            s.pop();
            cout<<p->val<<" ";
            p=p->right;
        }
    }
}
void PostVisitNoCur(TreeNode *r)
{
    if(!r)
        return;
    stack<TreeNode*>s;
    TreeNode *p=NULL,*pre=NULL;
    s.push(r);
    while(!s.empty())
    {
        p=s.top();
        if((!p->left&&!p->right)||(!p->right&&pre==p->left)||pre==p->right)
        {
            cout<<p->val<<" ";
            pre=p;
            s.pop();
        }
        else
        {
            if(p->right)
                s.push(p->right);
            if(p->left)
                s.push(p->left);
        }
    }
}
void LayerVisit(TreeNode *r)
{
    queue<TreeNode*>que;
    if(!r)
        return;
    que.push(r);
    TreeNode *p;
    while (!que.empty())
    {
        p=que.front();
        cout<<p->val<<" ";
        que.pop();
        if(p->left!=NULL)
            que.push(p->left);
        if(p->right!=NULL)
            que.push(p->right);
    }
}
int main(int argc, char const *argv[])
{
    TreeNode *root=Creat();
    cout<<endl<<"LayerVisit"<<endl;
    LayerVisit(root);
    cout<<endl<<"PreVisitCur"<<endl;
    PreVisitCur(root);
    cout<<endl<<"PreVisitNoCur"<<endl;
    PreVisitNoCur(root);
    cout<<endl<<"MidVisitCur"<<endl;
    MidVisitCur(root);
    cout<<endl<<"MidVisirNoCur"<<endl;
    MidVisitNoCur(root);
    cout<<endl<<"PostVisitCur"<<endl;
    PostVisitCur(root);
    cout<<endl<<"PostVisitNoCur"<<endl;
    PostVisitNoCur(root);
    return 0;
}

输出结果:

F:\Leetcode\cmake-build-debug\Leetcode.exe

LayerVisit
1 3 4 5 6 7
PreVisitCur
1 3 5 6 4 7
PreVisitNoCur
1 3 5 6 4 7
MidVisitCur
5 3 6 1 7 4
MidVisirNoCur
5 3 6 1 7 4
PostVisitCur
5 6 3 7 4 1
PostVisitNoCur
5 6 3 7 4 1
Process finished with exit code 0

    原文作者:kellen_f
    原文地址: https://blog.csdn.net/kellen_f/article/details/85029170
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞