用栈实现树遍历

 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的更多相关文章

  1. [刷题] PTA 03-树1 树的同构

    程序: 1 #include <stdio.h> 2 #define MaxTree 10 3 #define ElementType char 4 #define Tree int 5 ...

  2. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  3. LeetCode 刷题笔记 (树)

    1.  minimum-depth-of-binary-tree 题目描述 Given a binary tree, find its minimum depth.The minimum depth ...

  4. 【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 ...

  5. [刷题] PTA 04-树4 是否同一棵二叉搜索树

    程序: 1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct TreeNode *Tree; 4 struct ...

  6. 【刷题-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 ...

  7. 【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 ...

  8. [刷题] PTA 查验身份证

    题目: 7-63 查验身份证 (15 分)  一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5, ...

  9. 【刷题-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, ...

随机推荐

  1. [SpringCloud教程]3. Eureka服务注册中心集成

    新微服务项目多半采用Nacos作为服务注册与发现中心,但是旧项目可能使用Eureka.zookeeper.Consul.Nacos作为服务注册中心. 新项目建议使用Nacos作为服务注册中心 Spri ...

  2. java面试-什么是GC root

    一.什么是垃圾 内存中已经不再被使用到的空间就是垃圾 二.要进行垃圾回收,如何判断一个对象是否可以被回收? 引用计数法 很难解决对象之间的循环引用问题 枚举根节点做可达性分析 通过一系列名为" ...

  3. 前端框架之争丨除了Vue、Angular和React还有谁与之争锋

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 原文参考:https://www.sitepoint.com/most-popular-frontend-f ...

  4. 一次错误使用 go-cache 导致出现的线上问题

    话说一个美滋滋的上午, 突然就出现大量报警, 接口大量请求都响应超时了. 排查过程 查看服务器的监控系统, CPU, 内存, 负载等指标正常 排查日志, 日志能够响应的结果也正常. request.l ...

  5. sklearn.metrics【指标】

    [分类指标] 1.accuracy_score(y_true,y_pre) : 精度 2.auc(x, y, reorder=False) : ROC曲线下的面积;较大的AUC代表了较好的perfor ...

  6. UT之最后一测

    经过前面几次文章的分享的UT的相关知识,今天接着分享UT相关最后一测文章,希望对大家在UT的学习中有一点点的帮助. Spring集成测试 有时候我们需要在跑起来的Spring环境中验证,Spring ...

  7. 深入探索Android热修复技术原理读书笔记 —— 热修复技术介绍

    1.1 什么是热修复 对于广大的移动开发者而言,发版更新是最为寻常不过的事了.然而,如果你 发现刚发出去的包有紧急的BUG需要修复,那你就必须需要经过下面这样的流程: 这就是传统的更新流程,步骤十分繁 ...

  8. overflow和absolute之间的问题,transfrom可以解决

    CSS代码: .overflow { width: 191px; height: 191px; border: 2px solid #beceeb; overflow: hidden; } .over ...

  9. 让访问pc端的官网直接跳转到移动端的网站代码

    <SCRIPT LANGUAGE="JavaScript"> function mobile_device_detect(url) { var thisOS=navig ...

  10. dalvik浅析三:类加载

    android的安装包是个apk文件,其中包含dex.资源及签名文件.其中dex是包含程序运行的类代码,而android是运行在dalvik(5.0之前)上的.本篇我们就来看下dalvik是如何把de ...