PAT 甲级 1086 Tree Traversals Again (25 分)

题目描述

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.
在这里插入图片描述

输入

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.

输出

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.

思路

此题关键是从堆栈操作中找到前序和中序遍历,每一个push为前序,pop为中序列,之后就可以建树了

代码

#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
using namespace std;
typedef struct node {
    
    
	int key;
	struct node* left;
	struct node* right;
}Tree;
int ans[100];
int in[100];
int pre [100];
stack<int> st;
int N;
vector<int> aa;
/*Tree* Create_tree(int inS, int preS, int length)
{
	if (length <= 0)
	{
		return NULL;
	}
	int rootindex = inS;
	int ll = 0;
	for (; rootindex < inS+length; rootindex++)
	{
		if (pre[preS] == in[rootindex])
		{
			break;
		}
		ll++;
	}
	Tree* node = (Tree *)malloc(sizeof(Tree));
	node->key = pre[preS];
	node->left = Create_tree(inS, preS + 1, ll);
	node->right = Create_tree(inS+ll + 1, preS+ll + 1, length - ll-1);
	return node;
}*/
Tree* create(int preL, int preR, int inL, int inR) {
    
    
	if (preL > preR) return NULL;
	Tree* root = (Tree *)malloc(sizeof(Tree));
	root->key = pre[preL];
	int k;//中序中根结点位置 
	for (k = inL; k <= inR; k++) {
    
    
		if (in[k] == pre[preL]) break;
	}
	int lnum = k - inL;//左子树长度
	root->left = create(preL + 1, preL + lnum, inL, k - 1);
	root->right = create(preL + lnum + 1, preR, k + 1, inR);
	return root;
}

void dfs(Tree* head)
{
    
    
	if (head == NULL)
	{
    
    
		return;
	}
	dfs(head->left);
	dfs(head->right);
	aa.push_back(head->key);
}
int main()
{
    
    
	
	/*cin >> N;
	for (int i = 0; i < 2 * N; i++)
	{
		string s1;
		int k;
		cin >> s1;
		if (s1 == "Push")
		{
			cin >> k;
			st.push(k);
		}
		else
		{
			in.push_back(st.top());
			st.pop();
		}
	}
	int in2[100];
	for (int i = 0; i < N; i++)
	{
		pre[i] = in[i];
		in2[i] = in[i];
	}
	sort(pre, pre + N);*/
	string op;
	cin >> N;
	int T = 2 * N;
	int inI = 0;
	int preI = 0;
	while (T--) {
    
    
		cin >> op;
		if (op == "Push") {
    
    
			int x;
			cin >> x;
			st.push(x);
            pre[preI++] = x;
		}
		else {
    
    
			in[inI++] = st.top();
			st.pop();
		}
	}
	Tree *head=create(0, N-1,0, N-1);
	dfs(head);
	printf("%d", aa[0]);
	for (int i = 1; i < N; i++)
	{
    
    
		printf(" %d", aa[i]);
	}
	printf("\n");
}

猜你喜欢

转载自blog.csdn.net/qq_45478482/article/details/120100260
今日推荐