Class ThroneInheritance

java.lang.Object
g1501_1600.s1600_throne_inheritance.ThroneInheritance

public class ThroneInheritance extends Object
1600 - Throne Inheritance.<p>Medium</p> <p>A kingdom consists of a king, his children, his grandchildren, and so on. Every once in a while, someone in the family dies or a child is born.</p> <p>The kingdom has a well-defined order of inheritance that consists of the king as the first member. Let&rsquo;s define the recursive function <code>Successor(x, curOrder)</code>, which given a person <code>x</code> and the inheritance order so far, returns who should be the next person after <code>x</code> in the order of inheritance.</p> <p>Successor(x, curOrder): if x has no children or all of x&rsquo;s children are in curOrder: if x is the king return null else return Successor(x&rsquo;s parent, curOrder) else return x&rsquo;s oldest child who&rsquo;s not in curOrder</p> <p>For example, assume we have a kingdom that consists of the king, his children Alice and Bob (Alice is older than Bob), and finally Alice&rsquo;s son Jack.</p> <ol> <li>In the beginning, <code>curOrder</code> will be <code>[&quot;king&quot;]</code>.</li> <li>Calling <code>Successor(king, curOrder)</code> will return Alice, so we append to <code>curOrder</code> to get <code>[&quot;king&quot;, &quot;Alice&quot;]</code>.</li> <li>Calling <code>Successor(Alice, curOrder)</code> will return Jack, so we append to <code>curOrder</code> to get <code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;]</code>.</li> <li>Calling <code>Successor(Jack, curOrder)</code> will return Bob, so we append to <code>curOrder</code> to get <code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;, &quot;Bob&quot;]</code>.</li> <li>Calling <code>Successor(Bob, curOrder)</code> will return <code>null</code>. Thus the order of inheritance will be <code>[&quot;king&quot;, &quot;Alice&quot;, &quot;Jack&quot;, &quot;Bob&quot;]</code>.</li> </ol> <p>Using the above function, we can always obtain a unique order of inheritance.</p> <p>Implement the <code>ThroneInheritance</code> class:</p> <ul> <li><code>ThroneInheritance(string kingName)</code> Initializes an object of the <code>ThroneInheritance</code> class. The name of the king is given as part of the constructor.</li> <li><code>void birth(string parentName, string childName)</code> Indicates that <code>parentName</code> gave birth to <code>childName</code>.</li> <li><code>void death(string name)</code> Indicates the death of <code>name</code>. The death of the person doesn&rsquo;t affect the <code>Successor</code> function nor the current inheritance order. You can treat it as just marking the person as dead.</li> <li><code>string[] getInheritanceOrder()</code> Returns a list representing the current order of inheritance <strong>excluding</strong> dead people.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong></p> <p>[&ldquo;ThroneInheritance&rdquo;, &ldquo;birth&rdquo;, &ldquo;birth&rdquo;, &ldquo;birth&rdquo;, &ldquo;birth&rdquo;, &ldquo;birth&rdquo;, &ldquo;birth&rdquo;, &ldquo;getInheritanceOrder&rdquo;, &ldquo;death&rdquo;, &ldquo;getInheritanceOrder&rdquo;]</p> <p>[[&ldquo;king&rdquo;], [&ldquo;king&rdquo;, &ldquo;andy&rdquo;], [&ldquo;king&rdquo;, &ldquo;bob&rdquo;], [&ldquo;king&rdquo;, &ldquo;catherine&rdquo;], [&ldquo;andy&rdquo;, &ldquo;matthew&rdquo;], [&ldquo;bob&rdquo;, &ldquo;alex&rdquo;], [&ldquo;bob&rdquo;, &ldquo;asha&rdquo;], [null], [&ldquo;bob&rdquo;], [null]]</p> <p><strong>Output:</strong> [null, null, null, null, null, null, null, [&ldquo;king&rdquo;, &ldquo;andy&rdquo;, &ldquo;matthew&rdquo;, &ldquo;bob&rdquo;, &ldquo;alex&rdquo;, &ldquo;asha&rdquo;, &ldquo;catherine&rdquo;], null, [&ldquo;king&rdquo;, &ldquo;andy&rdquo;, &ldquo;matthew&rdquo;, &ldquo;alex&rdquo;, &ldquo;asha&rdquo;, &ldquo;catherine&rdquo;]]</p> <p><strong>Explanation:</strong></p> <p>ThroneInheritance t= new ThroneInheritance(&ldquo;king&rdquo;); // order: <strong>king</strong></p> <p>t.birth(&ldquo;king&rdquo;, &ldquo;andy&rdquo;); // order: king > <strong>andy</strong></p> <p>t.birth(&ldquo;king&rdquo;, &ldquo;bob&rdquo;); // order: king > andy > <strong>bob</strong></p> <p>t.birth(&ldquo;king&rdquo;, &ldquo;catherine&rdquo;); // order: king > andy > bob > <strong>catherine</strong></p> <p>t.birth(&ldquo;andy&rdquo;, &ldquo;matthew&rdquo;); // order: king > andy > <strong>matthew</strong> > bob > catherine</p> <p>t.birth(&ldquo;bob&rdquo;, &ldquo;alex&rdquo;); // order: king > andy > matthew > bob > <strong>alex</strong> > catherine</p> <p>t.birth(&ldquo;bob&rdquo;, &ldquo;asha&rdquo;); // order: king > andy > matthew > bob > alex > <strong>asha</strong> > catherine</p> <p>t.getInheritanceOrder(); // return [&ldquo;king&rdquo;, &ldquo;andy&rdquo;, &ldquo;matthew&rdquo;, &ldquo;bob&rdquo;, &ldquo;alex&rdquo;, &ldquo;asha&rdquo;, &ldquo;catherine&rdquo;]</p> <p>t.death(&ldquo;bob&rdquo;); // order: king > andy > matthew > <strong>bob</strong> > alex > asha > catherine t.getInheritanceOrder(); // return [&ldquo;king&rdquo;, &ldquo;andy&rdquo;, &ldquo;matthew&rdquo;, &ldquo;alex&rdquo;, &ldquo;asha&rdquo;, &ldquo;catherine&rdquo;]</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= kingName.length, parentName.length, childName.length, name.length <= 15</code></li> <li><code>kingName</code>, <code>parentName</code>, <code>childName</code>, and <code>name</code> consist of lowercase English letters only.</li> <li>All arguments <code>childName</code> and <code>kingName</code> are <strong>distinct</strong>.</li> <li>All <code>name</code> arguments of <code>death</code> will be passed to either the constructor or as <code>childName</code> to <code>birth</code> first.</li> <li>For each call to <code>birth(parentName, childName)</code>, it is guaranteed that <code>parentName</code> is alive.</li> <li>At most <code>10<sup>5</sup></code> calls will be made to <code>birth</code> and <code>death</code>.</li> <li>At most <code>10</code> calls will be made to <code>getInheritanceOrder</code>.</li> </ul>