栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Rosalind Java|Counting Phylogenetic Ancestors

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Rosalind Java|Counting Phylogenetic Ancestors

Rosalind编程问题之计算祖先数。

Counting Phylogenetic Ancestors

Problem:
Even though a binary tree can include nodes having degree 2, an unrooted binary tree is defined more specifically: all internal nodes have degree 3. In turn, a rooted binary tree is such that only the root has degree 2 (all other internal nodes have degree 3).

Given: A positive integer n (3≤n≤10000).
Sample input:

4

Return: The number of internal nodes of any unrooted binary tree having n leaves.
Sample output:

2


这道题非常简单,甚至不需要编写代码,但前提是思路要到位。
经过数树一道题我们知道,一棵有n个叶节点的无根树共存在n-1条边,每条边两端点各包含一个度(degree)。本题假定存在一棵有n个叶节点的无根树。需要我们计算其内部节点总数(祖先数)。对于一棵无根树,其叶节点的度为1,内部节点的度为3。我们如果设内部节点数为k,可以因此列出等式:3k+n=2(n+k-1)。因此可以得出k=n-2。

所以本道题只需要在给出的数值上减去2即可。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/927066.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号