Redian新闻
>
请教一道G家onsite题。。。
avatar
请教一道G家onsite题。。。# JobHunting - 待字闺中
d*i
1
请问下题如何用有向图来实现?谢谢。
题目:
printing a tree structure with giving collection of pairs of child> relation. Need to first find the root, and validate wether the given
relations is a valid tree, and then printing.
问题一: 如何判断有valid tree
问题二: 如果print?
例:
给一个set,里面是一堆pair,每个pair里是两个string,一个first,一个second。
如果这堆pair能够构成一个树状结构,按照一定的格式打印这棵树
first-second关系类似paretnt-child关系
eg
set: (a, b) (b, c) (a, d) (d, e) (d, f) (d, g)
树状结构是root = a, root.left = b, root.right = d blah blah
打印结果:[space] 就是一个空格. 1point3acres.com/bbs
a
[space]b
[space][space]c
[space]d
[space][space]e
[space][space]f
[space][space]g.1
avatar
c*z
2
detect cycles? BFS?
avatar
l*8
3
一,indegree为0的是root
从root开始dfs,检测是否访问一个节点两次,如果是,就有cycle,不是tree。
二,dfs, 传一个参数表示level.

given

【在 d********i 的大作中提到】
: 请问下题如何用有向图来实现?谢谢。
: 题目:
: printing a tree structure with giving collection of pairs of : child> relation. Need to first find the root, and validate wether the given
: relations is a valid tree, and then printing.
: 问题一: 如何判断有valid tree
: 问题二: 如果print?
: 例:
: 给一个set,里面是一堆pair,每个pair里是两个string,一个first,一个second。
: 如果这堆pair能够构成一个树状结构,按照一定的格式打印这棵树

avatar
s*x
4
NB.

【在 l*********8 的大作中提到】
: 一,indegree为0的是root
: 从root开始dfs,检测是否访问一个节点两次,如果是,就有cycle,不是tree。
: 二,dfs, 传一个参数表示level.
:
: given

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。