用栈实现树遍历

 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. C++并发与多线程学习笔记--unique_lock详解

    unique_lock 取代lock_quard unique_lock 的第二个参数 std::adopt_lock std::try_to_lock std::defer_lock unique_ ...

  2. [SIGIR2020] Sequential Recommendation with Self-Attentive Multi-Adversarial Network

    这篇论文主要提出了一个网络,成为Multi-Factor Generative Adversarial Network,直接翻译过来的话就是多因子生成对抗网络.主要是期望能够探究影响推荐的其他因子(因 ...

  3. 基于scrapy框架的爬虫基本步骤

    本文以爬取网站 代码的边城 为例 1.安装scrapy框架 详细教程可以查看本站文章 点击跳转 2.新建scrapy项目 生成一个爬虫文件.在指定的目录打开cmd.exe文件,输入代码 scrapy ...

  4. 【Python学习笔记】-虚拟环境virtualenv

    在开发python应用程序的时候,系统安装的python3只有一个版本:3.4.所有的第三方的包都回被pip安装到python3的site-packages目录下. 如果我们要要同时开发多个应用程序, ...

  5. Python简单实现杨辉三角

    n=input("请输入要打印的行数")n=int(n)for x in range(0,n+1): p=1 print(''.rjust(n-x),end="" ...

  6. 零基础学Java,PayPal技术专家手把手带你入门

    在最权威的 TIOBE 编程语言排名榜单上,Java 常年稳居第一,可以说是世界上应用最为广泛的一门语言. 同时,在微服务.云计算.大数据.Android App 开发等领域,Java 也是当之无愧的 ...

  7. hbuilderX打包苹果证书的申请方法

    现在uniapp越来越火,hbuilderX和apicloud这些工具使用html+js语言就可以开发强大的app,大大降低了app开发的技术门槛. hbuilderX或apicloud在打包ios应 ...

  8. ret2dl32

    ret2dl32 首先检查一下保护: IDA分析一下 程序很简单就是,往bss段上的buf读入0x400个数据,然后拷贝到栈上.read_got还被置为0,这一看就是要逼着你使用ret2dlresol ...

  9. 「HTML+CSS」--自定义加载动画【017】

    前言 Hello!小伙伴! 首先非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 哈哈 自我介绍一下 昵称:海轰 标签:程序猿一只|C++选手|学生 简介:因C语言结识编程,随后转入计算机 ...

  10. 6. VUE 指令-概述

    指令 (Directives) 是带有 v- 前缀的特殊特性.指令特性的值预期是单个 JavaScript 表达式 (v-for是例外情况,稍后我们再讨论).指令的职责是,当表达式的值改变时,将其产生 ...