03-树3 Tree Traversals Again

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack>
 4 #include <string>
 5 using namespace std;
 6
 7 #define MaxSize 30
 8
 9 #define OK 1
10 #define ERROR 0
11
12 int preOrder[MaxSize];
13 int inOrder[MaxSize];
14 int postOrder[MaxSize];
15
16 void postorderTraversal(int preNo, int inNo, int postNo, int N);
17
18 int main()
19 {
20     stack<int> stack;
21     int N;        //树的结点数
22     cin >> N;
23     string str;
24     int data;
25     int preNo = 0, inNo = 0, postNo = 0;
26     for(int i = 0; i < N * 2; i++) {        //push + pop = N*2
27         cin >> str;
28         if(str == "Push") {            //push为前序序列
29             cin >> data;
30             preOrder[preNo++] = data;
31             stack.push(data);
32         }else{                        //pop出的是中序序列
33             inOrder[inNo++] = stack.top();
34             stack.pop();            //pop() 移除栈顶元素(不会返回栈顶元素的值)
35         }
36     }
37     postorderTraversal(0, 0, 0, N);
38     for(int i = 0; i < N; i++) {        //输出后序遍历序列
39         if(i == 0)                        //控制输出格式
40             printf("%d",postOrder[i]);
41         else
42             printf(" %d",postOrder[i]);
43     }
44     printf("\n");
45     return 0;
46 }
47
48 void postorderTraversal(int preNo, int inNo, int postNo, int N)
49 {
50     if(N == 0)
51         return;
52     if(N == 1) {
53         postOrder[postNo] = preOrder[preNo];
54          return;
55     }
56     int L, R;
57     int root = preOrder[preNo];            //先序遍历GLR第一个为根
58     postOrder[postNo + N -1] = root;     //后序遍历LRG最后一个为根
59     for(int i = 0; i < N; i++) {
60         if(inOrder[inNo + i] == root) {    //找到中序的根 左边为左子树 右边为右子树
61             L = i;                        //左子树的结点数
62             break;
63         }
64     }
65     R = N - L - 1;                        //右子树的结点数
66     postorderTraversal(preNo + 1, inNo, postNo, L);    //同理,将左子树看成新的树
67     postorderTraversal(preNo + L + 1, inNo + L + 1, postNo + L, R);//同理,右子树
68 } 
 
时间: 2024-10-19 06:42:21

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

Tree Traversals

Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序遍历的特点递归求解,详细内容见代码 #include <iostream> #include <queue> using namespace std; struct TreeNode{ int val; TreeNode* left; TreeNode* right; }; int p

树-伸展树(Splay Tree)

伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二叉查找树,即它具有和二叉查找树一样的性质:假设x为树中的任意一个结点,x节点包含关键字key,节点x的key值记为key[x].如果y是x的左子树中的一个结点,则key[y] <= key[x]:如果y是x的右子树的一个结点,则key[y] >= key[x]. (02) 除了拥有二叉查找树的性质

PAT Tree Traversals Again

Tree Traversals Again 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); p

hdu 1710 Binary Tree Traversals 前序遍历和中序推后序

题链;http://acm.hdu.edu.cn/showproblem.php?pid=1710 Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4205    Accepted Submission(s): 1904 Problem Description A binary tree i

笛卡尔树cartesian tree

笛卡尔树cartesian tree 笛卡尔树是一种特定的二叉树数据结构,可由数列构造,在范围最值查询.范围top k查询(range top k queries)等问题上有广泛应用.它具有堆的有序性,中序遍历可以输出原数列.笛卡尔树结构由Vuillmin(1980)[1]在解决范围搜索的几何数据结构问题时提出.从数列中构造一棵笛卡尔树可以线性时间完成,需要采用基于栈的算法来找到在该数列中的所有最近小数. 定义 无相同元素的数列构造出的笛卡尔树具有下列性质: 结点一一对应于数列元素.即数列中的每

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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

使用行为树(Behavior Tree)实现游戏AI

原地址:http://blog.csdn.net/akara/article/details/6084786 [原创]使用行为树(Behavior Tree)实现游戏AIby AKara 2010-12-09 @ http://blog.csdn.net/akara @ akarachen(at)gmail.com @weibo.com/akaras 谈到游戏AI,很明显智能体拥有的知识条目越多,便显得更智能,但维护庞大数量的知识条目是个噩梦:使用有限状态机(FSM),分层有限状态机(HFSM)

使用行为树(Behavior Tree)实现网游奖励掉落系统

原地址:http://blog.csdn.net/akara/article/details/6165421 [原创]使用行为树(Behavior Tree)实现网游奖励掉落系统by AKara 2011-01-24 @ http://blog.csdn.net/akara @ akarachen(at)gmail.com @weibo.com/akaras 奖励/掉落系统,涵盖物品,经验,金钱等网游中可直接给予玩家的元素.一个简单,直观,可扩展的掉落系统对网游中的产出控制起非常重要的作用. 奖

线段树(segment tree)

1.概述 线段树,也叫区间树,是一个完全二叉树,它在各个节点保存一条线段(即"子数组"),因而常用于解决数列维护问题,基本能保证每个操作的复杂度为O(lgN). 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a,(a+b)/2],右儿子表示的区间为[(a+b)/2+1,b].因此线段树是平衡二叉树,最后的子节点数目为N,即整个线段区间的长度. 使用线段树可以

Leetcode 树 Symmetric Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Symmetric Tree Total Accepted: 13991 Total Submissions: 44240 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmet