The best way is through education, not only science and serious stuff, but also behavior and mindset training. Through early education, children can be taught to do the right things and have compassion towards others. Most of the terrorist are antisocial, so children should be trained how to socialize with others and help each other.
The other way, which is also important, is law enforcement. In mu opinion death sentence is the fairest penalty for those terrorist who cruelly kill many innocent people just because of their fanatic wrath. This penalty will cause deterrent effect, so logically it will decrease terrorism case if implemented properly.