您的位置:首页 > 其它

uva 548 Tree

2010-08-24 18:20 435 查看
好多次都re,原来是栈不够用。。。把递归改成迭代就ac了。。。


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


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <stack>
#include <cstring>
using namespace std;

#define MAX 10000
#define LEFT 0
#define RIGHT 1

int post_order[MAX];
int in_order[MAX];
int pv_post_order[MAX];
int child_post_order[2][MAX];
int sz;

int main()
{
char *pc;
char buf[50000];
int i, j;
int pv;//path value
int ip_in, iq_in;
int ip_post, iq_post;
int l_or_r;//left or right
int par;
stack<int> st;
while(true)
{
if(fgets(buf, sizeof(buf) - 1, stdin) == NULL)
break;
i = 0;
pc = strtok(buf, " /t/n");
while(pc != NULL)
{
sscanf(pc, "%d", &in_order[i++]);
pc = strtok(NULL, " /t/n");
}
sz = i;
fgets(buf, sizeof(buf) - 1, stdin);
i = 0;
pc = strtok(buf, " /t/n");
while(pc != NULL)
{
sscanf(pc, "%d", &post_order[i++]);
pc = strtok(NULL, " /t/n");
}
assert(sz == i);

/*	fprintf(stderr, "  in_order : ");
for(int k=0; k<sz; ++k)
fprintf(stderr, "%3d", in_order[k]);
fprintf(stderr, "/npost_order : ");
for(int k=0; k<sz; ++k)
fprintf(stderr, "%3d", post_order[k]);
fprintf(stderr, "/n");
*/
while(!st.empty()) st.pop();
st.push(0);
st.push(i);//in order
st.push(0);
st.push(i);//post order
st.push(-1);//parent
st.push(-1);//left child or right child
st.push(0);//path value
while(!st.empty())
{
pv = st.top();      st.pop();
l_or_r = st.top();  st.pop();
par = st.top();     st.pop();
iq_post = st.top(); st.pop();
ip_post = st.top(); st.pop();
iq_in = st.top();   st.pop();
ip_in = st.top();   st.pop();

//			fprintf(stderr, "ip_in = %d, iq_in = %d, ip_post = %d, iq_post = %d, ", ip_in, iq_in, ip_post, iq_post);
//			fprintf(stderr, "path value = %d, l_or_r = %d, parent = %d/n", pv, l_or_r, par);

if(ip_in == iq_in)
{
assert(ip_post == iq_post);
assert(par != -1);
assert(l_or_r != -1);//not a null tree

child_post_order[l_or_r][par] = -1;

//				fprintf(stderr, "continue, line = %d/n", __LINE__);

continue;
}

child_post_order[l_or_r][par] = iq_post - 1;
//	curr_sum += post_order[iq_post - 1];
//	sum_post_order[iq_post - 1] = curr_sum;
pv_post_order[iq_post - 1] = post_order[iq_post - 1] + pv;
for(i=ip_in; i<iq_in; ++i)
{
if(in_order[i] == post_order[iq_post - 1])
break;
}
assert(i != iq_in);

st.push(ip_in);
st.push(i);
st.push(ip_post);
st.push(ip_post + i - ip_in);
st.push(iq_post - 1);
st.push(LEFT);
st.push(pv_post_order[iq_post - 1]);//first half

st.push(i + 1);
st.push(iq_in);
st.push(ip_post + i - ip_in);
st.push(iq_post - 1);
st.push(iq_post - 1);
st.push(RIGHT);
st.push(pv + post_order[iq_post - 1]);//second half
}
j = 0;
for(i=1; i<sz; ++i)
{
if(pv_post_order[i] < pv_post_order[j] && child_post_order[LEFT][i] == -1 && child_post_order[RIGHT][i] == -1)
j = i;
}
fprintf(stdout, "%d/n", post_order[j]);
}
return 0;
}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: