如何找到一棵树的叶子
有一个问题,想要找到一棵树的中心,并且想删除叶子,直到有两个以上的顶点。 但我怎样才能找到叶子呢?
PS对不起我的英语
There is a problem that wants to find the center of a tree and I want to delete the leaves until there are more than 2 vertices.
But how can I find the leaves ?
PS sorry for my English
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:我刚刚看到你可能有一棵有根的树。您可以对树进行 DFS,当从当前节点到与父节点不同的节点没有边时,您就找到了叶子。
引用 维基百科:
只需计算每个节点的度即可。如果它是
1
那么你就有了一片叶子。Edit: I have just seen you probably have a rooted tree. You can make a DFS of the tree and when from the current node you don't have an edge to a node different from the parent, then you have found a leaf.
Quoting Wikipedia:
Just count the degree of every node. If it is
1
then you have a leaf.