日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

小峰峰的pat甲级刷题记录1020

發(fā)布時間:2023/12/20 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小峰峰的pat甲级刷题记录1020 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

小峰峰的pat甲級刷題記錄1020

方法一:通過后序和中序序列構(gòu)建樹,再層序輸出

#include<iostream> #include<vector> using namespace std;struct node {int value;node* left,*right; }; int N; vector<int>Post; vector<int>In; node* makenode(int h1,int t1,int h2,int t2) {if(h1>t1)return NULL; // 死路返回(h1==t1時傳入一棵節(jié)點數(shù)為1的樹)node* p = new node;p->value = Post[t1];int i;for(i=h2;In[i]!=Post[t1];i++);p->left=makenode(h1,i-1-h2+h1,h2,i-1); // 同一棵樹節(jié)點數(shù)相等,所以i-1-h2=x-h1 求出x=i-1-h2+h1 p->right=makenode(i+t1-t2,t1-1,i+1,t2);return p; }int main() {cin>>N;int i;Post.resize(N);In.resize(N);for(i=0;i<N;i++)cin>>Post[i];for(i=0;i<N;i++)cin>>In[i];node* root = makenode(0,N-1,0,N-1);node* Queue[40];int head=0,tall=0; // head==tall時隊列為空Queue[tall++]=root; // tall指針永遠為空int j=0;while(head<tall){node* p=Queue[head++];if(j==0)cout<<p->value;else cout<<' '<<p->value;j=1;if(p->left)Queue[tall++]=p->left;if(p->right)Queue[tall++]=p->right;} }

模塊&拓展

用先序序列與中序序列構(gòu)建樹

node* makenode(int h1,int t1,int h2,int t2) {if(h1>t1)return NULL; // 死路返回node* p = new node;p->value = Pre[h1];int i;for(i=h2;In[i]!=Post[h1];i++);p->left=makenode(h1+1,i-h2+h1,h2,i-1); // 同一棵樹節(jié)點數(shù)相等,所以i-1-h2=x-h1 求出x=i-h2+h1 p->right=makenode(t1-t2+i+1,t1,i+1,t2);return p; }

只傳三個參數(shù)構(gòu)建樹

node* makenode(int root,int In_h,int In_t) {if(In_h>In_t)return NULL; // 死路返回node* p = new node;p->value = Post[t1];int i;for(i=h2;In[i]!=Post[t1];i++);p->left=makenode(root-1-(In_t-i),In_h,i-1); p->right=makenode(root-1,i+1,In_t);return p; }

不用queue庫實現(xiàn)隊列

node* Queue[40];int head=0,tall=0; // head==tall時隊列為空Queue[tall++]=root; // tall指針永遠為空int j=0;while(head<tall){node* p=Queue[head++];if(j==0)cout<<p->value;else cout<<' '<<p->value;j=1;if(p->left)Queue[tall++]=p->left;if(p->right)Queue[tall++]=p->right;}

用queue庫

int j=0;while(!que.empty()){node* p=que.front();que.pop();if(j==0)cout<<p->value;else cout<<' '<<p->value;j=1;if(p->left)que.push(p->left);if(p->right)que.push(p->right);}

方法二:不構(gòu)建樹

#include <cstdio> #include <vector> #include <map> using namespace std; vector<int> post, in; map<int, int> level; void pre(int root, int start, int end, int index) {if(start > end) return ;int i = start;while(i < end && in[i] != post[root]) i++;level[index] = post[root];pre(root - 1 - end + i, start, i - 1, 2 * index + 1);pre(root - 1, i + 1, end, 2 * index + 2); } int main() {int n;scanf("%d", &n);post.resize(n);in.resize(n);for(int i = 0; i < n; i++) scanf("%d", &post[i]);for(int i = 0; i < n; i++) scanf("%d", &in[i]);pre(n-1, 0, n-1, 0);auto it = level.begin();printf("%d", it->second);while(++it != level.end()) printf(" %d", it->second);return 0;———————————————— 版權(quán)聲明:本文為CSDN博主「柳婼」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/liuchuo/article/details/52137796

模塊&拓展

通過后續(xù)序列和中序序列直接實現(xiàn)先序遍歷

void pre(int root,int In_h,int In_t) {if(In_h>In_t)return; //由于是void所以return后什么都不寫cout<<Post[root];int j;for(j=In_h;In[j]!=Post[root];j++);pre(root-1-(In_t-j),In_h,j-1);pre(root-1,j+1,In_t); }

當給出前中序求后序序列時,只需要改變cout語句的位置,就能獲得后序序列

利用字典的按鍵值自動排序功能求得層序序列

void pre(int root,int In_h,int In_t,int index) {if(In_h>In_t)return ; // 死路返回level[index]=Post[root];for(int j=In_h;In[j]!=Post[root];j++);pre(root-1-(In_t-j),In_h,j-1,2*index+1);pre(root-1,j+1,In_t,2*index+2); }

遍歷字典的方法

//第一種用指針 auto it = level.begin(); printf("%d", it->second); while(++it != level.end()) printf(" %d", it->second); // .end()指向空 //第二種用in int tem=0; for(auto it:level) {if(tem==0){tem=1; cout<<it.second;}else cout<<' '<<it.second; }

總結(jié)

以上是生活随笔為你收集整理的小峰峰的pat甲级刷题记录1020的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。