PAT甲级——A1043 Is It a Binary Search Tree

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 (≤). 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 11

Sample Output 1:

YES
5 7 6 8 11 10 8

Sample Input 2:

7
8 10 11 8 6 7 5

Sample Output 2:

YES
11 8 10 7 5 6 8

Sample Input 3:

7
8 6 8 5 10 9 11

Sample Output 3:

NO

题意
给出N个正整数来作为一棵二叉排序树的结点插入顺序,问:这串序列是否是该二叉排序树的先序序列或是该二叉排序树的镜像树的先序序列。所谓镜像树是指交换二叉树的所有结点的左右子树而形成的树(也即左子树所有结点数据域大于或等于根结点,而根结点数据域小于右子树所有结点的数据域)。如果是镜像树,则输出YES,并输出对应的树的后序序列;否则,输出NO。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node{//树结点
 4     int data;
 5     Node*left,*right;
 6     Node(int d):data(d),left(nullptr),right(nullptr){}
 7 };
 8 void insertNode(Node*&root,int data,bool mirror){//插入结点,构建二叉查找树或其镜像树
 9     if(root==nullptr)//根指针指向空节点
10         root=new Node(data);//建立新节点
11     else if(data<root->data&&!mirror)//如果不是镜像树且数据域比根节点的数据小
12         insertNode(root->left,data,mirror);//向左子树插入
13     else if(data<root->data&&mirror)//如果是镜像树且数据域比根节点的数据小
14         insertNode(root->right,data,mirror);//向右子树插入
15     else if(data>=root->data&&!mirror)//如果不是镜像树且数据域比根节点的数据大或相等
16         insertNode(root->right,data,mirror);//向右子树插入
17     else if(data>=root->data&&mirror)//如果是镜像树且数据域比根节点的数据大或相等
18         insertNode(root->left,data,mirror);//向左子树插入
19 }
20 void preOrder(Node*root,vector<int>&order){//求先根遍历
21     order.push_back(root->data);
22     if(root->left!=nullptr)
23         preOrder(root->left,order);
24     if(root->right!=nullptr)
25         preOrder(root->right,order);
26 }
27 void postOrder(Node*root,vector<int>&order){//求后根遍历
28     if(root->left!=nullptr)
29         postOrder(root->left,order);
30     if(root->right!=nullptr)
31         postOrder(root->right,order);
32     order.push_back(root->data);
33 }
34 int main(){
35     int N;
36     scanf("%d",&N);
37     vector<int> order,pre(N);
38     Node*root=nullptr;
39     for(int i=0;i<N;++i)
40         scanf("%d",&pre[i]);
41     for(int i=0;i<N;++i)//构建二叉查找树或其镜像树
42         if(pre.size()>=2&&pre[1]>pre[0])
43             insertNode(root,pre[i],true);
44         else
45             insertNode(root,pre[i],false);
46     preOrder(root,order);//求构建的树先根遍历序列
47     if(equal(pre.cbegin(),pre.cend(),order.cbegin())){//c++标准库自带的判断两个容器元素是否对应相等
48         order.clear();
49         postOrder(root,order);//求后根遍历序列
50         printf("YES\n");
51         for(int i=0;i<order.size();++i)
52             printf("%s%d",i>0?" ":"",order[i]);
53     }else
54         printf("NO");
55     return 0;
56 }

这道题只要求求出后根遍历序列,我们可以通过二叉查找树的先根遍历序列直接得出后根遍历序列。

按照第一个样例对算法进行阐述:

1.先根序列为:8 6 5 7 10 8 11

当前后根序列为空

可以确定8是整棵树的根节点,由于6 5 7小于8;8 10 11大于等于8,按照二叉查找树的性质可知6 5 7是8的左子树,10 8 11是8的右子树。由于后根序列是“左右中”顺序,先处理左子树6 5 7

2. 先根序列为6 5 7

当前后根序列为空

可以确定6是整棵子树的根节点,由于5小于6;7大于6,按照二叉查找树的性质可知5是6的左子树,7是6的右子树。由于后根序列是“左右中”顺序,先处理左子树5

3.先根序列为5

当前后根序列为空

可以确定5是叶子结点,放入后根遍历序列中,返回第2步中处理的子树中

4.先根序列为6 5 7

当前后根序列为5 

左子树处理完毕,处理右子树7

5.先根遍历序列为7

当前后根遍历序列为5

可以确定7是叶子结点,放入后根遍历序列中,返回第4步中处理的子树中

6.先根遍历序列为6 5 7

当前后根遍历序列为5 7

左右子树均处理完成,将根节点6放入后根遍历序列中,返回第1步处理的树中

7.先根序列为:8 6 5 7 10 8 11

当前后根遍历序列为5 7 6

左子树处理完毕,处理右子树10 8 11

8.先根序列为10 8 11

当前后根遍历序列为5 7 6

可以确定10是整棵子树的根节点,由于8小于10,11大于10,按照二叉查找树的性质可知8是10的左子树,11是10的右子树。由于后根序列是“左右中”顺序,且左右子树均为叶子结点,按8 11 10的顺序将三个结点数据值放入后根遍历序列中,返回到第7步处理的子树中

9.先根序列为:8 6 5 7 10 8 11

当前后根遍历序列为5 7 6 8 11 10

左右子树均处理完成,将根节点8放入后根遍历序列中,算法结束

镜像树求后根遍历序列的算法与之相同,只需做稍许修改。如果输入序列的长度>=2并且第2个数比第一个数小,那么输入的就应该是一棵二叉查找树;否则是一棵镜像树。对于无法构成二叉查找树和镜像树的序列,在进行上述算法时可以提前返回,通过判断得出的后根遍历序列中元素个数与给定的元素总数是否相等来判断是否是无法构成二叉查找树和镜像树的序列。具体方式可见代码。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 vector<int>post,pre(1005);
 4 bool mirror=false;//是否是镜像树
 5 void postOrder(int left,int right){
 6     if(left>right)
 7         return;
 8     int i=left+1,j=right;//i指示右子树的第一个数在先根遍历序列中的索引,j指示左子树的最后一个数在先根遍历序列中的索引
 9     if(!mirror) {
10         while(i<=right&&pre[left]>pre[i]) 
11             ++i;
12         while(j>left&&pre[left]<=pre[j]) 
13             --j;
14     }else{
15         while(i<=right&&pre[left]<=pre[i]) 
16             ++i;
17         while(j>left&&pre[left]>pre[j]) 
18             --j;
19     }
20     if(i-j!=1) return ;//i-j!=1,说明不能构成二叉查找树或镜像树,提前返回
21     postOrder(left+1,i-1);//处理左子树
22     postOrder(i,right);//处理右子树
23     post.push_back(pre[left]);//将根节点加入后根遍历序列中
24 }
25 int main(){
26     int N;
27     scanf("%d",&N);
28     for(int i=0;i<N;++i)
29         scanf("%d",&pre[i]);
30     if(N>2&&pre[1]>pre[0])//判断是否是镜像树
31         mirror=true;
32     postOrder(0,N-1);//得出后根遍历序列
33     if(post.size()==N){//得出的后根遍历序列中元素个数与给定的元素总数相等,说明能构成二叉查找树或镜像树
34         printf("YES\n");
35         for(int i=0;i<post.size();++i)
36             printf("%s%d",i>0?" ":"",post[i]);
37     }else//得出的后根遍历序列中元素个数与给定的元素总数相等,说明不能构成二叉查找树或镜像树
38         printf("NO");
39     return 0;
40 }

猜你喜欢

转载自www.cnblogs.com/zzw1024/p/11260648.html