日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)

發(fā)布時(shí)間:2024/7/23 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

思路:
根據(jù)題目數(shù)據(jù)先建立二叉搜索樹(shù);
進(jìn)行判斷:
1.是否是先序遍歷結(jié)果。
2.是否是鏡像的先序遍歷結(jié)果。

#include<iostream> #include<vector> using namespace std; struct node {int data;node* l;node* r; }; vector<int>pre, mirror_pre, post; void Insert(struct node*& root, int a)//&root,c++引用; {if (root == nullptr){root = new node();root->data = a;return;}if (a < root->data)Insert(root->l, a);else Insert(root->r, a);return; } struct node* create_tree(vector<int>v) {struct node* root = nullptr;for (int i = 0; i < v.size(); i++)//二叉搜索樹(shù)建設(shè){Insert(root, v[i]);}return root; } void preorder(struct node* root)//先序遍歷,結(jié)果保存在pre數(shù)組中 {if (root){pre.push_back(root->data);preorder(root->l);preorder(root->r);} } void postorder(struct node* root)//后序遍歷,結(jié)果保存在post數(shù)組 {if (root){postorder(root->l);postorder(root->r);post.push_back(root->data);} } void mirror_preorder(struct node* root)//鏡像先序遍歷,結(jié)果保存在mirror_pre數(shù)組中 {if (root){mirror_pre.push_back(root->data);mirror_preorder(root->r);mirror_preorder(root->l);} } void mirror_postorder(struct node* root)//鏡像后序遍歷,結(jié)果保存在post數(shù)組 {if (root){mirror_postorder(root->r);mirror_postorder(root->l);post.push_back(root->data);} } int main() {int n;cin >> n;vector<int>v(n);//v存儲(chǔ)給定的數(shù)組for (int i = 0; i < n; i++){cin >> v[i];}struct node* root = create_tree(v);//建樹(shù)preorder(root);//先進(jìn)行先序遍歷,保存結(jié)果。mirror_preorder(root);//保存鏡像先序遍歷值if (v == pre)//如果先序遍歷數(shù)組和題目給定的判別數(shù)組相同{postorder(root);//保存后續(xù)遍歷數(shù)組cout << "YES" << endl;for (int i = 0; i < post.size(); i++){if (i != 0)cout << " ";cout << post[i];}}else if (v == mirror_pre)//如果鏡像先序遍歷數(shù)組和題目給定的判別數(shù)組相同{mirror_postorder(root);//保存鏡像的后序遍歷數(shù)組cout << "YES" << endl;for (int i = 0; i < post.size(); i++){if (i != 0)cout << " ";cout << post[i];}}elsecout << "NO"; }

總結(jié)

以上是生活随笔為你收集整理的C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。