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.
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.
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.
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
1
3
255
题目要求是给你一个二叉树的中序遍历和后序遍历,找出路径和最小对应的路径叶子节点。刚开始最简单的想法是直接根据后序遍历和中序遍历递归地构造一颗二叉树,然后遍历找出最小的叶子节点
#include <iostream>
#include <cstdio>
using namespace std;
int inOrder[20001];
int postOrder[20001];
struct Node
{
int data;
Node * left;
Node * right;
};
typedef Node * Position;
Position buildTree( int inOrder_[], int a, int b, int postOrder_[], int m, int n)
{
Position root = NULL;
if( a > b) return root;
root = new Node;
root->data = postOrder_[n];
int na = a, nb = a-1;
for( int i = a; i <= b; i++)
if( inOrder_[i] == root->data){
nb = i-1;
break;
}
int nm = m, nn = m+nb-na;
root->left = buildTree( inOrder_, na, nb, postOrder_, nm, nn);
root->right = buildTree( inOrder_, nb+2, b, postOrder_, nn+1, n-1);
return root;
}
int leastSum = 100000000, leastNode;
void getSum( Position root_, int sum)
{
if( !root_->left && !root_->right) {
if( sum < leastSum){
leastSum = sum;
leastNode = root_->data;
}
else if( sum == leastSum) leastNode = min( leastNode, root_->data);
}
if( root_->left) getSum( root_->left, sum+root_->left->data);
if( root_->right) getSum( root_->right, sum+root_->right->data);
}
void makeEmpty( Position & t)
{
if( !t) return;
makeEmpty( t->left);
makeEmpty( t->right);
delete t;
}
int main()
{
int n, cnt;
char ch;
while( scanf( "%d%c",&n, &ch) != EOF){
cnt = 0;
inOrder[cnt++] = n;
while( ch != '\n'){
scanf( "%d%c", &n, &ch);
inOrder[cnt++] = n;
}
ch = 0, cnt = 0;
while( ch != '\n'){
scanf( "%d%c", &n, &ch);
postOrder[cnt++] = n;
}
Position root = buildTree( inOrder, 0, cnt-1, postOrder, 0, cnt-1);
getSum( root, root->data);
cout << leastNode << endl;
leastSum = 100000000;
}
return 0;
}
后来交完ac想想其实建树的过程中就是遍历,所以改了一下建立的时候直接求和,省去了遍历的麻烦
#include <iostream>
#include <cstdio>
using namespace std;
int inOrder[20001];
int postOrder[20001];
struct Node
{
int data;
Node * left;
Node * right;
};
typedef Node * Position;
int leastSum = 100000000, leastNode;
Position buildTree( int sum, int inOrder_[], int a, int b, int postOrder_[], int m, int n)
{
Position root = NULL;
if( a > b) return root;
root = new Node;
root->data = postOrder_[n];
sum += root->data;
int na = a, nb = a-1;
for( int i = a; i <= b; i++)
if( inOrder_[i] == root->data){
nb = i-1;
break;
}
int nm = m, nn = m+nb-na;
root->left = buildTree( sum, inOrder_, na, nb, postOrder_, nm, nn);
root->right = buildTree( sum, inOrder_, nb+2, b, postOrder_, nn+1, n-1);
if ( !root->left && !root->right){
if( sum < leastSum) {
leastSum = sum;
leastNode = root->data;
}
else if( sum == leastSum) leastNode = min( leastNode, root->data);
}
return root;
}
void makeEmpty( Position & t)
{
if( !t) return;
makeEmpty( t->left);
makeEmpty( t->right);
delete t;
}
int main()
{
int n, cnt;
char ch;
while( scanf( "%d%c",&n, &ch) != EOF){
cnt = 0;
inOrder[cnt++] = n;
while( ch != '\n'){
scanf( "%d%c", &n, &ch);
inOrder[cnt++] = n;
}
ch = 0, cnt = 0;
while( ch != '\n'){
scanf( "%d%c", &n, &ch);
postOrder[cnt++] = n;
}
Position root = buildTree( 0,inOrder, 0, cnt-1, postOrder, 0, cnt-1);
makeEmpty( root);
cout << leastNode << endl;
leastSum = 100000000;
}
return 0;
}
<span style="white-space:pre"> </span>修改后发现其实只用上了节点的data,其他根本不需要用,才醒悟过来只要求和他们的值就好了,没有必要真的建树。。。
<pre name="code" class="cpp">#include <iostream>
#include <cstdio>
using namespace std;
int inOrder[10010];
int postOrder[10010];
int leastSum = 100000000, leastNode;
void buildTree( int sum, int inOrder_[], int a, int b, int postOrder_[], int m, int n)
{
if( a > b) return ;
sum += postOrder_[n];
int na = a, nb = a-1;
for( int i = a; i <= b; i++)
if( inOrder_[i] == postOrder_[n]){
nb = i-1;
break;
}
int nm = m, nn = m+nb-na;
buildTree( sum, inOrder_, na, nb, postOrder_, nm, nn);
buildTree( sum, inOrder_, nb+2, b, postOrder_, nn+1, n-1);
if ( a == b){
if( sum < leastSum) {
leastSum = sum;
leastNode = postOrder_[n];
}
else if( sum == leastSum) leastNode = min( leastNode, postOrder_[n]);
}
}
int main()
{
int n, cnt;
char ch;
while( scanf( "%d%c",&n, &ch) != EOF){
cnt = 0;
inOrder[cnt++] = n;
while( ch != '\n'){
scanf( "%d%c", &n, &ch);
inOrder[cnt++] = n;
}
ch = 0, cnt = 0;
while( ch != '\n'){
scanf( "%d%c", &n, &ch);
postOrder[cnt++] = n;
}
buildTree( 0,inOrder, 0, cnt-1, postOrder, 0, cnt-1);
cout << leastNode << endl;
leastSum = 100000000;
}
return 0;
}