这段迭代 DFS 会输出什么?

来自 树与图
Node 24 进阶 2分钟

这段迭代 DFS 会输出什么?

JavaScript
const graph = new Map([
  ["A", ["B", "C"]],
  ["B", ["D"]],
  ["C", ["D"]],
  ["D", []],
]);
const seen = new Set();
const order = [];
const stack = ["A"];

while (stack.length > 0) {
  const vertex = stack.pop();
  if (seen.has(vertex)) continue;
  seen.add(vertex);
  order.push(vertex);
  const neighbors = graph.get(vertex) ?? [];
  for (let index = neighbors.length - 1; index >= 0; index -= 1) {
    stack.push(neighbors[index]);
  }
}

console.log(order.join(" -> "));
在试验场中打开
报告错误