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递归中的实参的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
/************************************************** filename :a.cpp author :maksyuki created time :2018/5/30 14:48:06 last modified :2018/5/30 15:15:42 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int lch[maxn], rch[maxn], in[maxn], post[maxn]; int cnt, minval, pos; int build(int l1, int r1, int l2, int r2) { if(l1 > r1) return 0; int rt = post[r2]; int p = l1; while(in[p] != rt) p++; int pp = p - l1; lch[rt] = build(l1, p - 1, l2, l2 + pp - 1); rch[rt] = build(p + 1, r1, l2 + pp, r2 - 1); return rt; } void dfs(int rt, int sum) { sum += rt; if(!lch[rt] && !rch[rt]) { if(sum < minval || (sum == minval && rt < pos)) { minval = sum; pos = rt; } } if(lch[rt]) dfs(lch[rt], sum); if(rch[rt]) dfs(rch[rt], sum); } int main() { #ifdef LOCAL CFF; //CFO; #endif string sa, sb; while(getline(cin, sa)) { getline(cin, sb); memset(lch, -1, sizeof(lch)); memset(rch, -1, sizeof(rch)); stringstream ssa(sa); int tmp; cnt = 0; while(ssa >> tmp) { in[cnt++] = tmp; } stringstream ssb(sb); int i = 0; while(ssb >> tmp) { post[i++] = tmp; } build(0, cnt - 1, 0, cnt - 1); minval = INF, pos = -1; dfs(post[cnt-1], 0); cout << pos << endl; } return 0; } |
- « 上一篇:uva122
- uva839:下一篇 »