【题意+分析】1043 Is It a Binary Search Tree (25 分)
立志用最少的代碼做最高效的表達
PAT甲級最優題解——>傳送門
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 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
題意:給定一個樹的先序序列,如果能以該序列構造出二叉搜索樹(左節點小于根節點、右節點大于等于根節點),或二叉鏡像搜索樹(反之),則輸出該樹或其鏡像樹的后序序列。
算法邏輯:按給定序列建二叉樹,由于二叉搜索樹唯一性,比較該二叉搜索樹的先序序列 或 該鏡像二叉搜索樹的先序序列是否等于原給定序列, 如果等于,則說明可以構建。 輸出其后序序列即可。
具體細節請讀者閱讀代碼體會。
#include<bits/stdc++.h> using namespace std; vector<int>pre1, pre2,post,vec; //先序后序用vector存儲 int n, x; struct node {int data; node *left, *right; }; struct node *root = NULL;//二叉搜索樹建樹方法 void Insert(node * &root, int data) { if(root == NULL) {root = new node();root->data = x;root->left = root->right = NULL;} else if(data < root->data)Insert(root->left, data);else if(data >= root->data)Insert(root->right, data); } //構造二叉搜索樹先序序列 void preorder1(node *root) {if(root) {pre1.push_back(root->data);preorder1(root->left);preorder1(root->right);} }//構造二叉樹鏡像搜索樹先序序列 void preorder2(node *root) {if(root) {pre2.push_back(root->data);preorder2(root->right);preorder2(root->left);} }void postorder1(node * root) {if(root) {postorder1(root->left);postorder1(root->right);post.push_back(root->data);} }void postorder2(node *root) {if(root) {postorder2(root->right);postorder2(root->left);post.push_back(root->data);} }int main() {cin >> n;for(int i = 1; i <= n; i++) {cin >> x; vec.push_back(x);Insert(root, x); //如果給的是后續序列,則 }preorder1(root); preorder2(root);bool flag = false;/**/ if(vec == pre1) { postorder1(root); flag = true; }else if(vec == pre2) { postorder2(root); flag = true; }if(flag) {cout << "YES\n";for(int j = 0; j < n; j++) {if(j != 0) cout << ' ';cout << post[j];}return 0;}cout << "NO\n"; }
耗時:
求贊哦~
總結
以上是生活随笔為你收集整理的【题意+分析】1043 Is It a Binary Search Tree (25 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【题意分析】1042 Shuffling
- 下一篇: 【测试点2超时问题】1046 Short