pat1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25)

時間限制
400 ms
內存限制
65536 kB
代碼長度限制
16000 B
判題程序
Standard
作者
CHEN, Yue

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 #include<cstdio>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<queue>
  6 #include<vector>
  7 #include<cmath>
  8 #include<string>
  9 #include<map>
 10 #include<set>
 11 using namespace std;
 12 struct node
 13 {
 14     int v;
 15     node *l,*r;
 16     node()
 17     {
 18         l=r=NULL;
 19     }
 20 };
 21 bool Buildtree(node *&h,int *line,int n)
 22 {
 23     if(n==0)
 24     {
 25         return true;
 26     }
 27     int i;
 28     h=new node();
 29     h->v=line[0];
 30     i=1;
 31     while(i<n&&line[i]<line[0])
 32     {
 33         i++;
 34     }
 35     int j=i;
 36     while(j<n&&line[j]>=line[0]){
 37         j++;
 38     }
 39     if(j!=n){
 40         return false;
 41     }
 42     return Buildtree(h->l,line+1,i-1)&&Buildtree(h->r,line+i,n-i);
 43 }
 44 
 45 bool Buildtree1(node *&h,int *line,int n)
 46 {
 47     if(n==0)
 48     {
 49         return true;
 50     }
 51     int i;
 52     h=new node();
 53     h->v=line[0];
 54     i=1;
 55     while(i<n&&line[i]>=line[0])
 56     {
 57         i++;
 58     }
 59     int j=i;
 60     while(j<n&&line[j]<line[0]){
 61         j++;
 62     }
 63     if(j!=n){
 64         return false;
 65     }
 66     return Buildtree1(h->l,line+1,i-1)&&Buildtree1(h->r,line+i,n-i);//黏貼復制害死人
 67 }
 68 void Postorder(node *&h)
 69 {
 70     if(h)
 71     {
 72         Postorder(h->l);
 73         Postorder(h->r);
 74         printf("%d ",h->v);
 75         delete []h;
 76     }
 77 }
 78 int line[1005];
 79 int main()
 80 {
 81     //freopen("D:\\INPUT.txt","r",stdin);
 82     int n;
 83     while(scanf("%d",&n)!=EOF)
 84     {
 85         node *h;
 86         int i;
 87         for(i=0; i<n; i++)
 88         {
 89             scanf("%d",&line[i]);
 90         }
 91         if(n==1){
 92             printf("YES\n");
 93             printf("%d\n",line[0]);
 94             continue;
 95         }
 96         if(line[0]>line[1]&&Buildtree(h,line,n))//BST
 97         {
 98             printf("YES\n");
 99             Postorder(h->l);
100             Postorder(h->r);
101             printf("%d\n",h->v);
102             continue;
103         }
104         if(line[0]<=line[1]&&Buildtree1(h,line,n))
105         {
106             printf("YES\n");
107             Postorder(h->l);
108             Postorder(h->r);
109             printf("%d\n",h->v);
110             continue;
111         }
112         printf("NO\n");
113     }
114     return 0;
115 }

?

轉載于:https://www.cnblogs.com/Deribs4/p/4770374.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/258408.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/258408.shtml
英文地址,請注明出處:http://en.pswp.cn/news/258408.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

微軟待辦應用更新

微軟做了一些更改和優化來改進微軟待辦。 為了在所有設備上獲得最佳體驗&#xff0c;需確保移動和桌面微軟待辦2021 年 12 月 31日之前的版本為 2.49 或更高版本&#xff0c;否則微軟待辦不再支持跨設備同步&#xff0c;但仍然能脫機使用。 桌面版的微軟待辦應用下載地址為&…

出租WiFi到底靠不靠譜?

創業是一種心態&#xff0c;也是不斷的探索&#xff0c;他融入我們的生活&#xff0c;從日常中積累&#xff0c;從小微處啟航。 一、背景交代 最近在換工作&#xff0c;本周搬到新租的單身公寓&#xff0c;空間不大&#xff0c;倒是干凈整潔。委托租房中介幫忙開通寬帶&#xf…

AD20學習筆記1---元件庫的創建

前言&#xff1a; 本文學習視頻是B站點擊率第一的凡億教育《Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設計視頻教程》&#xff0c;視頻地址&#xff1a;Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設…

nodejs環境搭建與express安裝配置

一、NPM 1、下載nodeJS 下載地址&#xff1a;https://nodejs.org/en/download/ 因為我的系統是Linux 的&#xff0c;所以下載已經編譯好的Linux&#xff0c;nodejs tar包 3、下載完成過后放到/usr/local/下面 4、解壓&#xff1a;因為這個包不是gz的包所以解壓 正確&#xff1a…

在vue中實現picker樣式_基于Vue實現timepicker

主要用到的還是Vue的基本知識而已&#xff0c;不過要想到的細節很多。先放效果&#xff0c;點擊上框&#xff0c;顯示timepicker。而且可以根據點擊的是時還是分來改變圓盤的數字。這里我用了兩個組件&#xff0c;和&#xff0c;這里的時和分的數值我掛在了根實例中&#xff0c…

玩玩

