I just got this question and it occurs to me this is a simple iteration of flood fill. X being one color, O being another. Visit each node, get its neighbors, put them on the neighbor stack. Go DFS and arbitrarily id go left to right...each time throwing neighbors into a stack. Put each node with its resultant x or o as data in a visited array. Go to the stack and pop off the first, get its data and check for neighbors, if they aren't on the stack put them on the stack. This will operate roughly Log(n) run time, as you won't iterate through the entire stack to find a win condition..