UVA548——Tree

來源:互聯網
上載者:User

標籤:des   style   blog   color   使用   strong   

Tree

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.
Input
The input file will contain a description of the binary tree given as the inorder and postorder traversal sequences of that tree. Your program will read two line (until end of file) from the input file. The first line will contain the sequence of values associated with an inorder traversal of the tree and the second line will contain the sequence of values associated with a postorder traversal of the tree. All values will be different, greater than zero and less than 10000. You may assume that no binary tree will have more than 10000 nodes or less than 1 node.
Output
For each tree description you should output the value of the leaf node of a path of least value. In the case of multiple paths of least value you should pick the one with the least value on the terminal node.
Sample Input
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
Sample Output
1
3
255

題目大意:

    輸入一個二叉樹的中序和後序,輸出一個葉子節點,該葉子節點到根的數值總和最小。

結題思路:

    先通過後序和中序建立二叉樹,在通過DFS進行搜尋,找到符合題目要求的葉子。(需要使用全域變數來記錄DFS過程中的最小和葉子)

    中序和後序建立二叉樹:

      使用遞迴來逐步建立,由後序連確定當前遞迴中的分支的根節點,再在中序中找到根的位置,則中序中根左的為左子樹的中序排列,根右的為右子樹的中序。設此時左子樹的長度為len,則當前的後序的前len個資料是左子樹的後序排列。同理進行遞迴即可。右子樹同理。

    DFS:

      設一個變數m,每次遞迴時作為實參進入調用,並執行m+=tree.data,則能保證遞迴到葉子節點時,m儲存的是當前葉子到根節點的和,根據m的大小,即可選出符合題意的葉子節點。

Code:

 1 #include<malloc.h> 2 #include<iostream> 3 #include<stdio.h> 4 #include<string> 5 #include<cstring> 6 using namespace std; 7 struct tree 8 { 9     int data;10     int left;11     int right;12 } T[100100]; //數組類比的二叉樹13 int m_sum=100000000,pos; //用於DFS時儲存結果14 int mid[100100],last[100100];15 int create_tree(int m1,int m2,int l1,int l2)16 {17     if (m1==m2)18     {19         T[m1].left=T[m1].right=-1;20         T[m1].data=mid[m1];21         return m1;22     }23     if (m1>m2)24         return -1;25     int i;26     for (i=0; i<=m2; i++)27         if (mid[i]==last[l2]) break;28     T[i].left=create_tree(m1,i-1,l1,l1+i-m1-1);//遞迴建樹!!注意四個參數,runtime好多遍29     T[i].right=create_tree(i+1,m2,l1+i-m1,l2-1);30     T[i].data=mid[i];31     return i;32 }33 int min(int a,int b)34 {35     return a>b?b:a;36 }37 int dfs(int head,int m)38 {39     m+=T[head].data;40     if (T[head].left==-1&&T[head].right==-1)41     {42         if (m<m_sum) //打擂台,選出最小的結果,並將葉子節點的數值存入pos中43         {44             m_sum=m;45             pos=T[head].data;46         }47         return T[head].data;48     }49     int sum=T[head].data;50     if (T[head].left!=-1&&T[head].right==-1) sum+=dfs(T[head].left,m);51     else if (T[head].right!=-1&&T[head].left==-1) sum+=dfs(T[head].right,m);52     else sum+=min(dfs(T[head].left,m),dfs(T[head].right,m));53     return sum;54 }55 int main()56 {57     int k1=0,k2=0;58     while (scanf("%d",&mid[0])!=EOF)59     {60         pos=0,m_sum=100000000;61         k1=1;62         for (int i=0; i<=10000; i++)63             T[i].left=T[i].right=-1,T[i].data=0;64         while (1)65         {66             char ch=getchar();67             if (ch==‘\n‘) break;68             scanf("%d",&mid[k1++]);69         }70         k1--,k2=0;71         while (1)72         {73             scanf("%d",&last[k2++]);74             char ch=getchar();75             if (ch==‘\n‘) break;76         }77         k2--;78         int T_head=create_tree(0,k1,0,k2);79         dfs(T_head,0);80         printf("%d\n",pos);81     }82     return 0;83 }

 

聯繫我們

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