[刷题] 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, ...
随机推荐
- java例题_02 101~200以内的素数
1 /*2 [程序 2 输出素数] 2 题目:判断 101-200 之间有多少个素数,并输出所有素数. 3 程序分析:判断素数的方法:用一个数分别去除 2 到 sqrt(这个数),如果能被整除,则表明 ...
- 【分布式】SpringCloud(3)--Eureka服务注册与发现
1.Eureka概述 1.1.什么是Eureka Eureka是Netflix的一个子模块.基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. 只需要使用服务的标识符,就可以访问到 ...
- 关于生产环境改用G1垃圾收集器的思考
背景 由于我们的业务量非常大,响应延迟要求高.目前沿用的老的ParNew+CMS已经不能支撑业务的需求.平均一台机器在1个月内有1次秒级别的stop the world.对系统来说是个巨大的隐患.所以 ...
- 认识Python解释器和PyCharm编辑器
(1)安装Python解释器 Python官网:https://www.python.org/ 下载对应机器(Windows/Mac)的安装包: 百度网盘地址: 链接:https://pan.baid ...
- 运维趋势2019年总结,运维就是要做到"技多不压身"
2019年 在互联网这个行业中对运维的要求越来越来越高,比如2015.16年的时候,运维只是做一些人力投入的事情,比如重启个服务,搭建一个nginx,mysql主从服务,简单的优化一下,就可以上线了, ...
- Kotlin编写Processing程序(使用函数式编程思维和面向接口方式)
写一例Kotlin编写的Processing程序,充分调用函数式编程思维和面向接口的编程思维,供自己和读者参考学习. 初衷 想要实现一行行的文字排版功能,每一行作为一个单位,可制定显示的位置.大小.文 ...
- linux 更新python3.8
1 下载源码 地址 选版本下载即可,目前最新为3.8.2版本. 2 解压 tar -zxvf Python-3.8.2.tgz cd Python-3.8.2 3 新建安装目录 安装目录在/usr/l ...
- 观世音甘泉活树的故事竟然是Java设计模式:备忘录模式
目录 定义 意图 主要解决问题 何时使用 优缺点 结构 白箱实现 黑箱实现 多重检查点 观世音甘泉活树的故事 定义 备忘录模式是对象的行为型模式,备忘录对象是一个用来存储另外一个对象内部状态的快照的对 ...
- 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...
- 【并发编程】Java中的锁有哪些?
0.死锁 两个或者两个以上的线程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞现象,若无外力作用,他们都将无法让程序进行下去: 死锁条件: 不可剥夺条件: T1持有的资源无法被T2剥夺 请 ...