Agree with JJeng. If getUnvisitedChildNode() never returns null, the code in dfs will always be adding another node to the stack, thus the while loop checking the stack size will never quit.