Try using a stack instead of recursion
test case
Compare recursion to stack
Stack produces wrong topo sort
merge