use a stack to replace left and right expand array