我想知道如何编写带有副作用的迭代DFS来遍历?
function DFS(x) {
x.in = time++ // this is obvious
foreach (child in x.children) {
DFS(child)
}
x.out = time++ // it looks problematic
}
其中time
通过引用访问(因此与所有foo
调用共享)。
使用堆栈进行迭代dfs,如果是递归dfs,则使用函数调用堆栈