zoukankan      html  css  js  c++  java
  • 由树的中后序遍历求树的前层序遍历

    题目意思言简意赅

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

    eg:input:

    7
    2 3 1 5 7 6 4
    1 2 3 4 5 6 7
    output:4132657
    注意这个树非常丑陋:
          4
         /
        1   6
         /
         3 5 7
         /
        2          

    首先是先序排列:根据后序的最后一个可以找到根的位置,然后再根据根在中序里面搜索,分为左右子树

    详情可以参考这篇题解https://www.luogu.org/blog/user21653/solution-p1030 这个输入输出有点区别,但原理是一样的

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int inf=1<<30;
     4 typedef long long ll;
     5 typedef pair<int,int> P;
     6 const double pi=acos(-1);
     7 const int mod=1e8+7;
     8 const int maxn=140;
     9 const int maxm=6300;
    10 int a[maxn],b[maxn];//a存后序遍历,b存中序遍历 
    11 void rule(int i,int j,int l,int r){
    12     if(j-i>=0){    
    13     int dd=j,idx;
    14     cout<<a[dd];
    15     for(int p=l;p<=r;p++){
    16         if(b[p]==a[dd]){
    17             idx=p;break;
    18         }
    19     }
    20     rule(i,i+(idx-1-l),l,idx-1);
    21     rule(i+(idx-l),j-1,idx+1,r);
    22 }
    23 }
    24 int main(){
    25     int n;scanf("%d",&n);
    26     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    27     for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    28     rule(1,n,1,n);
    29     return 0;
    30 }

    接着便是层序遍历:

    输入一样,这次输出是

    4 1 6 3 5 7 2
    方法也基本相同,就是多了个index,初始都为-1,最后按顺序输出不为-1的
    修改:注,下面这个方法虽然能过GPLT L2-006,但其实是错的,因为三十个节点的二叉树,极端情况下会变成斜的一列,那么level开10W是不够存的(最大能有2^29),正确做法应该是建树,然后BFS
    参考题解:https://www.cnblogs.com/pk28/p/5496017.html
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int inf=1<<30;
     4 typedef long long ll;
     5 typedef pair<int,int> P;
     6 const double pi=acos(-1);
     7 const int mod=1e8+7;
     8 const int maxn=140;
     9 const int maxm=6300;
    10 int a[maxn],b[maxn];//a存后序遍历,b存中序遍历 
    11 vector<int> level(100000,-1); 
    12 void rule(int i,int j,int l,int r,int index){
    13     if(j-i>=0){    
    14     int dd=j,idx;
    15     level[index]=a[dd];
    16     for(int p=l;p<=r;p++){
    17         if(b[p]==a[dd]){
    18             idx=p;break;
    19         }
    20     }
    21     rule(i,i+(idx-1-l),l,idx-1,index*2+1);
    22     rule(i+(idx-l),j-1,idx+1,r,index*2+2);
    23 }
    24 }
    25 int main(){
    26     int n;scanf("%d",&n);
    27     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    28     for(int i=1;i<=n;i++)scanf("%d",&b[i]);
    29     rule(1,n,1,n,0);
    30     int cnt=0;
    31     for(int i=0;i<level.size();i++){
    32         if(level[i]!=-1&&cnt!=n-1){
    33             cnt++;printf("%d ",level[i]);
    34         }else if(level[i]!=-1){
    35             printf("%d
    ",level[i]);
    36             break;
    37         }
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    ant 软件包不存在报错
    在 Internet Explorer 中使用 Windows 窗体控件
    智能客户端
    Back to the Future with Smart Clients
    "Automation 服务器不能创建对象" 的解决方案
    Top 10 Reasons for Developers to Create Smart Clients
    Updater Application Block for .NET
    Smart Client Application Model and the .NET Framework 1.1
    Security and Versioning Models in the Windows Forms Engine Help You Create and Deploy Smart Clients
    智能客户端技术总结(二)
  • 原文地址:https://www.cnblogs.com/qingjiuling/p/10491872.html
Copyright © 2011-2022 走看看