1086 Tree Traversals Again (25)(25 point(s))---MOOC浙大数据结构

1086 Tree Traversals Again (25)(25 point(s))

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.

用堆栈实现乱序二叉树的遍历

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

尽量今天做出来

结果7.11才做出来,比较笨的方法,建立起来树,然后遍历

#include <iostream>
#include <string>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

struct TreeNode{
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x):val(x),left(NULL),right(NULL){}
};

TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
	if(preorder.size()==0)//是否为空?
		return NULL;      
	TreeNode* tree=new TreeNode(preorder[0]);//new申请根节点

	vector<int>::iterator it;//找到根节点的位置
	it=find(inorder.begin(),inorder.end(),preorder[0]);
	int position=0;
	if(it!=inorder.end())
		position=distance(inorder.begin(),it);

	if(position==1)//左序列
		tree->left=new TreeNode(inorder[0]);
	else{
		vector<int> left_pre(preorder.begin()+1,preorder.begin()+position+1);  
		vector<int> left_ino(inorder.begin(),inorder.begin()+position);
		tree->left=buildTree(left_pre,left_ino);//递归
	}

	if(preorder.size()-position==2)//右序列
		tree->right=new TreeNode(inorder[inorder.size()-1]);
	else{
		vector<int> right_pre(preorder.begin()+position+1,preorder.end());
		vector<int> right_ino(inorder.begin()+position+1,inorder.end());    
		tree->right=buildTree(right_pre,right_ino);//递归
	}
	return tree;
}
void PostOrderTraversal(TreeNode * tree)
{	
	static bool flag = true;
	if(tree!=NULL)
	{
		PostOrderTraversal(tree->left);
		PostOrderTraversal(tree->right);
		if (flag)
		{
			cout << (*tree).val;
			flag = false;
		}
		else
			cout << " "<< (*tree).val;
	}
}

int main()
{
	stack<int> sta;
	vector<int> vec_preorder;
	vector<int> vec_inorder;
	int n;
	cin>>n;
	int a=2*n;
	while (a--)
	{
		string s1;
		cin>>s1;
		if(strcmp(s1.c_str(),"Push")==0)
		{
			int tmp;
			cin>>tmp;
			sta.push(tmp);
			vec_preorder.push_back(tmp);
		}else
		{
			vec_inorder.push_back(sta.top());
			sta.pop();
		}
	}
	TreeNode *Root;
	Root=buildTree(vec_preorder,vec_inorder);
	PostOrderTraversal(Root);
	return 0;
}



这个是看了陈老老的视频看着写出来的,这种比较看重思路,需要把数据拿到头文件去写:


#include <iostream>
#include <string.h>
#include <string>
#include <stack>
using namespace std;

int prearr[100]={0};
int inoarr[100]={0};
int posarr[100]={0};
void solve(int preL,int inL,int postL,int n)
{
	int i;
	if(n==0)
		return ;
	if(n==1)
	{
		posarr[postL]=prearr[preL];
		return;
	}
	int root=prearr[preL];
	posarr[postL+n-1]=root;
	for(i=0;i<n;i++) {
		if (inoarr[inL+i]==root)
			break;
	}
	solve(preL+1,inL,postL,i);
	solve(preL+i+1,inL+i+1,postL+i,n-i-1);
}
int main()
{
	stack<int> sta;

	int n;
	cin>>n;
	int i,j;
	i=j=0;
	int a=2*n;
	while (a--)
	{
		string s1;
		cin>>s1;
		if(strcmp(s1.c_str(),"Push")==0)
		{
			int tmp;
			cin>>tmp;
			sta.push(tmp);
			prearr[i++]=tmp;

		}else
		{
			inoarr[j++]=sta.top();
			sta.pop();
		}
	}
	solve(0,0,0,i);
	cout<<posarr[0];
	for(int k=1;k<i;k++)
		cout<<" "<<posarr[k];

	return 0;
}
完全可以把n=1的情况去掉


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值