本文主要是介绍1600. 王位继承顺序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1600. 王位继承顺序
题目链接:1600. 王位继承顺序
代码如下:
class ThroneInheritance {
public:ThroneInheritance(string kingName) {this->king=kingName;}void birth(string parentName, string childName) {edges[parentName].push_back(childName);}void death(string name) {dead.insert(name);}vector<string> getInheritanceOrder() {vector<string> res;//存储结果preOrder(king,res);return res;}private:unordered_map<string,vector<string>> edges;unordered_set<string> dead;string king;void preOrder(const string& name,vector<string>& res){if(!dead.count(name)){res.push_back(name);}if(edges.count(name)){for(const string& childName:edges[name]){preOrder(childName,res);}}}
};/*** Your ThroneInheritance object will be instantiated and called as such:* ThroneInheritance* obj = new ThroneInheritance(kingName);* obj->birth(parentName,childName);* obj->death(name);* vector<string> param_3 = obj->getInheritanceOrder();*/
这篇关于1600. 王位继承顺序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!