包含父项和子项的列表。这里根节点的父节点是-1。我必须从这里创建一棵树,但是任何子节点都不能在它的父节点之前创建。
父子列表
3 7
3 6
2 5
-1 1
2 4
1 2
1 3
发布于 2020-02-23 01:46:24
下面的算法将起作用:
Map
从Integer
初始化为List/Array[Integer]
。这里,key
代表Parent
,List/Array[Integer]
代表上述parent child list
上的Childrens
.Map
。即,对于上述示例,Map
将如下所示:-1 -> [1] 1 -> [2, 3] 2 -> [4, 5] 3 -> [6, 7]
Map
后,按键对Map
进行排序。为<代码>D22中的每个<代码>D21>创建Key, Value
对。这将确保始终在Child
之前准备好Parent
。
https://stackoverflow.com/questions/60350758
复制相似问题