The psychological examination of terrorism has shown the good news that it's not really linked to how poor, religious, or mentally unstable the people are. The poorest countries are not the ones that create the most terrorism, many terrorists are educated and middle class or higher (with respect to the countries they came from, anyway), and unsurprisingly, a lot of terrorists aren't devoutly religious. Terror cells require secrecy and organization, so while crazy guys might seem easier to persuade to do crazy things, they have the drawback of being a liability to whatever plan is being hatched.
The bad news is that the most common link is entirely too common: angry young males ranging from their late adolescence to their 20s. The demographic that commits the most violent crime of any variety. Doing something about angry young men who feel they have nothing to lose and are willing to hurt people for a cause is going to be a bit complicated.