首页 > 解决方案 > 如何检查一个子节点在图中是否有多个父节点?打印父母和那个孩子

问题描述

我想编写遍历图的代码并检查是否有任何节点有多个父节点。并打印父节点和子节点的列表。

One node in the tree has multiple parents.
- Identify that node
- Identify the count of parents of that node and the return list of parents

    A
 /  |  \
B   |   C
  \ |
    D

TreeNode find_node_with_multiple_parents(TreeNode root)
List<TreeNode> find_parents_of_bad_node(TreeNode root)

在此图中,节点 D 是坏节点,因为它有多个父节点 A 和 B。我如何验证 D 是否有多个父节点?

标签: javagraphbreadth-first-search

解决方案


如果我理解得很好 A 有孩子 B,C,D B bas 孩子 D C 没有孩子。如果是这样,如果您遍历节点并为每个子商店在地图中遇到该孩子的次数


推荐阅读