<p>一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。</p> <p>这个王国有一个明确规定的王位继承顺序,第一继承人总是国王自己。我们定义递归函数 <code>Successor(x, curOrder)</code> ,给定一个人 <code>x</code> 和当前的继承顺序,该函数返回 <code>x</code> 的下一继承人。</p> <pre> Successor(x, curOrder): 如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中: 如果 x 是国王,那么返回 null 否则,返回 Successor(x 的父亲, curOrder) 否则,返回 x 不在 curOrder 中最年长的孩子 </pre> <p>比方说,假设王国由国王,他的孩子 Alice 和 Bob (Alice 比 Bob 年长)和 Alice 的孩子 Jack 组成。</p> <ol> <li>一开始, <code>curOrder</code> 为 <code>["king"]</code>.</li> <li>调用 <code>Successor(king, curOrder)</code> ,返回 Alice ,所以我们将 Alice 放入 <code>curOrder</code> 中,得到 <code>["king", "Alice"]</code> 。</li> <li>调用 <code>Successor(Alice, curOrder)</code> ,返回 Jack ,所以我们将 Jack 放入 <code>curOrder</code> 中,得到 <code>["king", "Alice", "Jack"]</code> 。</li> <li>调用 <code>Successor(Jack, curOrder)</code> ,返回 Bob ,所以我们将 Bob 放入 <code>curOrder</code> 中,得到 <code>["king", "Alice", "Jack", "Bob"]</code> 。</li> <li>调用 <code>Successor(Bob, curOrder)</code> ,返回 <code>null</code> 。最终得到继承顺序为 <code>["king", "Alice", "Jack", "Bob"]</code> 。</li> </ol> <p>通过以上的函数,我们总是能得到一个唯一的继承顺序。</p> <p>请你实现 <code>ThroneInheritance</code> 类:</p> <ul> <li><code>ThroneInheritance(string kingName)</code> 初始化一个 <code>ThroneInheritance</code> 类的对象。国王的名字作为构造函数的参数传入。</li> <li><code>void birth(string parentName, string childName)</code> 表示 <code>parentName</code> 新拥有了一个名为 <code>childName</code> 的孩子。</li> <li><code>void death(string name)</code> 表示名为 <code>name</code> 的人死亡。一个人的死亡不会影响 <code>Successor</code> 函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。</li> <li><code>string[] getInheritanceOrder()</code> 返回 <strong>除去</strong> 死亡人员的当前继承顺序列表。</li> </ul> <p> </p> <p><strong>示例:</strong></p> <pre> <strong>输入:</strong> ["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"] [["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]] <strong>输出:</strong> [null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]] <strong>解释:</strong> ThroneInheritance t= new ThroneInheritance("king"); // 继承顺序:<strong>king</strong> t.birth("king", "andy"); // 继承顺序:king > <strong>andy</strong> t.birth("king", "bob"); // 继承顺序:king > andy > <strong>bob</strong> t.birth("king", "catherine"); // 继承顺序:king > andy > bob > <strong>catherine</strong> t.birth("andy", "matthew"); // 继承顺序:king > andy > <strong>matthew</strong> > bob > catherine t.birth("bob", "alex"); // 继承顺序:king > andy > matthew > bob > <strong>alex</strong> > catherine t.birth("bob", "asha"); // 继承顺序:king > andy > matthew > bob > alex > <strong>asha</strong> > catherine t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"] t.death("bob"); // 继承顺序:king > andy > matthew > <strong>bob(已经去世)</strong>> alex > asha > catherine t.getInheritanceOrder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "catherine"] </pre> <p> </p> <p><strong>提示:</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> 和 <code>name</code> 仅包含小写英文字母。</li> <li>所有的参数 <code>childName</code> 和 <code>kingName</code> <strong>互不相同</strong>。</li> <li>所有 <code>death</code> 函数中的死亡名字 <code>name</code> 要么是国王,要么是已经出生了的人员名字。</li> <li>每次调用 <code>birth(parentName, childName)</code> 时,测试用例都保证 <code>parentName</code> 对应的人员是活着的。</li> <li>最多调用 <code>10<sup>5</sup></code> 次<code>birth</code> 和 <code>death</code> 。</li> <li>最多调用 <code>10</code> 次 <code>getInheritanceOrder</code> 。</li> </ul>