Description
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 最近才开始作数据结构和树的题 这道题算是我第一次比较清晰的解树题 遍历整颗树 求最小的值的路径 给了中序和后序排列 后序的最后一个数是根 放到中序当中分出左子树和右子树 再把子树放到后序排列中找出根 如此递归 很久没有认真做题了 shame#include <iostream> #include <cstdio> #include <cstring> #include <cctype> #include <cstdlib> using namespace std; #define N 100005 struct node{ struct node *left; struct node *right; int v; }; node *root; char s[N]; int v1[N], v2[N], top; int init(char *s, int *v){ int top = 0; for (int i = 0; s[i]; i++) { while (s[i] == ' ') i++; v[top] = 0; while (s[i] && isdigit(s[i])) { v[top] = v[top] * 10 + s[i] - '0'; // v[top] = s[i] - '0'; i++; } top++; if (!s[i]) break; } return top; } int find(int *v, int n, int c){ for (int i = n - 1; i > 0; i--) if (v[i] == c) return i; return 0; } node *build(int n, int *v1, int *v2){ if (n <= 0) return NULL; int p = find(v1, n, v2[n - 1]); node *root = (node *)malloc(sizeof(node)); root->v = v2[n - 1]; root->left = build(p, v1, v2); root->right = build(n - p -1, v1 + p + 1, v2 + p); return root; } int min_sum, ans; void dfs(node *root, int sum){ if (root == NULL) return ; sum += root->v; if (root->left == NULL && root->right == NULL){ if (min_sum > sum) { min_sum = sum; ans = root->v; } else if (min_sum == sum) ans = min(ans, root->v); return ; } dfs(root->left, sum); dfs(root->right, sum); } int main(){ while (gets(s)) { int v; init(s, v1); gets(s); top = init(s, v2); node *root; root = build(top, v1, v2); ans = min_sum = 10000000; dfs(root, 0); printf("%d\n", ans); } return 0; }
如此递归