java - 如何检查一个子节点在图中是否有多个父节点?打印父母和那个孩子
问题描述
我想编写遍历图的代码并检查是否有任何节点有多个父节点。并打印父节点和子节点的列表。
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 是否有多个父节点?
解决方案
如果我理解得很好 A 有孩子 B,C,D B bas 孩子 D C 没有孩子。如果是这样,如果您遍历节点并为每个子商店在地图中遇到该孩子的次数
推荐阅读
- bash - 将日期/时间值传递给 bash 函数中的触摸命令
- javascript - 如何保存 blob 并使用 wavesurfer 播放
- python - 数据框将现有列值更改为单个特定值
- azure-cognitive-search - Azure 认知搜索 - 通过 API 创建数据源
- sql - 将 varchar 值 ' 和 f.FUNCTION_ID= CAST(' 转换为数据类型 int 时转换失败
- javascript - 如何在 JavaScript 代码中添加 HTML 按钮?
- javascript - 反应打开文件对话框
- javascript - NodeJS Mocha 不会将参数传递给函数调用
- javascript - 在 GET 上发送嵌套对象
- swift - 当从一个视图控制器转到另一个视图控制器(如 Swift 中的 Instagram 或 Facebook)时,如何在表视图中保留当前的 IndexPath.row?