why is this code giving segmentation fault for 2 3 4 2 -1 while running perfectly on codeblocks?
Problem with code
Most probably you are trying to access a null node somewhere due to which segmentation fault. Dry run your code with the sample test case once and check.