小峰峰的pat甲级刷题记录1020
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                小峰峰的pat甲级刷题记录1020
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                小峰峰的pat甲級刷題記錄1020
方法一:通過后序和中序序列構建樹,再層序輸出
#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時傳入一棵節點數為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); // 同一棵樹節點數相等,所以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;} }模塊&拓展
用先序序列與中序序列構建樹
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); // 同一棵樹節點數相等,所以i-1-h2=x-h1 求出x=i-h2+h1 p->right=makenode(t1-t2+i+1,t1,i+1,t2);return p; }只傳三個參數構建樹
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庫實現隊列
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);}方法二:不構建樹
#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;———————————————— 版權聲明:本文為CSDN博主「柳婼」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/liuchuo/article/details/52137796模塊&拓展
通過后續序列和中序序列直接實現先序遍歷
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; }總結
以上是生活随笔為你收集整理的小峰峰的pat甲级刷题记录1020的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: ROS学习笔记之——amcl源码的解读
- 下一篇: SQL学习笔记(04)_JOIN
