Reverse Breadth First traversal in C#

后端 未结 2 1569
无人及你
无人及你 2021-02-04 05:09

Anyone has a ready implementation of the Reverse Breadth First traversal algorithm in C#?

By Reverse Breadth First traversal , I mean instead of searching a tree startin

2条回答
  •  面向向阳花
    2021-02-04 05:14

    Run a normal BFS from rootNode and let depth[i] = linked list of nodes with depth i. So for your example you'll have:

    depth[1] = {1}, depth[2] = {2, 3, 4} etc.. You can build this with a simple BFS search. Then print all the nodes in depth[maxDepth], then those in depth[maxDepth - 1] etc.

    The depth of a node i is equal to the depth of its father node + 1. The depth of the root node can be considered 1 or 0.

提交回复
热议问题