PAT (Advanced Level) Practice 1086 Tree Traversals Again (25 分) 凌宸1642

本文介绍如何使用非递归方法通过栈操作实现中序遍历,然后根据序列重建二叉树并求得其后序遍历。通过给定的示例输入和输出,展示了如何将先序和中序遍历应用于树的重构,适用于解决PAT高级别题目1086题目的解法。
摘要由CSDN通过智能技术生成

PAT (Advanced Level) Practice 1086 Tree Traversals Again (25 分) 凌宸1642

题目描述:

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.

译:可以使用堆栈以非递归方式实现中序二叉树遍历。 例如,假设遍历一棵6节点的二叉树(键从1到6),栈操作为:push(1); push(2);push(3); pop() ; pop() ; push(4);pop() ; pop() ; push(5);push(6);pop() ; pop() ;。 然后可以从这个操作序列生成一个唯一的二叉树(如图 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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行包含一个正整数 N(≤30),它是树中节点的总数(因此节点编号为 1 到 N)。 然后是 2N 行,每行描述一个堆栈操作,格式为:“Push X”,其中 X 是被压入堆栈的节点的索引; 或 “Pop” 表示从堆栈中弹出一个节点。


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

The Idea:

通过push X 的先后顺序,我们可以很自然的想到push书序就是先序遍历得到的序列,然后pop出栈的顺序就是中序遍历得到序列,有先序遍历和中序遍历,很自然的想到利用先序遍历和中序遍历重建二叉树,然后对重建的二叉树再进行后序遍历即可。

The Codes:

#include<bits/stdc++.h>
using namespace std ;
struct node{
	int val ;
	node *l , *r ;
};
string s , s1 ;
node* root ;
int n , cnt = 0 , cnt2 = 0 , top = -1 , cnt3 = 0 ;
int pre[33] , in[33] , post[33];
node* create(int preL , int preR , int inL , int inR){
	if(preL > preR) return NULL;
	node* root = new node ;
	root->val = pre[preL] ;
	int k ; 
	for(k = inL ; k <= inR ; k ++){
		if(pre[preL] == in[k]) break ;
	}
	int numLeft = k - inL ;
	root -> l = create(preL + 1 , preL + numLeft , inL , k - 1) ;
	root -> r = create(preL + numLeft + 1 , preR , k + 1 , inR) ;
	return root ;
}
void postOrder(node *root){
	if(!root) return ;
	postOrder(root->l) ;
	postOrder(root->r) ;
	post[cnt3++] = root->val ;
}
int main(){
	cin >> n ;
	getline(cin , s) ; // 	吸收回车 
	stack<int> st ;
	for(int i = 0 ; i < 2 * n ; i ++){
		getline(cin , s) ;
		if(s.size() > 3){
			sscanf(s.substr(5 , s.size() - 5).c_str() , "%d" , &pre[cnt]) ; // 将字符串转为数字
			st.push(pre[cnt ++]) ;
		}else{
			in[cnt2 ++] = st.top() ;
			st.pop() ;
		}
	}
	root = create( 0 , n - 1 , 0 , n - 1) ;
	postOrder(root) ;
	for(int i = 0 ; i < n ; i ++)
		printf("%d%c" , post[i] , ((i == n -1)?'\n':' ')) ;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值