uva00548,

# 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
```

Miguel A. Revilla
1999-01-11

Solution:
Using recursive to reconstruct the tree.
PS: Don't use too many push_back() function when you need to insert elements into vectors; It will cost you a lot to TLE.
Alert: my solution doesn't pass the OJ, even I believe it's right.
If anybody could figure it out which part I did wrong, pleassssssssssssssse make a comment.
This problem really took me a lot of time.
Thank you.

## Source Code:

//Mon May  3 21:32:23 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

#define inf 100000000;
using namespace std;

class Node {
public:
char data;
Node *left;
Node *right;

Node(char str) {
data = str;
left = NULL;
right = NULL;
}
};

int mmin;
int leaf;

void reconstruction(Node *&root, vector<int> inorder, vector<int> postorder) {
int N = inorder.size();
if (N == 0) {
root = NULL;
return;
}

int last = N - 1;
root = new Node(postorder[last]);

if (N == 1) {
root->left = NULL;
root->right = NULL;
return;
}
vector<int>::iterator found = find_end(inorder.begin(), inorder.end(),
postorder.begin() + last, postorder.end());
vector<int> inorderA(inorder.begin(), found);
vector<int> inorderB(++found, inorder.end());
vector<int> postorderA(postorder.begin(), postorder.begin()
+ inorderA.size());
vector<int> postorderB(postorder.begin() + postorderA.size(),
postorder.end() - 1);
//  cout << "inorder A: ";
//  for (int i = 0; i < inorderA.size(); i++)
//      cout << inorderA[i] << ", ";
//  cout << endl;
//  cout << "inorder B: ";
//  for (int i = 0; i < inorderB.size(); i++)
//      cout << inorderB[i] << ", ";
//  cout << endl;
//  cout << "postorder A: ";
//  for (int i = 0; i < postorderA.size(); i++)
//      cout << postorderA[i] << ", ";
//  cout << endl;
//  cout << "postorder B: ";
//  for (int i = 0; i < postorderB.size(); i++)
//      cout << postorderB[i] << ", ";
//  cout << endl;
reconstruction(root->left, inorderA, postorderA);
reconstruction(root->right, inorderB, postorderB);
return;
}

void minLeaf(Node *root, int sum) {
if (root == NULL)
return;
if (root->left == NULL && root->right == NULL) {
if (mmin > root->data + sum) {
mmin = root->data + sum;
leaf = root->data;
} else if (mmin == root->data + sum) {
if (leaf == -1 || leaf > root->data)
leaf = root->data;
}
return;
}
minLeaf(root->left, sum + root->data);
minLeaf(root->right, sum + root->data);
}

int main(int argc, char* argv[]) {
freopen("input.in", "r", stdin);
//  freopen("output.out", "w", stdout);
string str1, str2;
while (getline(cin, str1, '\n') && getline(cin, str2, '\n')) {
stringstream s1(str1);
stringstream s2(str2);
int number;
vector<int> inorder;
vector<int> postorder;
while (s1 >> number) {
inorder.push_back(number);
}
while (s2 >> number) {
postorder.push_back(number);
}
//      for (int i = 0; i < inorder.size(); i++)
//          cout << inorder[i] << ",";
//      cout << endl;
//      for (int j = 0; j < postorder.size(); j++)
//          cout << postorder[j] << ",";
//      cout << endl;

if (inorder.size() == 1) {
cout << inorder << endl;
continue;
}

Node *root;
root = NULL;
reconstruction(root, inorder, postorder);
//      cout << root->data << endl;
mmin = inf;
leaf = -1;
minLeaf(root, 0);
cout << leaf << endl;
}
//  fclose(stdin);
//  fclose(stdout);
return 0;
}