changed depthLastTraversal to depthFirstTraversal with an extra flag