uva548 tree

题意 : 建立一颗树然后计算每一条到叶子节点路径的中值和最小的,输出叶子节点值

坑爹一个最后del()多写一个一直RERERE。。

题目:

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


代码:
  1 #include <iostream>
  2 #include <string>
  3 #include <sstream>
  4 #include <limits.h>
  5 #include <malloc.h>
  6 #include <cstdio>
  7 #include <string.h>
  8 using namespace std;
  9 
 10 const int maxn = 50000;
 11 
 12 typedef struct Tnode
 13 {
 14     int data;
 15     struct Tnode* left,*right;
 16 }Node;
 17 
 18 int inorder[maxn];
 19 int postorder[maxn];
 20 int pi,pp;
 21 
 22 int num = 0;
 23 
 24 Node* newnode()
 25 {
 26     Node* T;
 27     T=(Node*)malloc(sizeof(Node));
 28     if(!T)return NULL;
 29     T->left=NULL;T->right=NULL;
 30     T->data=0;
 31     return T;
 32 }
 33 
 34 
 35 Node* build(int left,int right,int* inorder)
 36 {
 37     //cout<<"LEfT : "<<left<<"RIGHT : "<<right<<endl;
 38 
 39     //cout<<"POST :"<<pp<<"  "<<postorder[pp]<<" NExt"<<pp-1<<" "<<postorder[pp-1]<<endl;
 40     //cout<<endl;
 41 
 42 
 43     if(left>right||left<0 || right>=pi)return NULL;
 44     if(left==right)
 45     {
 46         //cout<<"here1"<<endl;
 47         Node* now;
 48         now = newnode();
 49         now->data=inorder[left];
 50         now->left=now->right=NULL;
 51         pp--;
 52         return now;
 53     }
 54 
 55 
 56     for(int i=left;i<=right;i++)
 57     {
 58         if(inorder[i]==postorder[pp-1])
 59         {
 60             //cout<<"here2"<<endl;
 61             Node* now;
 62             now = newnode();now->data= inorder[i];
 63             pp--;
 64             now->left = build(i+1,right,inorder);
 65            // if(!now->left)pp--;
 66             now->right = build (left,i-1,inorder);
 67             return now;
 68         }
 69     }
 70 
 71 }
 72 void print(Node* T)
 73 {
 74     if(T)
 75     {
 76         cout<<T->data<<" ";
 77         print(T->left);
 78         print(T->right);
 79     }
 80 }
 81 int MIN,LAST,ans;
 82 void cal(int sum,Node* T)
 83 {
 84     if(T==NULL)return;
 85     sum+=T->data;
 86     if(T->left==NULL && T->right==NULL)
 87     {
 88         if(sum<MIN)
 89         {
 90             MIN=sum;
 91             LAST= T->data;
 92             ans=LAST;
 93         }
 94         if(sum==MIN && T->data<LAST)
 95         {
 96             LAST=T->data;
 97             ans=LAST;
 98         }
 99     }
100     else
101     {
102         if(T->left)
103             cal(sum,T->left);
104         if(T->right)
105             cal(sum,T->right);
106     }
107 
108 }
109 
110 
111 void del(Node* T)
112 {
113     if(T)
114     {
115         del(T->left);
116         del(T->right);
117         free(T);
118     }
119 }
120 Node* root;
121 int main()
122 {
123     int ti;
124     char tc;
125     while(scanf("%d%c",&ti,&tc)!=EOF)
126     {
127         inorder[pi++]=ti;
128         while(tc!='\n')
129         {
130             scanf("%d%c",&ti,&tc);
131             inorder[pi++]=ti;
132         }
133         scanf("%d%c",&ti,&tc);
134         postorder[pp++]=ti;
135         while(tc!='\n')
136         {
137             scanf("%d%c",&ti,&tc);
138             postorder[pp++]=ti;
139         }
140 
141         root=build(0,pi-1,inorder);
142        // print(root);
143        // cout<<endl;
144         MIN = INT_MAX;
145         LAST = 0;
146         ans=0;
147         cal(0,root);
148         cout<<ans<<endl;
149         del(root);
150         pi=0;pp=0;
151         memset(inorder,0,sizeof(inorder));
152         memset(postorder,0,sizeof(postorder));
153         //print();
154     }
155     //del(root);
156     return 0;
157 }

 

转载于:https://www.cnblogs.com/doubleshik/p/3442432.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值