• UVA-548Tree(二叉树的递归遍历)


    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

     Status

    Description

    Download as PDF
     

    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<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    #define SI(x) scanf("%d",&x)
    #define mem(x,y) memset(x,y,sizeof(x)) 
    #define PI(x) printf("%d",x)
    #define P_ printf(" ")
    const int INF=0x3f3f3f3f;
    typedef long long LL;
    const int MAXN=100010;
    int v1[MAXN],v2[MAXN];
    char s[MAXN];
    int ans,minlength;
    struct Node{
    	int v;
    	Node *L,*R;
    	Node(){
    		L=NULL;R=NULL;
    	}
    };
    int init(char *s,int *v){
    	int k=0;
    	for(int i=0;s[i];i++){
    		while(isdigit(s[i]))v[k]=v[k]*10+s[i++]-'0';
    		k++;
    	}
    //	for(int i=0;i<k;i++)printf("%d ",v[i]);puts("");
    	return k;
    }
    int find(int n,int *v,int t){
    	for(int i=n-1;i>=0;i--)
    		if(v[i]==t)return i;
    		 return 0;
    }
    Node* build(int n,int *v1,int *v2){
    	Node *root;
    	if(n<=0)return NULL;
    	root=new Node;
    	root->v=v2[n-1];
    	int p=find(n,v1,v2[n-1]);
    	root->L=build(p,v1,v2);
    	root->R=build(n-p-1,v1+p+1,v2+p);
    	return root;
    }
    void dfs(Node *root,int length){
    	if(root==NULL)return;
    	length+=root->v;
    	if(root->L==NULL&&root->R==NULL){
    		if(minlength>length)
            {
                minlength=length;
                ans=root->v;
            }
            else if(minlength==length)
            ans=min(ans,root->v);
    		return;
    	}
    	dfs(root->L,length);
    	dfs(root->R,length);
    }
    int main(){
    	while(gets(s)){
    		mem(v1,0);mem(v2,0);
    		init(s,v1);
    		gets(s);
    		int k=init(s,v2);
    		Node *root=build(k,v1,v2);
    		ans=minlength=INF;
    		dfs(root,0);
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

      


  • 相关阅读:
    gitlab第一次提交
    Command 'cnpm' not found解决方法
    ubuntu18.04安装sublime
    django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured.
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'. Did you install mysqlclient or MySQL-python?
    ubuntu18.04安装MySQL
    ubuntu18.04彻底卸载mysql
    rabbitmq高可用
    ASP.NET Global.asax详解
    如何真正提高ASP.NET网站的性能
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5096983.html
Copyright © 2020-2023  润新知