1. 程式人生 > >PTA L2-004 這是二叉搜索樹嗎?-判斷是否是對一棵二叉搜索樹或其鏡像進行前序遍歷的結果 團體程序設計天梯賽-練習集

PTA L2-004 這是二叉搜索樹嗎?-判斷是否是對一棵二叉搜索樹或其鏡像進行前序遍歷的結果 團體程序設計天梯賽-練習集

text oid bar main 遍歷 html flex 輸出 感覺

L2-004 這是二叉搜索樹嗎? (25 分)

一棵二叉搜索樹可被遞歸地定義為具有下列性質的二叉樹:對於任一結點,

  • 其左子樹中所有結點的鍵值小於該結點的鍵值;
  • 其右子樹中所有結點的鍵值大於等於該結點的鍵值;
  • 其左右子樹都是二叉搜索樹。

所謂二叉搜索樹的“鏡像”,即將所有結點的左右子樹對換位置後所得到的樹。

給定一個整數鍵值序列,現請你編寫程序,判斷這是否是對一棵二叉搜索樹或其鏡像進行前序遍歷的結果。

輸入格式:

輸入的第一行給出正整數 N(≤)。隨後一行給出 N 個整數鍵值,其間以空格分隔。

輸出格式:

如果輸入序列是對一棵二叉搜索樹或其鏡像進行前序遍歷的結果,則首先在一行中輸出 YES

,然後在下一行輸出該樹後序遍歷的結果。數字間有 1 個空格,一行的首尾不得有多余空格。若答案是否,則輸出 NO

輸入樣例 1:

7
8 6 5 7 10 8 11

輸出樣例 1:

YES
5 7 6 8 11 10 8

輸入樣例 2:

7
8 10 11 8 6 7 5

輸出樣例 2:

YES
11 8 10 7 5 6 8

輸入樣例 3:

7
8 6 8 5 10 9 11

輸出樣例 3:

NO

因為前面寫了二叉樹的,所以這道題打算也用那種思路,寫是寫出來了,但是感覺怪怪的。

建完樹之後,寫了個bfs搜了一下左右節點,發現好像也沒毛病,但是自己想的一個樣例過不了,肯定是錯的代碼,應該是後臺樣例比較水,所以滿分水過了。。。

技術分享圖片

代碼寫的奇醜無比,但是還是希望有好心人能幫我想想我的代碼哪裏要改才能改對,過不了的樣例:

2

2 1

輸出應該為

YES

1 2

我的代碼跑不出來。

後面看了一下別人的題解,發現還是別人的思路更簡潔好實現一些,學到了,我這個弟弟。

先貼一下我的代碼,不要學我的代碼,有bug的,找不出來什麽問題。。。

代碼:

  1 //L2-004 這是二叉搜索樹嗎?
  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 int pre[maxn]; 9 int n,flag=0; 10 11 struct node{ 12 int l,r; 13 }tree[maxn]; 14 15 int buildmin(int l,int r) 16 { 17 if(l>r) return 0; 18 if(r==-1) return 0; 19 int rt=pre[l]; 20 int p1=r; 21 while(pre[p1]>=rt){ 22 if(p1==l) break; 23 p1--; 24 } 25 int minn=inf,pos; 26 for(int i=p1;i>l;i--){ 27 if(minn>pre[i]){ 28 minn=pre[i]; 29 pos=i; 30 } 31 } 32 for(int i=pos;i>l;i--){ 33 if(pre[i]>=rt){ 34 flag=1;break; 35 } 36 } 37 if(flag==1){ 38 buildmin(0,-1); 39 } 40 // cout<<pre[p1]<<endl; 41 else{ 42 if(p1!=l){ 43 tree[l].l=buildmin(l+1,p1); 44 tree[l].r=buildmin(p1+1,r); 45 } 46 else{ 47 tree[l].r=buildmin(p1+1,r); 48 } 49 return l; 50 } 51 } 52 53 int buildmax(int l,int r) 54 { 55 if(l>r) return 0; 56 if(r==-1) return 0; 57 int rt=pre[l]; 58 int p1=r; 59 while(pre[p1]<rt){ 60 p1--; 61 } 62 for(int i=p1;i>l;i--){ 63 if(pre[i]<rt){ 64 flag=1;break; 65 } 66 } 67 if(flag==1){ 68 buildmax(0,-1); 69 } 70 // cout<<pre[p1]<<endl; 71 else{ 72 if(p1!=r){ 73 tree[l].l=buildmax(l+1,p1); 74 tree[l].r=buildmax(p1+1,r); 75 } 76 else{ 77 tree[l].l=buildmax(l+1,p1); 78 } 79 return l; 80 } 81 } 82 83 void bfs(int x) 84 { 85 vector<int> vec; 86 queue<int> que; 87 que.push(x); 88 while(!que.empty()){ 89 int ret=que.front(); 90 que.pop(); 91 if(ret==0) break; 92 vec.push_back(ret); 93 if(tree[ret].l!=0){ 94 que.push(tree[ret].l); 95 } 96 if(tree[ret].r!=0){ 97 que.push(tree[ret].r); 98 } 99 } 100 int l=vec.size(); 101 for(int i=0;i<l;i++) 102 cout<<pre[vec[i]]<<" "; 103 cout<<endl; 104 } 105 106 vector<int> vec; 107 108 int behorder(int x) 109 { 110 if(tree[x].l==0&&tree[x].r==0){ 111 if(pre[x]!=0) 112 vec.push_back(pre[x]); 113 return 0; 114 } 115 behorder(tree[x].l); 116 behorder(tree[x].r); 117 vec.push_back(pre[x]); 118 } 119 120 void print() 121 { 122 int l=vec.size(); 123 for(int i=0;i<l-1;i++) 124 cout<<vec[i]<<" "; 125 cout<<vec[l-1]<<endl; 126 } 127 128 int main() 129 { 130 scanf("%d",&n); 131 for(int i=1;i<=n;i++) 132 scanf("%d",&pre[i]); 133 // cout<<"23333333333"<<endl; 134 if(pre[2]<pre[1]){ 135 buildmin(1,n); 136 if(flag==1) cout<<"NO"<<endl; 137 else{ 138 // bfs(1); 139 cout<<"YES"<<endl; 140 behorder(1); 141 print(); 142 } 143 } 144 else{ 145 buildmax(1,n); 146 if(flag==1) cout<<"NO"<<endl; 147 else{ 148 // bfs(1); 149 cout<<"YES"<<endl; 150 behorder(1); 151 print(); 152 } 153 } 154 // for(int i=1;i<=n;i++) 155 // cout<<tree[i].l<<" "<<tree[i].r<<endl; 156 // 157 //// else buildmax(1,n); 158 // behorder(); 159 160 }

學的別人的思路:

代碼:

 1 //L2-004 這是二叉搜索樹嗎?
 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 int n;
 9 int isMirror=0;
10 int pre[maxn];
11 vector<int> vec;
12 
13 int build(int l,int r)
14 {
15     if(l>r) return 0;
16     int rt=pre[l];
17     int p1=l+1,p2=r;
18     if(!isMirror){
19         while(p1<=r&&rt>pre[p1]) p1++;
20         while(p2>l&&rt<=pre[p2]) p2--;
21     }
22     else{
23         while(p1<=r&&rt<=pre[p1]) p1++;
24         while(p2>l&&rt>pre[p2]) p2--;
25     }
26     if(p1-p2!=1) return 0;
27     build(l+1,p2);//按照後序遍歷建樹
28     build(p1,r);
29     vec.push_back(rt);
30 }
31 
32 int main()
33 {

先這樣。

PTA L2-004 這是二叉搜索樹嗎?-判斷是否是對一棵二叉搜索樹或其鏡像進行前序遍歷的結果 團體程序設計天梯賽-練習集