本文主要是介绍【PAT】【Advanced Level】1043. Is It a Binary Search Tree (25),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1043. Is It a Binary Search Tree (25)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.
Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input 1:7 8 6 5 7 10 8 11Sample Output 1:
YES 5 7 6 8 11 10 8Sample Input 2:
7 8 10 11 8 6 7 5Sample Output 2:
YES 11 8 10 7 5 6 8Sample Input 3:
7 8 6 8 5 10 9 11Sample Output 3:
NO
https://www.patest.cn/contests/pat-a-practise/1043
https://www.nowcoder.com/pat/5/problem/4082
思路:
重新建树并遍历
注意细节处理
CODE:
#include<iostream>
#include<cstring>
#include<string>
#define N 1001using namespace std;typedef struct S
{int val;int ls,rs;
};
S no[N];bool dfs1(int l,int r)
{//cout<<l<<" "<<r<<endl;if (l==r) return true;int f=-1;bool fla=0;for (int i=l+1;i<=r;i++){if (no[i].val>=no[l].val){fla=1;break;}f=i;}//cout<<f<<endl;bool pd=1;if (fla==1){for (int i=max(f+1,l+1);i<=r;i++) //max 不可忘 if (no[i].val<no[l].val)return false;no[l].rs=max(f+1,l+1);pd=pd&dfs1(max(f+1,l+1),r);}if (f!=-1){no[l].ls=l+1;pd=pd&dfs1(l+1,f);} return pd;
}bool dfs2(int l,int r)
{//cout<<l<<" "<<r<<endl;if (l==r) return true;int f=-1;bool fla=0;for (int i=l+1;i<=r;i++){if (no[i].val<no[l].val){fla=1;break;}f=i;}//cout<<f<<" "<<fla<<endl;bool pd=1;if (fla==1){for (int i=max(f+1,l+1);i<=r;i++) //max 不可忘 if (no[i].val>no[l].val)return false;no[l].rs=max(f+1,l+1);pd=pd&dfs2(max(f+1,l+1),r);}if (f!=-1){no[l].ls=l+1;pd=pd&dfs2(l+1,f);} return pd;
}void pri(int n)
{if (no[n].ls!=-1){pri(no[n].ls);}if (no[n].rs!=-1){pri(no[n].rs);}cout<<no[n].val;if (n!=1) cout<<" ";
}
int main()
{int n;cin>>n;for (int i=1;i<=n;i++){S t;int v;cin>>v;t.val=v;t.ls=t.rs=-1;no[i]=t;}bool fl;//cout<<n<<endl;//cout<<no[1].val<<" "<<no[2].val<<endl;if (n==1 || no[1].val>no[2].val){//cout<<no[1].val<<" "<<no[2].val<<endl;fl=dfs1(1,n);} else{fl=dfs2(1,n);}//cout<<fl<<endl;if (fl==0){cout<<"NO";}else{cout<<"YES"<<endl;pri(1);}return 0;
}
这篇关于【PAT】【Advanced Level】1043. Is It a Binary Search Tree (25)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!