03-树3 Tree Traversals Again (25 分) (根据树的先序序列和中序序列求后序序列)递归调用

该题目要求根据给定的二叉树先序和中序遍历序列,生成后序遍历序列。通过栈操作模拟二叉树的中序遍历,然后使用递归函数解决此问题。输入包含节点数量和一系列栈操作,输出是后序遍历序列。需要注意递归函数的实现和处理含有空格的字符串输入。
摘要由CSDN通过智能技术生成

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

这题其实不难理解,Push的为二叉树的先序遍历序列,Pop的为二叉树的中序遍历序列,要根据先序序列和中序序列求后序序列,愚蠢的我一时没有发现,于是用了错误的方法:
这个算法明显有很大问题,只通过了一个测试样例,稍加验证就会发现很多情况是无法正确输出的。而且输入带有空格的字符串,用cin.getline仿佛还出了问题,必须2N+1的循环才能输出2N项。

#include<iostream>
#include<string>
const int MaxSize = 50;
using namespace std;
typedef int ElemType;
typedef struct {
	ElemType data[MaxSize];
	int top;
}SqStack;
void InitStack(SqStack*& s)
{
	s = new SqStack;
	s->top = -1;
}
void DestroyStack(SqStack*& s)
{
	delete s;
}
bool stackEmpty(SqStack* s)
{
	return(s->top == -1);
}
void Push(SqStack*& s, ElemType e)
{
	if (s->top == MaxSize - 1)
		return;
	s->top++;
	s->data[s->top] = e;
}
void Pop(SqStack* s, ElemType& e)
{
	if (s->top == -1)
		return ;
	e = s->data[s->top];
	s->top--;
}
int main()
{
	int N;
	cin >> N;
	int x;
	SqStack *q;
	InitStack(q);
	int a[50] = { 0 };
	int flag = 0;
	char s[10];
	int ans = -1;
	for (int i = 0;i <= 2 * N;i++)
	{
		cin.getline(s,10);
		if (s[1] == 'u')
		{
			x = s[5] - '0';
			Push(q, x);
		}
		if (s[1] == 'o')
		{
			ans++;
		}
		while (ans > 0)
		{
			Pop(q, a[flag++]);
			ans--;
		}
	}
	Pop(q, a[flag++]);
	for (int i = 0;i < flag-1;i++)
		cout << a[i] << " ";
	cout << a[flag - 1];
}

后来观摩了姥姥的做法,豁然开朗
这里使用了stack头文件简化运算,然后输入也分两步进行,先输入一个string类,判断是不是Push,然后再输入数字。巧妙的化解了cin只识别到空格就结束的一个弊端。而且没有出现cin.getline出现的问题。

#include<iostream>
#include<stack>
#include<string>
using namespace std;
const int MaxSize = 100;
int pre[MaxSize], in[MaxSize], post[MaxSize];
void solve(int PreL, int inL, int postL, int n) {
	if (n == 0)return;
	if (n == 1) {
		post[postL] = pre[PreL];
		return;
	}
	int i;
	int root = pre[PreL];
	post[postL + n - 1] = root;
	for (i = 0; i < n; i++) {
		if (in[inL + i] == root)break;
	}
	int L = i, R = n - i - 1;
	solve(PreL + 1, inL, postL, L);
	solve(PreL + 1 + L, inL + L + 1, postL + L, R);
}
int main()
{
	int N, num, i = 0, j = 0;
	cin >> N;
	string s;//这里不要轻易使用cin.getline函数,会出问题
	stack<int> st;
	for (int z = 0; z < 2 * N; z++) {
		cin >> s;
		if (s[1] =='u') {
			cin >> num;
			st.push(num);
			pre[i++] = num;
		}
		else {
			num = st.top();
			st.pop();
			in[j++] = num;
		}
	}
	solve(0, 0, 0, N);
	for (int i = 0;i < N - 1;i++)
		cout << post[i] << " ";
	cout << post[N - 1];
	return 0;
}

要特别注意姥姥这里的递归函数的使用以及含有空格字符串的输入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值