zoukankan      html  css  js  c++  java
  • L2-006. 树的遍历

    给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

    输入格式:

    输入第一行给出一个正整数N(<=30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

    输出格式:

    在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

    输入样例:
    7
    2 3 1 5 7 6 4
    1 2 3 4 5 6 7
    
    输出样例:
    4 1 6 3 5 7 2



    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <queue>
    
    using namespace std;
    int z[30],h[30];
    class Tree
    {
    public:
        int data;
        Tree *lchild;
        Tree *rchild;
        Tree()
        {
            lchild=NULL;
            rchild=NULL;
        }
    }*root;
    Tree *CreatNode()
    {
        Tree *root=new Tree();
        return root;
    }
    Tree *RestoreTree(int z1,int z2,int h1,int h2)
    {
        Tree *root=CreatNode();
        root->data=h[h2];
        for(int i=0;z2-i>=z1;i++)
        {
            if(z[z2-i]==h[h2])
            {
                if(i>0)root->rchild=RestoreTree(z2-i+1,z2,h2-i,h2-1);
                if(z2-i>z1)root->lchild=RestoreTree(z1,z2-i-1,h1,h2-i-1);
                break;
            }
        }
        return root;
    }
    
    void levelOrder(Tree *root)
    {
        queue<Tree *>q;
        q.push(root);
        cout<<root->data;
        while(!q.empty())
        {
            if(q.front()->lchild!=NULL)
            {
                cout<<' '<<q.front()->lchild->data;
                q.push(q.front()->lchild);
            }
            if(q.front()->rchild!=NULL)
            {
                cout<<' '<<q.front()->rchild->data;
                q.push(q.front()->rchild);
            }
            q.pop();
        }
    }
    int main()
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>h[i];
        for(int i=0;i<n;i++)
            cin>>z[i];
        root=RestoreTree(0,n-1,0,n-1);
        levelOrder(root);
        return 0;
    }

     数组实现:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    struct tree {
        int d,l,r;
        tree() {
            l = r = 0;
        }
    }t[31];
    int n,pos;
    int in[30],post[30];
    int build(int inl,int inr,int postl,int postr) {
        int temp;
        t[temp = ++ pos].d = post[postr];
        for(int i = inl;i <= inr;i ++) {
            if(in[i] == post[postr]) {
                if(i != inl) t[temp].l = build(inl,i - 1,postl,postl + i - 1 - inl);
                if(i != inr) t[temp].r = build(i + 1,inr,postl + i - inl,postr - 1);
                break;
            }
        }
        return temp;
    }
    void level() {
        queue<int> q;
        q.push(1);
        int flag = 0;
        while(!q.empty()) {
            if(flag ++) putchar(' ');
            printf("%d",t[q.front()].d);
            if(t[q.front()].l) {
                q.push(t[q.front()].l);
            }
            if(t[q.front()].r) {
                q.push(t[q.front()].r);
            }
            q.pop();
        }
    }
    int main() {
        cin >> n;
        for(int i = 0;i < n;i ++) {
            cin >> post[i];
        }
        for(int i = 0;i < n;i ++) {
            cin >> in[i];
        }
        build(0,n - 1,0,n - 1);
        level();
    }
  • 相关阅读:
    SCAU 9504 面试
    SCAU 9503 懒人选座位
    SCAU 8628 相亲
    SCAU 10691 ACM 光环
    SCAU 8626 原子量计数
    SCAU 10674 等差对
    HDU ACM 1048 The Hardest Problem Ever (水题)
    SCAU 9502 ARDF
    SCAU 10686 DeathGod不知道的事情
    SCAU 8629 热身游戏(高精度)
  • 原文地址:https://www.cnblogs.com/8023spz/p/7339389.html
Copyright © 2011-2022 走看看