C++ sol using recursion, without stack


#1

int Solution::evalRPN(vector &A)
{
if(A.size()==1)
return stoi(A[0]);

char op=A[A.size()-1][0];
A.pop_back();

int left;
int right;

string temp1=A[A.size()-1];
if(temp1!="+"&&temp1!="-"&&temp1!="/"&&temp1!="*")
{
    right=stoi(temp1);
    A.pop_back();
}
else
{
    right=evalRPN(A);
}

string temp2=A[A.size()-1];
if(temp2!="+"&&temp2!="-"&&temp2!="/"&&temp2!="*")
{
    left=stoi(temp2);
    A.pop_back();
}
else
{
    left=evalRPN(A);
}
 
if(op=='+')
return left+right;
if(op=='-')
return left-right;
if(op=='*')
return left*right;
if(op=='/')
return left/right;

}