二叉树及其遍历

push为前序遍历序列,pop为中序遍历序列。将题目转化为已知前序、中序,求后序。

前序GLR 中序LGR

前序第一个为G,在中序中找到G,左边为左子树L,右边为右子树R。

将左右子树看成新的树,同理。

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N(≤30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

Sample Output:

3 4 2 6 5 1

 #include <iostream>
#include <cstdio>
#include <stack>
#include <string>
using namespace std; #define MaxSize 30 #define OK 1
#define ERROR 0 int preOrder[MaxSize];
int inOrder[MaxSize];
int postOrder[MaxSize]; void postorderTraversal(int preNo, int inNo, int postNo, int N); int main()
{
stack<int> stack;
int N; //树的结点数
cin >> N;
string str;
int data;
int preNo = , inNo = , postNo = ;
for(int i = ; i < N * ; i++) { //push + pop = N*2
cin >> str;
if(str == "Push") { //push为前序序列
cin >> data;
preOrder[preNo++] = data;
stack.push(data);
}else{ //pop出的是中序序列
inOrder[inNo++] = stack.top();
stack.pop(); //pop() 移除栈顶元素(不会返回栈顶元素的值)
}
}
postorderTraversal(, , , N);
for(int i = ; i < N; i++) { //输出后序遍历序列
if(i == ) //控制输出格式
printf("%d",postOrder[i]);
else
printf(" %d",postOrder[i]);
}
printf("\n");
return ;
} void postorderTraversal(int preNo, int inNo, int postNo, int N)
{
if(N == )
return;
if(N == ) {
postOrder[postNo] = preOrder[preNo];
return;
}
int L, R;
int root = preOrder[preNo]; //先序遍历GLR第一个为根
postOrder[postNo + N -] = root; //后序遍历LRG最后一个为根
for(int i = ; i < N; i++) {
if(inOrder[inNo + i] == root) { //找到中序的根 左边为左子树 右边为右子树
L = i; //左子树的结点数
break;
}
}
R = N - L - ; //右子树的结点数
postorderTraversal(preNo + , inNo, postNo, L); //同理,将左子树看成新的树
postorderTraversal(preNo + L + , inNo + L + , postNo + L, R);//同理,右子树
}
 

03-树3 Tree Traversals Again的更多相关文章

  1. PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列

    Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...

  2. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  4. 树-伸展树(Splay Tree)

    伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...

  5. HDU1710Binary Tree Traversals

    HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...

  6. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. PAT1086:Tree Traversals Again

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  10. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. 30. Distinct Subsequences

    Distinct Subsequences OJ: https://oj.leetcode.com/problems/distinct-subsequences/ Given a string S a ...

  2. 《Code Complete》ch.7 高质量的子程序

    WHAT? 子程序(routines)是为实现一个特定目的而编写的可被调用的方法或过程.在C++中是函数(function),在Java中是方法(method),在VB中是函数过程(function ...

  3. hibernate常见错误

    1.Hibernate: Could not synchronize database state with session 1.主键不是自动生成的,然后自己没手动设置.  2.插入的实体字段跟数据库 ...

  4. 请添加 MIME 映射

    HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 以管理员运行命令:C:\Wind ...

  5. org.apache.ibatis.reflection.ReflectionException

    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Reflecti ...

  6. BZOJ3613 南园满地堆轻絮-二分法

    http://www.lydsy.com/JudgeOnline/problem.php?id=3613 //话说BZOJ终于修好了... Description 小 Z 是 ZRP(Zombies' ...

  7. XML Namespace 命名空间

    根据 Namespaces in XML W3C 推荐标准的定义,XML 命名空间 是由国际化资源标识符 (IRI) 标识的 XML 元素和属性集合:该集合通常称作 XML“词汇”. 定义 XML 命 ...

  8. Java基础——数据类型之间的转换

    Java数据类型分为三大类,即布尔型.字符型和数值型.其中数值型又分为整型和浮点型.Java的基本数据类型(8种)为布尔型boolean(1字节):字符型char(2字节):整型byte(1字节).s ...

  9. asp.net(c#)修改时的赋值操作

    赋值操作方法(将信息显示至文本框中): public void Show_Infobase(int _peoid) { DataSet ds = new DataSet(); ds = platbll ...

  10. jqmobi 的一些設置

    jqmobi version=2.1; 不是 version =3.0: 好吧,我用了jqmobi 差不多半年了,我竟然連 官方的文檔都沒有看完,怪不得我走了多少的彎路.....哎!!!! 1.隱藏 ...