UVA 548.Tree-fgets()函数读入字符串+二叉树(中序+后序遍历还原二叉树)+DFS or BFS(二叉树路径最小值并且相同路径值叶子节点权值最小)

Tree UVA - 548 

题意就是多次读入两个序列,第一个是中序遍历的,第二个是后序遍历的。还原二叉树,然后从根节点走到叶子节点,找路径权值和最小的,如果有相同权值的就找叶子节点权值最小的。

最后输出来叶子节点。

一开始写的时候是用gets读入的,报CE,

要用fgets写,关于fgets(),传送门:

fgets函数及其用法,C语言fgets函数详解

一开始用bfs过的,后来发现,好多人都是dfs过的,又写了一下dfs。。。

代码:

  1 //二叉树的中序和后序遍历还原树并输出最短路径的叶子节点值
  2 #include<bits/stdc++.h>
  3 using namespace std;
  4 typedef long long ll;
  5 const int maxn=1e5+10;
  6 const int inf=0x3f3f3f3f;
  7 
  8 struct node{
  9     int l,r,val,father;
 10 }tree[maxn];
 11 
 12 int n=0,minn,leaf;
 13 int mid[maxn],beh[maxn];
 14 
 15 int build(int la,int ra,int lb,int rb)
 16 {
 17     if(la>ra) return 0;
 18     int rt=beh[rb];
 19     int p1=la;
 20     while(mid[p1]!=rt) p1++;
 21     int p2=p1-la;
 22     tree[rt].l=build(la,p1-1,lb,lb+p2-1);
 23     tree[rt].r=build(p1+1,ra,lb+p2,rb-1);
 24     return rt;
 25 }
 26 
 27 /*
 28 int fa[maxn];
 29 
 30 void bfs(int x)
 31 {
 32     queue<int> q;
 33     vector<int> vec;
 34     q.push(x);
 35     fa[x]=0;tree[0].val=0;
 36     while(!q.empty()){
 37         int rt=q.front();q.pop();
 38         if(rt==0){
 39             break;
 40         }
 41         tree[rt].val=tree[fa[rt]].val+rt;
 42 //        cout<<tree[rt].val<<endl;
 43         if(tree[rt].l){
 44             fa[tree[rt].l]=rt;
 45             q.push(tree[rt].l);
 46         }
 47         if(tree[rt].r){
 48             fa[tree[rt].r]=rt;
 49             q.push(tree[rt].r);
 50         }
 51         if(tree[rt].l==0&&tree[rt].r==0){
 52             vec.push_back(rt);
 53         }
 54 
 55     }
 56     int l=vec.size();
 57     int minn=inf,ans=inf;
 58     for(int i=0;i<l;i++){
 59         if(tree[vec[i]].val<minn){
 60             minn=tree[vec[i]].val;
 61             ans=vec[i];
 62         }
 63         if(tree[vec[i]].val==minn){
 64             if(vec[i]<ans) ans=vec[i];
 65         }
 66     }
 67     printf("%d
",ans);
 68 }
 69 */
 70 
 71 
 72 void dfs(int rt,int sum)
 73 {
 74     sum+=rt;
 75     if(!tree[rt].l&&!tree[rt].r){
 76         if(sum<minn||(sum==minn&&rt<leaf)){
 77            minn=sum;
 78            leaf=rt;
 79         }
 80     }
 81     if(tree[rt].l) dfs(tree[rt].l,sum);
 82     if(tree[rt].r) dfs(tree[rt].r,sum);
 83 }
 84 
 85 char c[maxn];
 86 
 87 int main()
 88 {
 89     while(fgets(c,maxn,stdin)){
 90         int l=strlen(c);
 91         int i=0,cnt=0;
 92         if(n==0){
 93             while(i<l){
 94                 if(c[i]=='') break;
 95                 if(c[i]==' '||c[i]=='
') i++,mid[++n]=cnt,cnt=0;
 96                 else{
 97                     cnt=cnt*10+c[i]-'0';
 98                     i++;
 99                 }
100             }
101         }
102         else{
103             n=0;
104             while(i<l){
105                 if(c[i]=='') break;
106                 if(c[i]==' '||c[i]=='
') i++,beh[++n]=cnt,cnt=0;
107                 else{
108                     cnt=cnt*10+c[i]-'0';
109                     i++;
110                 }
111             }
112             build(1,n,1,n);
113 //            bfs(beh[n]);
114             minn=inf,leaf=beh[n];
115             dfs(beh[n],0);
116             printf("%d
",leaf);
117             n=0;
118         }
119     }
120 }
原文地址:https://www.cnblogs.com/ZERO-/p/10651264.html