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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【题意+分析】1043 Is It a Binary Search Tree (25 分)

發布時間:2024/2/28 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【题意+分析】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 分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久久激情视频 | 国模小黎自慰gogo人体 | 94av视频| 国产欧美三区 | 日本久久久久久久久 | 精品在线视频一区 | 欧美人妖老妇 | 免费网站在线观看黄色 | 亚洲精品一级 | 先锋影音一区二区 | 免费欧美黄色片 | 三级理论电影 | 久久国产美女 | 国产无遮挡a片又黄又爽 | 一本一道波多野结衣一区二区 | 欧美在线你懂的 | 91精品网站 | 日韩精品极品 | 国产美女引诱水电工 | 一本色道久久综合狠狠躁的推荐 | 国产精品污www一区二区三区 | 成人小视频在线免费观看 | 在线免费观看亚洲视频 | 成年人在线观看av | 男女插孔视频 | 精品人妻无码在线 | 国产级毛片 | 一区二区韩国 | 中文字幕专区 | 九色在线播放 | 在线观看网址你懂的 | 另类ts人妖一区二区三区 | 日本伦理片在线播放 | 黑人操日本 | 国产黄在线免费观看 | 91手机在线视频 | 99国产精品视频免费观看一公开 | 日本黄图 | 日韩福利影院 | 日本一区二区三区在线观看视频 | 日本不卡视频在线观看 | 人妻少妇精品无码专区二区 | 国产在线观看av | 国产精品一区二区在线观看 | 韩国三级视频在线 | 日皮视频免费观看 | 狠狠躁日日躁 | 不卡一区二区在线观看 | 欧美成人视 | 激情麻豆 | 不卡日韩 | 按摩毛片 | 97在线观看免费高清 | 蜜桃久久精品成人无码av | 中国成人毛片 | 国产综合图区 | 亚洲欧美综合精品久久成人 | 国产日韩欧美另类 | 91精品免费在线 | 亚洲国产欧美日韩 | 91久久精品国产91性色69 | 久久成人免费视频 | a级黄色录像| 日本xx片| 91精品久久久久久久 | 免费看污的网站 | 精品国产一区三区 | 欧洲中文字幕日韩精品成人 | 五月天激情综合网 | 好色先生视频污 | 久久免费福利视频 | 国产精品久久婷婷 | 黄色a大片 | 黄污视频在线观看 | 一级黄色免费毛片 | 在线黄网 | 亚洲一区免费视频 | 久久久久久久久久久久久久免费看 | 五月天激情丁香 | 噼里啪啦国语电影 | 免费污视频 | 成人黄色片网站 | 图片区亚洲色图 | 国产精品三级 | a级淫片| 国产做爰xxxⅹ久久久精华液 | 国产电影一区二区三区 | 亚洲熟女乱色一区二区三区 | 亚洲成人 av| 福利午夜视频 | 日韩欧美在线精品 | 麻豆av一区二区 | 日韩欧美视频免费在线观看 | 国产一级片免费视频 | 极品国产91在线网站 | 成人黄色三级 | 18p在线观看 | 在线中文字幕播放 | 高清人妖shemale japan |