PAT甲級(jí)1119Pre- and Post-order Traversals

題目大意

根據(jù)一個(gè)二叉樹的前序和后序遍歷序列励负,判斷此二叉樹是否唯一莺奔,并輸出此二叉樹的中序遍歷序列鸠珠,若不唯一則隨意輸出一個(gè)滿足前序與后序遍歷序列的中序序列饺蔑。

思路

二叉樹不唯一的情況是當(dāng)前序和后序遍歷只包含兩個(gè)元素時(shí)锌介,此時(shí)無法確定葉子結(jié)點(diǎn)屬于右子樹還是左子樹,只要在轉(zhuǎn)中序序列的遞歸函數(shù)中判斷當(dāng)前遞歸層中序列中元素的個(gè)數(shù)是否為兩個(gè)即可猾警。

原題

1119 Pre- and Post-order Traversals (30 分)

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.
Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

AC代碼(C++)

#include <iostream>
#include <vector>
using namespace std;
int N, pre[35], post[35], judge = 1;
vector<int>res;
void transform_(int pl, int pr, int pol, int por){
    if(por > pol){
        if(por == pol + 1)judge = 0;
        int root = post[por - 1], pos = pl;
        for(int i = pl; i <= pr; i++){
            if(pre[i] == root){
                pos = i;
                break;
            }
        }
        transform_(pl + 1, pos - 1, pol, por + pos - pr - 2);
        res.push_back(post[por]);
        transform_(pos, pr, por + pos - pr - 1, por - 1);
    }else if(pol == por) res.push_back(post[por]);
    else return;
}
int main(){
    scanf("%d", &N);
    for(int i = 0; i < N; i++)scanf("%d", &pre[i]);
    for(int i = 0; i < N; i++)scanf("%d", &post[i]);
    transform_(0, N - 1, 0, N - 1);
    printf("%s\n", judge ? "Yes" : "No");
    for(int i = 0; i < N; i++){
        if(i == 0)printf("%d", res[i]);
        else printf(" %d", res[i]);
    }printf("\n");
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末孔祸,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子发皿,更是在濱河造成了極大的恐慌崔慧,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件穴墅,死亡現(xiàn)場(chǎng)離奇詭異惶室,居然都是意外死亡温自,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門皇钞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來悼泌,“玉大人,你說我怎么就攤上這事夹界」堇铮” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵可柿,是天一觀的道長(zhǎng)鸠踪。 經(jīng)常有香客問我,道長(zhǎng)趾痘,這世上最難降的妖魔是什么慢哈? 我笑而不...
    開封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任蔓钟,我火速辦了婚禮永票,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘滥沫。我一直安慰自己侣集,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開白布兰绣。 她就那樣靜靜地躺著世分,像睡著了一般。 火紅的嫁衣襯著肌膚如雪缀辩。 梳的紋絲不亂的頭發(fā)上臭埋,一...
    開封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音臀玄,去河邊找鬼瓢阴。 笑死,一個(gè)胖子當(dāng)著我的面吹牛健无,可吹牛的內(nèi)容都是我干的荣恐。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼累贤,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼叠穆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起臼膏,我...
    開封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤硼被,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后渗磅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體祷嘶,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屎媳,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了论巍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片烛谊。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖嘉汰,靈堂內(nèi)的尸體忽然破棺而出丹禀,到底是詐尸還是另有隱情,我是刑警寧澤鞋怀,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布双泪,位于F島的核電站,受9級(jí)特大地震影響密似,放射性物質(zhì)發(fā)生泄漏焙矛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一残腌、第九天 我趴在偏房一處隱蔽的房頂上張望村斟。 院中可真熱鬧,春花似錦抛猫、人聲如沸蟆盹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)逾滥。三九已至,卻和暖如春败匹,著一層夾襖步出監(jiān)牢的瞬間寨昙,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工掀亩, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留舔哪,地道東北人困檩。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓帅刊,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親逛薇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子刹泄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容