Not able to get the efficient code than this

#include<bits/stdc++.h>

using namespace std;

int main(){

int n;
stack<int>S;
stack<int>S1,S2;
int ch;
int val;
cin>>n;

for(int i=0;i<n;i++){
	cin>>ch;
	if(ch==2){
		cin>>val;
		S.push(val);		
	}
	if(ch==1){
		S1.push(S.top());
		S.pop();
	}	
}

while(!S1.empty()){
	S2.push(S1.top());
	S1.pop();
}

while(!S2.empty()){
	cout<<S2.top()<<endl;
	S2.pop();
}	


return 0;

}

@Gunit Please provide code in coding blocks IDE

My code : https://ide.codingblocks.com/s/207098

@Gunit there is no need to create multiple stacks, simply use one stack, push element for query two, for query one output top element and pop from stack.


If this resolves your doubt mark it as resolved.

Sir there is one more query. Actually I have given Pre and Inorder array. I have to create a binary tree.But I am unable to do that.Here is my code: https://ide.codingblocks.com/s/207828

@Gunit It would be better if you raise this doubt under topics it occurs it would be help for both of us.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.