1

Gooooogle  •  4个月前


#include<iostream>
#include<stack>

using namespace std;

void tree(stack<int> q)
{
	int left,right;
	cin>>left;
	if(left!=-1)
	{
		q.push(left);
		tree(q);
		q.pop();
	}
	cin>>right;
	if(right!=-1)
	{
		q.push(right);
		tree(q);
		q.pop();
	}
	if(left==-1&&right==-1)
	{
		stack<int> t;
		while(!q.empty())
		{
			t.push(q.top());
			q.pop();
		}
		cout<<t.top();
		t.pop();
		while(!t.empty())
		{
			cout<<"->"<<t.top();
			t.pop();
		}
		cout<<endl;
	}
	return;
}

int main()
{
	stack<int> q;
	int t;
	cin>>t;
	q.push(t);
	tree(q);
	return 0;
}

评论:

请先登录,才能进行评论