Files
PAT/html/t1007.html
T

29 lines
1.5 KiB
HTML

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<h1>1007. Red-black Tree (35)</h1>
<div id="problemContent">
<p>There is a kind of binary tree named <b>red-black tree</b> in the data structure. It has the following 5 properties:</p>
<p>
(1) Every node is either red or black.<br/>
(2) The root is black.<br/>
(3) All the leaves are NULL nodes and are colored black.<br/>
(4) Each red node must have 2 black descends (may be NULL).<br/>
(5) All simple paths from any node x to a descendant leaf have the same number of black nodes.</p>
<p>
We call a non-NULL node an <b>internal node</b>. From property (5) we can define the <b>black-height</b> of a red-black tree as the number of nodes on the simple path from the root (excluding the root itself) to any NULL leaf (including the NULL leaf). And we can derive that a red-black tree with black height H has at least 2<sup>H</sup>-1 internal nodes.</p>
<p>Here comes the question: given a positive N, how many distinct red-black trees are there that consist of exactly N internal nodes?</p>
<p><b>
Input Specification:
</b></p>
<p>Each input file contains one test case which gives a positive integer N (&lt;= 500).</p>
<p><b>
Output Specification:
</b></p>
<p>For each case, print in a line the number of distinct red-black tees with N internal nodes. Since the answer may be very large, output the remainder of it divided by 1000000007 please.</p>
<b>Sample Input:</b><pre>
5
</pre>
<b>Sample Output:</b><pre>
8
</pre>
</div>