九度OJ 1078 二叉樹遍曆__九度OJ

來源:互聯網
上載者:User

題目地址:http://ac.jobdu.com/problem.php?pid=1078

題目描述:

二叉樹的前序、中序、後序遍曆的定義:
前序走訪:對任一子樹,先訪問跟,然後遍曆其左子樹,最後遍曆其右子樹;
中序遍曆:對任一子樹,先遍曆其左子樹,然後訪問根,最後遍曆其右子樹;
後序遍曆:對任一子樹,先遍曆其左子樹,然後遍曆其右子樹,最後訪問根。
給定一棵二叉樹的前序走訪和中序遍曆,求其後序遍曆(提示:給定前序走訪與中序遍曆能夠唯一確定後序遍曆)。 輸入:

兩個字串,其長度n均小於等於26。
第一行為前序走訪,第二行為中序遍曆。
二叉樹中的結點名稱以大寫字母表示:A,B,C....最多26個結點。 輸出:

輸入範例可能有多組,對於每組測試範例,
輸出一行,為後序遍曆的字串。 範例輸入:

ABCBACFDXEAGXDEFAG
範例輸出:
BCAXEDGAF
重建二叉樹

/* * Main.c * *  Created on: 2014年1月27日 *      Author: Shaobo */#include <stdio.h>#include <string.h> void to_post(char pre[], char in[], char post[], int len){    int i;     if (len <= 0)        return;     for (i=0; i<len; ++i)        if (in[i] == pre[0])            break;    post[len-1] = pre[0];    to_post (pre+1, in, post, i);    to_post (pre+i+1, in+i+1, post+i, len-i-1);} int main(void){    char pre[30], in[30], post[30];    int len;     while (scanf ("%s", pre) != EOF){        scanf ("%s", in);        len = strlen (pre);        to_post (pre, in, post, len);        post[len] = '\0';        printf ("%s\n", post);    }     return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.