#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct node{
int left = -1, right = - 1;
};
node nodes[31];
int N, root_id = -1, current_node = -1, current_back = -1;
void printTree(int root){
if(root < 0)
return;
printTree(nodes[root].left);
printTree(nodes[root].right);
cout<<root;
if(root != root_id)
cout<<" ";
}
int main()
{
int st[31];
bool left_flag = true;
cin>>N;
for(int i = 0; i < N * 2 ; i++){
string op;
cin>>op;
if(op == "Push"){
int id;
cin>>id;
if(root_id < 0)
root_id = id;
else
(left_flag?(nodes[current_node].left):(nodes[current_node].right)) = id;
st[++current_back] = id;
current_node = id;
left_flag = true;
}
else if(op == "Pop"){
current_node = st[current_back--];
left_flag = false;
}
}
printTree(root_id);
}
用LUA(和C++)刷PAT (Advanced Level) ——1086 Tree Traversals Again
最新推荐文章于 2025-04-11 15:47:45 发布