PAT 1020

1020. Tree Traversals (25)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2

这题也不难,由后序跟中序序列构建一颗树,然后以层次遍历的方法输出各节点数据,主要输出后要把树给销毁,不然会造成内存泄露。
代码
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef struct Node{
 5     int data;
 6     struct Node *left,*right;
 7 }Node;
 8 int postOrder[30];
 9 int inOrder[30];
10 Node* buildTree(int,int,int,int);
11 void printAndDestroyTree(Node *);
12 int main()
13 {
14     int N,i;
15     while(scanf("%d",&N) != EOF){
16         for(i=0;i<N;++i){
17             scanf("%d",&postOrder[i]);
18         }
19         for(i=0;i<N;++i){
20             scanf("%d",&inOrder[i]);
21         }
22         Node *tree = buildTree(0,N-1,0,N-1);
23         printAndDestroyTree(tree);
24     }
25     return 0;
26 }
27 
28 Node* buildTree(int postStart,int postEnd,int inStart,int inEnd)
29 {
30     if(postStart == postEnd){
31         Node *p = (Node *)malloc(sizeof(Node));
32         p->data = postOrder[postStart];
33         p->left = p->right = NULL;
34         return p;
35     }
36     else{
37         Node *p = (Node *)malloc(sizeof(Node));
38         p->data = postOrder[postEnd];
39         p->left = p->right = NULL;
40         int i = inStart;
41         while(i<=inEnd && postOrder[postEnd] != inOrder[i++]);
42         if(i > inStart+1)
43             p->left =  buildTree(postStart,postStart+i-inStart-2,inStart,i-2);
44         if(i <= inEnd)
45             p->right = buildTree(postStart+i-inStart-1,postEnd-1,i,inEnd);
46         return p;
47     }
48 }
49 
50 void printAndDestroyTree(Node *p)
51 {
52     if(!p)
53         return;
54     Node *nodeArray[30];
55     int top=0,base=0;
56     Node *q;
57     nodeArray[top++] = p;
58     while(top>base){
59         q = nodeArray[base++];
60         if(base == 1)
61             printf("%d",q->data);
62         else
63             printf(" %d",q->data);
64         if(q->left)
65             nodeArray[top++] = q->left;
66         if(q->right)
67             nodeArray[top++] = q->right;
68         free(q);
69     }
70     printf("\n");
71 }

 

posted on 2014-02-24 21:45 Boostable 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/boostable/p/pat_1020.html

### 关于PAT乙级1020题月饼问题的解答 此题目旨在解决如何基于给定的不同种类月饼的库存量、总售价以及市场的最大需求量,计算能够获得的最大收益。对于此类优化问题,可以采用贪心算法求解。 #### 贪心算法思路 为了最大化收益,应当优先考虑单价较高的月饼。因此,首先需要计算每种月饼的单位价格(即总价除以数量),并将这些月饼按照单位价格降序排列。接着,在满足市场需求的前提下尽可能多地购买高价值密度的商品[^1]。 #### 实现方法 具体来说,可以通过以下Python代码实现上述逻辑: ```python def max_profit(n, d, stocks, prices): # 计算每种商品的价值密度,并将其与对应的索引一起存储在一个列表中 value_density = [(prices[i]/stocks[i], i) for i in range(len(stocks))] # 将所有商品按价值密度从大到小排序 sorted_items = sorted(value_density, reverse=True) total_value = 0 remaining_demand = d for vd, idx in sorted_items: if remaining_demand <= 0: break available_stock = min(remaining_demand, stocks[idx]) total_value += available_stock * vd remaining_demand -= available_stock return round(total_value, 2) if __name__ == "__main__": n, d = map(int, input().split()) stock_quantities = list(map(float, input().strip().split())) sale_prices = list(map(float, input().strip().split())) result = max_profit(n, d, stock_quantities, sale_prices) print(f"{result:.2f}") ``` 该程序接收标准输入作为参数,其中`n`代表月饼种类的数量,而`d`则指代市场上的最大需求量。之后两行分别包含了各种类月饼的具体存量及其对应的价格信息。最后输出的结果为可达到的最大利润值,保留两位小数[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值