03-树2 Tree Traversals Again

这题是第二次做了,两次都不是独立完成,不过我发现我第一次参考的程序,也是参考老师(陈越)的范例做出来的。我对老师给的做了小幅修改,因为我不想有全局变量的的存在,所以我多传了三个参数进去。正序遍历每次都是从1到N吗?看题目我认为应该是,结果我错了,我是对比正确的程序一点点修改才发现的,不容易啊。下面是题目及程序

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4
 5 typedef struct
 6 {
 7     int * a;
 8     int top;
 9 }SeqStack;
10
11 void push(SeqStack * pS, int X);
12 int pop(SeqStack * pS);
13 void solve(int * pre, int * in, int * post, int preL, int inL, int postL, int n);
14
15 int main()
16 {
17 //    freopen("in.txt", "r", stdin); // for test
18     int i, N;
19     scanf("%d", &N);
20
21     SeqStack S;
22     S.a = (int *)malloc(N * sizeof(int));
23     S.top = -1;
24     int pre[N], in[N], post[N];
25
26     char chars[5];
27     char * str = chars;
28     int X, pre_index, in_index;
29     pre_index = in_index = 0;
30     for(i = 0; i < 2 * N; i++)
31     {
32         scanf("%s", str);
33         if(strcmp(str, "Push") == 0)
34         {
35             scanf("%d", &X);
36             pre[pre_index++] = X;
37             push(&S, X);
38         }
39         else
40             in[in_index++] = pop(&S);
41     }
42
43     solve(pre, in, post, 0, 0, 0, N);
44     for(i = 0; i < N; i++)
45     {
46         printf("%d", post[i]);
47         if(i < N - 1)
48             printf(" ");
49         else
50             printf("\n");
51     }
52 //    fclose(stdin); // for test
53     return 0;
54 }
55
56 void push(SeqStack * pS, int X)
57 {
58     pS->a[++(pS->top)] = X;
59 }
60
61 int pop(SeqStack * pS)
62 {
63     return pS->a[pS->top--];
64 }
65
66 void solve(int * pre, int * in, int * post, int preL, int inL, int postL, int n)
67 {
68     int i, root, L, R;
69
70     if(n == 0)
71         return;
72     if(n == 1)
73     {
74         post[postL] = pre[preL];
75         return;
76     }
77     root = pre[preL];
78     post[postL + n - 1] = root;
79     for(i = 0; i < n; i++)
80         if(in[inL + i] == root)
81             break;
82     L = i;
83     R = n - L - 1;
84     solve(pre, in, post, preL + 1, inL, postL, L);
85     solve(pre, in, post, preL + L + 1, inL + L + 1, postL + L, R);
86 }

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
时间: 2024-10-03 08:25:20

03-树2 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