Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
This can be easily extended to N-ary trees, replace line10 with a loop that goes through all children and of course, push and pop all N operands.
do
{
while( p != NULL )
stack.push(p);
p = p->left;
while( !stack.empty() )
last = stack.pop();
lastbutone = stack.pop()
if( isoperator(lastbutone)
if( isoperand(lastbutone->right) ) - // line 10
stack.push(operate(last, lastbutone, lastbutone->right));
else{
push(lastbutone);
push(last);
break;
else
stack.push(operate(last, stack.pop(), lastbutone));
}while( !stack.empty() );
Use post-order traversal on the expression tree to evaluate that.
- amshali February 21, 2012N-ary operator should not be a problem if we follow a correct post-order traversal.