uva_ 548-Tree

  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 <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;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值