1

噢莫加纳加加加 · 发布于 2024-06-01 · 浏览 510 回复 0
#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;
}

全部回复 共 0 条

还没有人回复。往下写第一条。

发表回复