[PAT Level] 1043 IS IT A Binary Search Tree (25 points) (Judge whether or not the BST is traversed and outputs traversal traversal)

Title:

Enter a positive integer N (<=1000), and then enter the serial number of N points. If the sequence just entered is a binary search tree or its mirror image (the center is flipped 180°), then output YES and output its post-order traversal, otherwise output NO.

trick:

for(auto it:post)
cout<

This output will make the 0th, 1st data point format wrong. . . The reason is unknown

cout<
for(int i=1;i
cout<<" "<
This is also correct

Code:

#define HAVE_STRUCT_TIMESPEC
#include< bits/stdc++.h>
using namespace std;
int pre[1007];
vect orpost;
int flag;
void dfs(int l,int r){
if(l>r)
return ;
int ll=l+1,rr=r;
if(!flag){
while(ll <=r&&pre[ll] ++ll;< br> while(rr>l&&pre[rr]>=pre[l])
–rr;
}
else{
while( ll<=r&&pre[ll]>=pre[l])
++ll;
while(rr>l&&pre[rr] –rr;
}
if(ll!=rr+1)
return ;
dfs(l+1,rr);
dfs(ll,r);
post.push_back(pre[l]);
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;++ i)
cin>>pre[i];
flag=0;
dfs(1,n);
if(post.size()
flag=1;
post.clear();
dfs(1,n );
}
if(post.size()< n)
cout<<"NO";
else{
cout< <"YES "< for(int i=1;i cout<<" "< }
return 0;
}
p>

cout<
for(int i=1;i
cout<<" "<
This is also correct

cout<

for(int i=1;i

cout<<" "<

This is also correct

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5258 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.