具有副作用的迭代DFS

问题描述 投票:-2回答:1

我想知道如何编写带有副作用的迭代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调用共享)。

algorithm recursion graph iteration
1个回答
1
投票

使用堆栈进行迭代dfs,如果是递归dfs,则使用函数调用堆栈

© www.soinside.com 2019 - 2024. All rights reserved.