[刷题] 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, ...
随机推荐
- Android Studio 如何运行单个activity
•写在前面 调试界面运行单个 Activity 可节省编译整个项目的时间提高效率: 本着提高效率的角度,特地上网百度相关知识: •解决方法 首先,在 AndroidManifest.xml 文件中,找 ...
- Jmeter(四十一) - 从入门到精通进阶篇 - Jmeter配置文件的刨根问底 - 下篇(详解教程)
1.简介 为什么宏哥要对Jmeter的配置文件进行一下讲解了,因为有的童鞋或者小伙伴在测试中遇到一些需要修改配置文件的问题不是很清楚也不是很懂,就算修改了也是模模糊糊的.更有甚者觉得那是禁地神圣不可轻 ...
- [SIGIR2020] Sequential Recommendation with Self-Attentive Multi-Adversarial Network
这篇论文主要提出了一个网络,成为Multi-Factor Generative Adversarial Network,直接翻译过来的话就是多因子生成对抗网络.主要是期望能够探究影响推荐的其他因子(因 ...
- 定制开发——GitHub 热点速览 v.21.15
作者:HelloGitHub-小鱼干 自定义 或者说 定制 是本周 GitHub 热点的最佳写照.比如,lipgloss 这个项目,可以让你自己定义终端样式,五彩斑斓的黑终端来一个.接着,是 Appl ...
- Java实现十个经典排序算法(带动态效果图)
前言 排序算法是老生常谈的了,但是在面试中也有会被问到,例如有时候,在考察算法能力的时候,不让你写算法,就让你描述一下,某个排序算法的思想以及时间复杂度或空间复杂度.我就遇到过,直接问快排的,所以这次 ...
- Tomcat9配置HTTP/2
1 概述 Tomcat从Tomcat8的一些较新版本就支持HTTP/2了,Tomcat9直接支持,本文首先讲述了相关HTTP/2的特性,接着利用一个简单的开源工具mkcert生成证书并利用该证书配置H ...
- JDBC_02_JDBC连接数据库 (INSERT INTO)
JDBC连接数据库 (INSERT INTO) String url="jdbc:mysql://127.0.0.1:3306/employ?useUnicode=true&char ...
- Vue.js入门及其常用指令
一.Vue框架 https://cn.vuejs.org/ 官网 前端领域有三大框架 Angular诞生于2009年,是由谷歌公司创建出来的框架: React诞生于2013年,是由facebook公司 ...
- 记某次sql注入绕过ids
昨天测试sql注入,发现个站,存在ids,一个单引号直接拦截,无论我怎么编码都不行,怕不是废了.. 灵机一动 基础探测 /*'*/ 报错 /*''*/ 返回正常 是字符串类型. 先本地测试 返回所有 ...
- hdu1839 二分最短路
题意: 给你n个城市,m条双向边,每条边有自己的长度和最大运输量,让你找到一条时间小于等于T的运输能力最大的那条路... 思路: 刚开始以为是费用流呢,后来发现根本不是,因为根 ...