UVa 548 – Tree 二叉樹的重建——中序遍曆與後續遍曆進行建樹

來源:互聯網
上載者:User

548 - Tree 4606 28.96% 974 77.21%

題目連結:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489


題目類型: 資料結構, 二叉樹



題目大意:

給兩個組數字,都是在同一棵二叉樹上的,第一組是按中序遍曆(inorder)順序輸出的,第二組是按後序遍曆(postorder)輸出的,
根據這兩組資料構建出原來的二叉樹,然後計算從根結點到每個葉子結點的路徑上的數字之和, 輸出最小之和。


範例輸入:

3 2 1 4 5 7 63 1 2 5 6 7 47 8 11 3 5 16 12 188 3 11 7 16 18 12 5255255

範例輸出:

13255


分析:

這題就是運用了二叉樹重建, 以及遍曆。

二叉樹的遍曆:先序遍曆,中序遍曆,後序遍曆
只要有一個中序序列再加上另一個序列就可唯一地重建原來二叉樹。 

進行了二叉樹重建之後,只要對這棵二叉樹進行搜尋, 取得各個路徑之和,然後找出最小的那個和即可。



由中序遍曆 分別和前序走訪,後序遍曆進行建樹的方法:

// 由中序和後序遍曆序列進行建樹, 返回根結點指標Node * InPostCreateTree(int *mid,int *post,int len){if(len == 0)return NULL;int i=len-1;    while(post[len-1] != mid[i])--i;Node *h=NewNode();h->data=post[len-1];h->left=InPostCreateTree(mid,post,i);h->right=InPostCreateTree(mid+i+1,post+i,len-i-1);return h;} // 由前序和中序遍曆序列進行建樹, 返回根結點的指標Node * PreInCreateTree(int *mid,int *pre,int len)//n標識s2的長度{     if(len==0)    return NULL;        int i = 0;    while(*mid != pre[i])++i;    Node *h=NewNode();    h->data= *mid;    h->left  = PreInCreateTree(mid+1, pre, i);    h->right = PreInCreateTree(mid+i+1, pre+i+1, len-i-1);    return h;}

AC代碼:

#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int MAXN = 10005;int inOrder[MAXN], postOrder[MAXN], nIndex;class Node{public:    int data;    Node *left;    Node *right;};int nodeIndex;Node node[MAXN];vector<int>result;vector<Node*>pResult;bool flag;int ans;inline Node* NewNode(){    node[nodeIndex].left = NULL;    node[nodeIndex].right = NULL;    return &node[nodeIndex++];} inline void input(){    nIndex=1;    while(getchar()!='\n')         scanf("%d", &inOrder[nIndex++]);    // 輸入第二行,後序遍曆    for(int i=0; i<nIndex; ++i)         scanf("%d", &postOrder[i]);} // 由中序和後序遍曆序列進行建樹, 返回根結點指標Node * InPostCreateTree(int *mid,int *post,int len){if(len == 0)return NULL;int i=len-1;    while(post[len-1] != mid[i])--i;Node *h=NewNode();h->data=post[len-1];h->left=InPostCreateTree(mid,post,i);h->right=InPostCreateTree(mid+i+1,post+i,len-i-1);return h;}void dfs(Node *root, int n){    if(!root->left && !root->right){        result.push_back(n+root->data);        pResult.push_back(root);        return ;    }    if(root->left) dfs(root->left, n+root->data);    if(root->right) dfs(root->right, n+root->data);}int main(){    freopen("input.txt","r",stdin);    while(~scanf("%d", &inOrder[0])){        input();        nodeIndex = 0;        Node *root = InPostCreateTree(inOrder, postOrder, nIndex);        result.clear();        pResult.clear();        dfs(root, 0);        int minPos = 0;        for(int i=1; i<result.size(); ++i)            if(result[i] < result[minPos]) minPos=i;                printf("%d\n",pResult[minPos]->data);    }      return 0;}


——      生命的意義,在於賦予它意義。 

                   原創  http://blog.csdn.net/shuangde800  , By
  D_Double



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.