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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

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

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

思路:
根據題目數據先建立二叉搜索樹;
進行判斷:
1.是否是先序遍歷結果。
2.是否是鏡像的先序遍歷結果。

#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++)//二叉搜索樹建設{Insert(root, v[i]);}return root; } void preorder(struct node* root)//先序遍歷,結果保存在pre數組中 {if (root){pre.push_back(root->data);preorder(root->l);preorder(root->r);} } void postorder(struct node* root)//后序遍歷,結果保存在post數組 {if (root){postorder(root->l);postorder(root->r);post.push_back(root->data);} } void mirror_preorder(struct node* root)//鏡像先序遍歷,結果保存在mirror_pre數組中 {if (root){mirror_pre.push_back(root->data);mirror_preorder(root->r);mirror_preorder(root->l);} } void mirror_postorder(struct node* root)//鏡像后序遍歷,結果保存在post數組 {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存儲給定的數組for (int i = 0; i < n; i++){cin >> v[i];}struct node* root = create_tree(v);//建樹preorder(root);//先進行先序遍歷,保存結果。mirror_preorder(root);//保存鏡像先序遍歷值if (v == pre)//如果先序遍歷數組和題目給定的判別數組相同{postorder(root);//保存后續遍歷數組cout << "YES" << endl;for (int i = 0; i < post.size(); i++){if (i != 0)cout << " ";cout << post[i];}}else if (v == mirror_pre)//如果鏡像先序遍歷數組和題目給定的判別數組相同{mirror_postorder(root);//保存鏡像的后序遍歷數組cout << "YES" << endl;for (int i = 0; i < post.size(); i++){if (i != 0)cout << " ";cout << post[i];}}elsecout << "NO"; }

總結

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

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

主站蜘蛛池模板: 久草网视频在线观看 | 1000亚洲裸体人体 | 天堂中文字幕在线观看 | 蜜乳av中文字幕 | 国产精品久久久久久三级 | 九月丁香婷婷 | 美女88av| 中文字幕在线一区 | 免费播放黄色片 | 精品一区电影 | 国产熟女一区二区三区四区 | 日韩免费视频 | 国产网站在线免费观看 | 日本公妇乱淫免费视频一区三区 | 国产美女精品视频 | 91色偷偷| 欧美男女交配 | 手机在线看永久av片免费 | 中文字幕素人 | 亚洲一区二区免费视频 | 爱情岛论坛亚洲自拍 | 欧美在线一区二区视频 | 欧美第七页 | 18女人毛片| 亚洲精品国产精品国 | 伊人中文字幕在线观看 | 91中文字幕在线观看 | 丰满少妇高潮久久三区 | 99久久久国产精品免费蜜臀 | 中出一区二区 | 国产激情免费视频 | 日屁网站 | 蜜臀精品一区二区三区 | 爱爱视频在线播放 | 综合久久精品 | 国产成人一区二区三区视频 | 亚欧色视频 | 国产精品久久久av | 久久高清无码视频 | 黄色三级大片 | 欧美日韩国产成人在线 | 精品一二三区久久aaa片 | 偷拍老头老太高潮抽搐 | a网站在线观看 | 国产精品色 | 中文字幕无码人妻少妇免费 | 国产成人在线观看免费 | av天堂永久资源网 | 黄色一极视频 | 国产调教视频在线观看 | 天天色综合av | 国产三区在线观看 | 与子敌伦刺激对白播放的优点 | 国产熟妇一区二区三区aⅴ网站 | 欧美日韩电影一区二区 | 欧美精品一区二区三区久久久 | 瑟瑟综合网| 天堂аⅴ在线最新版在线 | 一本一道波多野结衣一区二区 | 91毛片网| 国产污污视频在线观看 | 午夜精品999 | 捆绑调教在线观看 | 色妞欧美| 69av视频在线| 中文字幕 亚洲一区 | 性生活视屏 | 奇米影视在线观看 | 蜜臀国产AV天堂久久无码蜜臀 | 黄色正能量网站 | 98堂 最新网名 | 久久久久久一区二区三区 | 2020亚洲男人天堂 | 日韩精品视频一区二区在线观看 | 国产一区二区三区免费观看 | 日韩黄色高清视频 | 综合在线亚洲 | 成人午夜影院 | 亚洲免费色| 豆花视频成人 | 色咪咪网站 | aaa日韩 | av在线高清观看 | a级大片在线观看 | 欧美另类xxx| 国产女人被狂躁到高潮小说 | 免费黄色在线网站 | 在线成人福利 | 国产一区二区三区精品视频 | 色老头一区二区三区 | 一卡二卡三卡四卡五卡 | 美女擦边视频 | 五月婷婷社区 | 69精品人人 | 蜜桃视频色 | 15p亚洲| 致命魔术电影高清在线观看 | 毛片毛片毛片毛片毛片毛片毛片毛片毛片毛片 | 婷婷国产一区 |