2020-03-30

1086 Tree Traversals Again (25分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

[圖片上傳失敗...(image-36d4ad-1585574665319)]
Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

      
    

Sample Output:

3 4 2 6 5 1
#include<iostream>
#include<vector>
#include<stack>
#include<cstring>
using namespace std;
vector<int> pre,mid,back;
void postorder(int root,int start,int end){
    if(start>end){
        return;
    }
    int i = start;
    while(i<end && mid[i]!=pre[root]){
        i++;
    }
    postorder(root+1,start,i-1);
    postorder(root + 1 + i - start,i+1,end);
    back.push_back(pre[root]);
}
int main(){
    int node;
    stack<int> stack;
    cin>>node;
    string s;
    int m;
    for(int i=0;i<2*node;++i){
        cin>>s;
        if(s[1] == 'u'){
            cin>>m;
            pre.push_back(m);
            stack.push(m);
        }else{
            mid.push_back(stack.top());
            stack.pop();
        }
    }
    postorder(0,0,node-1);
    printf("%d",back[0]);
    for(int i=1;i<back.size();++i){
        printf(" %d",back[i]);
    }
    return 0;
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末还栓,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌塌衰,老刑警劉巖通殃,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件跛十,死亡現(xiàn)場離奇詭異豆巨,居然都是意外死亡蜻拨,警方通過查閱死者的電腦和手機压怠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門眠冈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人菌瘫,你說我怎么就攤上這事蜗顽〔伎ǎ” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵雇盖,是天一觀的道長忿等。 經(jīng)常有香客問我,道長崔挖,這世上最難降的妖魔是什么这弧? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮虚汛,結果婚禮上匾浪,老公的妹妹穿的比我還像新娘。我一直安慰自己卷哩,他們只是感情好蛋辈,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著将谊,像睡著了一般冷溶。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上尊浓,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天逞频,我揣著相機與錄音,去河邊找鬼栋齿。 笑死苗胀,一個胖子當著我的面吹牛,可吹牛的內容都是我干的瓦堵。 我是一名探鬼主播基协,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼菇用!你這毒婦竟也來了澜驮?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤惋鸥,失蹤者是張志新(化名)和其女友劉穎杂穷,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體卦绣,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡耐量,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了迎卤。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拴鸵。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出劲藐,到底是詐尸還是另有隱情八堡,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布聘芜,位于F島的核電站兄渺,受9級特大地震影響,放射性物質發(fā)生泄漏汰现。R本人自食惡果不足惜挂谍,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瞎饲。 院中可真熱鬧口叙,春花似錦、人聲如沸嗅战。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽驮捍。三九已至疟呐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間东且,已是汗流浹背启具。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留珊泳,地道東北人鲁冯。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像旨椒,于是被迫代替她去往敵國和親晓褪。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內容