Rosalind编程问题之计算祖先数。
Counting Phylogenetic AncestorsProblem:
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即可。



