【letmefly】1600.王位继承顺序:深度优先搜索(dfs)
力扣题目链接:https://leetcode.cn/problems/throne-inheritance/
一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。
这个王国有一个明确规定的王位继承顺序,第一继承人总是国王自己。我们定义递归函数 successor(x, curorder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。
successor(x, curorder):
如果 x 没有孩子或者所有 x 的孩子都在 curorder 中:
如果 x 是国王,那么返回 null
否则,返回 successor(x 的父亲, curorder)
否则,返回 x 不在 curorder 中最年长的孩子
比方说,假设王国由国王,他的孩子 alice 和 bob (alice 比 bob 年长)和 alice 的孩子 jack 组成。
- 一开始,
curorder为["king"]. - 调用
successor(king, curorder),返回 alice ,所以我们将 alice 放入curorder中,得到["king", "alice"]。 - 调用
successor(alice, curorder),返回 jack ,所以我们将 jack 放入curorder中,得到["king", "alice", "jack"]。 - 调用
successor(jack, curorder),返回 bob ,所以我们将 bob 放入curorder中,得到["king", "alice", "jack", "bob"]。 - 调用
successor(bob, curorder),返回null。最终得到继承顺序为["king", "alice", "jack", "bob"]。
通过以上的函数,我们总是能得到一个唯一的继承顺序。
请你实现 throneinheritance 类:
throneinheritance(string kingname)初始化一个throneinheritance类的对象。国王的名字作为构造函数的参数传入。void birth(string parentname, string childname)表示parentname新拥有了一个名为childname的孩子。void death(string name)表示名为name的人死亡。一个人的死亡不会影响successor函数,也不会影响当前的继承顺序。你可以只将这个人标记为死亡状态。string[] getinheritanceorder()返回 除去 死亡人员的当前继承顺序列表。
示例:
输入:
["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]]
输出:
[null, null, null, null, null, null, null, ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"], null, ["king", "andy", "matthew", "alex", "asha", "catherine"]]
解释:
throneinheritance t= new throneinheritance("king"); // 继承顺序:king
t.birth("king", "andy"); // 继承顺序:king > andy
t.birth("king", "bob"); // 继承顺序:king > andy > bob
t.birth("king", "catherine"); // 继承顺序:king > andy > bob > catherine
t.birth("andy", "matthew"); // 继承顺序:king > andy > matthew > bob > catherine
t.birth("bob", "alex"); // 继承顺序:king > andy > matthew > bob > alex > catherine
t.birth("bob", "asha"); // 继承顺序:king > andy > matthew > bob > alex > asha > catherine
t.getinheritanceorder(); // 返回 ["king", "andy", "matthew", "bob", "alex", "asha", "catherine"]
t.death("bob"); // 继承顺序:king > andy > matthew > bob(已经去世)> alex > asha > catherine
t.getinheritanceorder(); // 返回 ["king", "andy", "matthew", "alex", "asha", "catherine"]
提示:
1 <= kingname.length, parentname.length, childname.length, name.length <= 15kingname,parentname,childname和name仅包含小写英文字母。- 所有的参数
childname和kingname互不相同。 - 所有
death函数中的死亡名字name要么是国王,要么是已经出生了的人员名字。 - 每次调用
birth(parentname, childname)时,测试用例都保证parentname对应的人员是活着的。 - 最多调用
105次birth和death。 - 最多调用
10次getinheritanceorder。
解题方法:深度优先搜索(dfs)
其实不难发现,王位继承顺序就是多叉树前序遍历的顺序。
那么,我们只需要设计一个国王节点的数据结构:
除此之外,我们还需要一个哈希表,用来快速地从姓名映射到节点。
接着,如果是“新增节点”就新增节点,如果是“返回序列”就前序遍历,如果是“有人死亡”就标记死亡。
- 时间复杂度:初始化、单次出生、单次死亡 o ( 1 ) o(1) o(1);返回继承顺序 o ( n ) o(n) o(n)
- 空间复杂度:初始化、单次出生、单次死亡 o ( 1 ) o(1) o(1);总计 o ( n ) o(n) o(n)
ac代码
c++
struct kingnode {
string name;
vector<kingnode*> childs;
bool isalive;
kingnode(string name) : name(name) {
isalive = true;
}
};
class throneinheritance {
private:
unordered_map<string, kingnode*> ma;
kingnode* root;
vector<string> tempforinheritanceorder;
void dfs(kingnode* root) {
if (root->isalive) {
tempforinheritanceorder.push_back(root->name);
}
for (kingnode* child : root->childs) {
dfs(child);
}
}
public:
throneinheritance(string kingname) {
root = new kingnode(kingname);
ma[kingname] = root;
}
void birth(string parentname, string childname) {
kingnode* child = new kingnode(childname);
ma[childname] = child;
ma[parentname]->childs.push_back(child);
}
void death(string name) {
ma[name]->isalive = false;
}
vector<string> getinheritanceorder() {
tempforinheritanceorder.clear();
dfs(root);
return tempforinheritanceorder;
}
};
python
# from typing import list
class kingnode:
def __init__(self, name) -> none:
self.name = name
self.childs = []
self.isalive = true
class throneinheritance:
def _dfs(self, root: kingnode) -> none:
if root.isalive:
self.tempforinheritanceorder.append(root.name)
for child in root.childs:
self._dfs(child)
def __init__(self, kingname: str):
self.name2node = dict()
self.root = kingnode(kingname)
self.name2node[kingname] = self.root
def birth(self, parentname: str, childname: str) -> none:
child = kingnode(childname)
self.name2node[childname] = child
self.name2node[parentname].childs.append(child)
def death(self, name: str) -> none:
self.name2node[name].isalive = false
def getinheritanceorder(self) -> list[str]:
self.tempforinheritanceorder = []
self._dfs(self.root)
return self.tempforinheritanceorder
发表评论