金字塔一樣輸出字母&#xff0c;如 輸入 d a a b a a b c b a a b c d c b a 代碼實現 #include<stdio.h> int main(void) { char z; int j,t,k; scanf("%c",&z); t0; if(z>a&&z<z) { for(int i0;i<z-a;i) { for(kz-a-t;k…

總結界面框架_UI_Adapter

本人定期更新經典案例及解決方案如有疑問請聯系我QQ1822282728 -- 277627117 下面是常用到的ui Demo安卓三級篩選菜單listview&#xff08;非常經典&#xff09; http://download.csdn.net/detail/zillvip/9138975android地圖應用&#xff08;路徑規劃&#xff0c;地理編碼&…

AD20學習筆記2---原理圖繪制及編譯檢查

前言&#xff1a; 本文學習視頻是B站點擊率第一的凡億教育《Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設計視頻教程》&#xff0c;視頻地址&#xff1a;Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設…

git如何設置master分支的權限_Git 從master 分支拉新分支開發

一、 切換到被copy的分支(master)&#xff0c;并且從遠端拉取最新版本$git checkout master$git pull二、從當前分支拉copy開發分支$git checkout -b devSwitched to a new branch dev三、 把新建的分支push到遠端$git push origin dev四、拉取遠端分支$git pullThere is no tr…

Yii框架 phpexcel 導出

一、說明 之前使用的是PHPExcelXML包實現的數據導出&#xff0c;由于導出的文件擴展名為“.xls” 在office2007上帶不開&#xff0c;報如下圖錯誤&#xff08;用 WPS都能打開&#xff09; 因此&#xff0c;此次采用了 PHPExcel包 不僅支持生成Excel&#xff08;.xls&#xff09…

慎用stl中的erase的返回值

在windows下的VC編譯或者Mac OX的XCode下編譯也許不會出問題。但是在linux下可能就會掛掉。 比如我上一篇里的poj4093出現了編譯錯誤 2007120.8890/Main.cc: In function ‘int main()’: 2007120.8890/Main.cc:50:44: error: no match for ‘operator’ in ‘itr1 a.std::set…

AD20學習筆記3---PCB封裝庫的創建方法及現有封裝調用

前言&#xff1a; 本文學習視頻是B站點擊率第一的凡億教育《Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設計視頻教程》&#xff0c;視頻地址&#xff1a;Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設…

php的兩種復合數據類型是什么意思_2.4PHP復合數據類型:數組和對象

Posted by 撒得一地 on 2015年9月29日 in PHP入門教程國外穩定加速器推薦vypr |NordPHP中復合數據類型包括兩種&#xff0c;即數組和對象。array(數組)&#xff1a;一組數據的集合。object(對象)&#xff1a;對象是類型的實例&#xff0c;使用new命令來創建。數組(array)數組是…

Python守護進程和腳本單例運行

2019獨角獸企業重金招聘Python工程師標準>>> 一、簡介 守護進程最重要的特性是后臺運行&#xff1b;它必須與其運行前的環境隔離開來&#xff0c;這些環境包括未關閉的文件描述符、控制終端、會話和進程組、工作目錄以及文件創建掩碼等&#xff1b;它可以在系統啟動…

分析access.log

cat access.log | awk {print $4,$1,$9} | awk -F/ {print $3}| awk -F: {print $2 ":" $3,$4} | awk {print $1,$3,$4} | uniq -c | sort -n轉載于:https://www.cnblogs.com/olderblue/p/4778339.html

AD20學習筆記4---網表導入及模塊化布局設計

前言&#xff1a; 本文學習視頻是B站點擊率第一的凡億教育《Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設計視頻教程》&#xff0c;視頻地址&#xff1a;Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設…

Paoding-Rose學習

* HttpServletRequest.getContextPath 獲取web程序root。如果是默認位置&#xff0c;返回””空串&#xff0c;否則返回 /根路徑名 * rose是如何掃描到資源的 利用spring提供的類掃描類和jar* rose建立匹配樹的過程 傳入根節點和List&#xff0c;按照路徑建立每個節點 * Module…

楪祈機器人_饑荒 Inori楪祈人物MOD V20161211

使用說明&#xff1a;1.解壓縮2.復制所有文件到游戲目錄mods3.啟動游戲&#xff0c;點擊mods(模組)加載MOD適用游戲版本&#xff1a;理論上支持所有版本的饑荒(普通&#xff0c;巨人&#xff0c;海難&#xff0c;聯機版)MOD說明&#xff1a;饑荒 Inori楪祈人物MOD&#xff1b;由…

javascript 模塊化

2019獨角獸企業重金招聘Python工程師標準>>> 一直好奇像node.js,require.js的模塊化是怎么做的&#xff0c;在看了《你不知道的javascript》后&#xff0c;對js的模塊化有了一些簡單的了解。這本書真的還不錯。 書里講述了js的模塊化的原理 和 現代js實現模塊化的簡…

AD20學習筆記5---PCB設計規則設置及PCB手工布線

前言&#xff1a; 本文學習視頻是B站點擊率第一的凡億教育《Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設計視頻教程》&#xff0c;視頻地址&#xff1a;Altium Designer 20 19&#xff08;入門到精通全38集&#xff09;四層板智能車PCB設…