您的位置:首页 > 其它

例题6-8 树 Tree UVa 548 中序遍历+后序遍历建树

2015-05-28 00:09 369 查看
Tree

Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu
SubmitStatus

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


分析:根据中序遍历的根节点左右分别是左右右子树,后序遍历最后为根节点来建树,然后递归遍历即可。

#include<iostream>
#include<stdio.h>
#include<sstream>
#include<string>
#include<algorithm>
using namespace std;
const int maxv=10000+10;
int inorder[maxv],post[maxv],lch[maxv],rch[maxv];
int n;
//因为各个点的权值各不相同而且都是整数,直接用权值作为节点的编号
bool readlist(int * a)
{
    string line;
    if(!getline(cin,line))return false;
    stringstream ss(line);
    n=0;
    int x;
    while(ss>>x) a[n++]=x;//此处方法值得学习
    return n>0;
}
//把inorder[maxv],post[mzxv],建成一棵树,返回树根
int build(int L1,int r1,int L2,int r2)
{
    if(L1>r1) return 0;//空树
    int root=post[r2];
    int p=L1;
    while(inorder[p]!=root)p++;
    int cnt=p-L1;//左子结点的个数
    lch[root]=build(L1,p-1,L2,L2+cnt-1);
    rch[root]=build(p+1,r1,L2+cnt,r2-1);
    return root;
}
int best,bestsum;//目前为止最优解和对应的权和
void dfs(int u,int sum)
{
    sum+=u;
    if(!lch[u]&&!rch[u])
    {
        if(sum<bestsum||(sum==bestsum&&u<best))
        {
            best=u;bestsum=sum;
        }
    }
    if(lch[u])dfs(lch[u],sum);
    if(rch[u])dfs(rch[u],sum);
}
int main()
{
    while(readlist(inorder))
    {
        readlist(post);
        build(0,n-1,0,n-1);
        bestsum=10000000000;
        dfs(post[n-1],0);
        cout<<best<<endl;
    }
    return 0;
}


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 10005;
int inOrder[MAXN], postOrder[MAXN], nIndex;

struct Node{
    int data;
    Node *left;
    Node *right;
};

int nodeIndex;
Node node[MAXN];
vector<int>result;
vector<Node*>pResult;
bool flag;
int ans;

inline Node* NewNode(){
    node[nodeIndex].left = NULL;
    node[nodeIndex].right = NULL;
    return &node[nodeIndex++];
}
 
inline void input(){
    nIndex=1;
    while(getchar()!='\n') 
        scanf("%d", &inOrder[nIndex++]);
    // 输入第二行,后序遍历
    for(int i=0; i<nIndex; ++i) 
        scanf("%d", &postOrder[i]);
} 

// 由中序和后序遍历序列进行建树, 返回根结点指针
Node * InPostCreateTree(int *mid,int *post,int len){
	if(len == 0)
		return NULL;
	int i=len-1;
    while(post[len-1] != mid[i])
		--i;
	Node *h=NewNode();
	h->data=post[len-1];
	h->left=InPostCreateTree(mid,post,i);
	h->right=InPostCreateTree(mid+i+1,post+i,len-i-1);
	return h;
}

void dfs(Node *root, int n){
    if(!root->left && !root->right){
        result.push_back(n+root->data);
        pResult.push_back(root);
        return ;
    }
    if(root->left) dfs(root->left, n+root->data);
    if(root->right) dfs(root->right, n+root->data);
}

int main(){
    freopen("input.txt","r",stdin);
    while(~scanf("%d", &inOrder[0])){
        input();
        nodeIndex = 0;
        Node *root = InPostCreateTree(inOrder, postOrder, nIndex);
        result.clear();
        pResult.clear();
        dfs(root, 0);
        int minPos = 0;
        for(int i=1; i<result.size(); ++i)
            if(result[i] < result[minPos]) minPos=i;
        
        printf("%d\n",pResult[minPos]->data);
    }  
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: