PAT_A1043#Is It a Binary Search Tree

Source:

PAT A1043 Is It a Binary Search Tree (25 分)

Description:

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 Imageof 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

Keys:

Code:

 1 /*
 2 Data: 2019-06-26 17:25:17
 3 Problem: PAT_A1043#Is It a Binary Search Tree
 4 AC: 26:46
 5 
 6 题目大意:
 7 BST定义:lchild < root <= rchild
 8 镜像树:交换左右子树
 9 现给定一个序列,判断其是否为BST或Mirror BST的先序遍历,若是,打印后序遍历
10 
11 基本思路:
12 由先序序列可以构建一棵唯一的二叉树,
13 分别NLR和NRL遍历该二叉树,若遍历序列与给定序列相同,则Yes,反之No
14 */
15 #include<cstdio>
16 #include<vector>
17 using namespace std;
18 vector<int> pre,post,input;
19 struct node
20 {
21     int data;
22     node *lchild,*rchild;
23 };
24 
25 void Insert(node *&root, int x)
26 {
27     if(root == NULL)
28     {
29         root = new node;
30         root->data = x;
31         root->lchild = root->rchild = NULL;
32     }
33     else if(x < root->data)
34         Insert(root->lchild, x);
35     else
36         Insert(root->rchild, x);
37 }
38 
39 void NLR(node *root)
40 {
41     if(root == NULL)
42         return;
43     pre.push_back(root->data);
44     NLR(root->lchild);
45     NLR(root->rchild);
46     post.push_back(root->data);
47 }
48 
49 void NRL(node *root)
50 {
51     if(root == NULL)
52         return;
53     pre.push_back(root->data);
54     NRL(root->rchild);
55     NRL(root->lchild);
56     post.push_back(root->data);
57 }
58 int main()
59 {
60 #ifdef ONLINE_JUDGE
61 #else
62     freopen("Test.txt", "r", stdin);
63 #endif // ONLINE_JUDGE
64 
65     int n,x;
66     scanf("%d", &n);
67     node *root = NULL;
68     for(int i=0; i<n; i++)
69     {
70         scanf("%d", &x);
71         Insert(root, x);
72         input.push_back(x);
73     }
74     NLR(root);
75     if(pre == input)
76     {
77         printf("YES\n");
78         for(int i=0; i<post.size(); i++)
79             printf("%d%c", post[i],i==post.size()-1?'\n':' ');
80     }
81     else
82     {
83         pre.clear();
84         post.clear();
85         NRL(root);
86         if(pre == input)
87         {
88             printf("YES\n");
89             for(int i=0; i<post.size(); i++)
90                 printf("%d%c", post[i],i==post.size()-1?'\n':' ');
91         }
92         else
93             printf("NO\n");
94     }
95 
96     return 0;
97 }

 

转载于:https://www.cnblogs.com/blue-lin/p/11091224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值