title: Fill color before or after recursive call (Flood Fill algorithm)
body: <p>I was implementing a basic version of the flood fill algorithm when I ran into this doubt.</p>
  <p>When should you color the present cell (i.e. do <code>image[sr][sc] = newColor</code>) before the recursive calls or after the recursive calls? Why is there a difference between the two approaches? When the current cell is colored before the recursive calls works but if I change the order then it gives segmentation error.</p>
  <p>Here's the code:</p>
  <pre class="lang-cpp prettyprint-override"><code>vector&lt;vector&lt;int&gt;&gt; floodFill(vector&lt;vector&lt;int&gt;&gt;&amp; image, int sr, int sc, int newColor) {
      if(image.size()&lt;=0 || image[sr][sc] == newColor) return image;
      int rows = image.size(),cols=image[sr].size();
      int temp = image[sr][sc];
      image[sr][sc] = newColor;
      //check up
      if((sr-1)&gt;=0 &amp;&amp; image[sr-1][sc] == temp){
          image = floodFill(image,sr-1,sc,newColor);
      }
      //check left
      if((sc-1)&gt;=0 &amp;&amp; image[sr][sc-1] == temp){
          image = floodFill(image,sr,sc-1,newColor);
      }
      //check right
      if((sc+1)&lt;cols &amp;&amp; image[sr][sc+1] == temp){
          image = floodFill(image,sr,sc+1,newColor);
      }
      //check down
      if((sr+1)&lt;rows &amp;&amp; image[sr+1][sc] == temp){
          image = floodFill(image,sr+1,sc,newColor);
      }
      //if i put the image[sr][sc] = newColor; here it give seg error
      return image;
  }
  </code></pre>