[刷题] PTA 03-树3 Tree Traversals Again
用栈实现树遍历
1 #include<stdio.h>
2 #include<string.h>
3 #define MAXSIZE 30
4
5 int Pre[MAXSIZE],In[MAXSIZE],Post[MAXSIZE];
6 void solve(int preL,int inL,int postL,int n);
7 void outPut(int p[],int n);
8
9 int main(){
10 int n,tmp,i,j = 0;
11 int topPre = -1;
12 int topIn = -1;
13 const char *push = "Push";
14 char cnt[5];
15 int st[MAXSIZE];
16 scanf("%d",&n);
17 for(i=0; i<2*n; i++){
18 scanf("\n%s",cnt);
19 if(!strcmp(cnt,push)){
20 scanf("%d",&tmp);
21 Pre[j++] = tmp;
22 st[++topPre] = tmp;
23 }else{
24 In[++topIn] = st[topPre--];
25 }
26 }
27 solve(0,0,0,n);
28 outPut(Post,n);
29 return 0;
30 }
31
32 void solve(int preL, int intL, int postL, int n){
33 int i,L,R,root;
34 if(n==0) return;
35 if(n==1) {Post[postL] = Pre[preL]; return;}
36 root = Pre[preL];
37 Post[postL+n-1] = root;
38 for(i=0; i<n; i++) if(In[intL+i]==root) break;
39 L = i;R = n-L-1;
40 solve(preL+1, intL, postL, L);
41 solve(preL+L+1, intL+L+1, postL+L, R);
42 }
43
44 void outPut(int p[],int n){
45 int i;
46 for(i=0; i<n; i++){
47 if(!i) printf("%d",p[i]);
48 else printf(" %d",p[i]);
49 }
50 }
分析:
1、定义全局数组,Pre是对应先序遍历的输入,In是对应中序遍历的出栈,Post是对应后序遍历的输出
2、处理输入的过程用了栈的思路
3、solve()用了分治
[刷题] PTA 03-树3 Tree Traversals Again的更多相关文章
- [刷题] PTA 03-树1 树的同构
程序: 1 #include <stdio.h> 2 #define MaxTree 10 3 #define ElementType char 4 #define Tree int 5 ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- LeetCode 刷题笔记 (树)
1. minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...
- 【leetcode刷题笔记】Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [刷题] PTA 04-树4 是否同一棵二叉搜索树
程序: 1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct TreeNode *Tree; 4 struct ...
- 【刷题-PAT】A1119 Pre- and Post-order Traversals (30 分)
1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...
- 【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [刷题] PTA 查验身份证
题目: 7-63 查验身份证 (15 分) 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5, ...
- 【刷题-LeetCode】199 Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
随机推荐
- 彻底搞清Flink中的Window
窗口 在流处理应用中,数据是连续不断的,因此我们不可能等到所有数据都到了才开始处理.当然我们可以每来一个消息就处理一次,但是有时我们需要做一些聚合类的处理,例如:在过去的1分钟内有多少用户点击了我们的 ...
- 【RocketMQ源码分析】深入消息存储(2)
前文回顾 CommitLog篇 --[RocketMQ源码分析]深入消息存储(1) MappedFile篇 --[RocketMQ源码分析]深入消息存储(3) 前文说完了一条消息如何被持久化到本地磁盘 ...
- 使用Portainer部署Docker容器实践
一.背景 最近在使用rancher2.5.5部署Redis主从复制的时候,发现rancher会产生很多iptables的规则,这些规则导致我们在部署了rancher的机器上无法使用Redis的主从复制 ...
- 结对作业-stage_2
见队友博客:结对编程-stage_2
- Semaphore自白:限流器用我就对了!
大家好,我是 Semaphore,我的中文名字叫"信号量",我来自 JUC(java.util.concurrent)家族. 我们家族有很多优秀的成员,比如:CountDownLa ...
- JAVAEE_01_什么是javaEE
javaEE Java平台包含三个版本: - JavaME :适用于小型设备和智能卡的JavaME (Java Platform Micro Edition,Java微型版) - JavaSE : 适 ...
- Day01_12_Java的类型转换
Java类型转换 实例 public class 类型转换 { public static void main(String[] args) { int i =128; byte b= (byte) ...
- 计算eks node 中pod数量
计算eks node 中pod数量 计算公式:((IP数I - 1) * ENI数) + 2 实例规格等ENI数和IP的对应关系,请参考 https://docs.aws.amazon.com/zh_ ...
- 软件篇-02-基于ZED 2和ORB_SLAM2的SLAM实践
时隔两周,我又回来了. 本期内容如题,ZED 2的SDK功能还是挺多的,包括轨迹跟踪,实时建图等等.虽然由于是商业产品,我看不到他们的源代码,但是根据使用情况来看,ZED 2内部是采用了IMU和光 ...
- Blade模板引擎教程-常用语法格式
打印变量或者默认值,这个语法会自动转义变量内容中的html标记,使得html标签原样输出Welcome, {{ $name or 'California' }} 打印变量原始内容,不进行转义的用法{! ...