leetcode 1600. 王位继承顺序-内存100-Throne Inheritance

张开发
2026/4/13 4:25:06 15 分钟阅读

分享文章

leetcode 1600. 王位继承顺序-内存100-Throne Inheritance
Problem: 1600. 王位继承顺序-内存100-Throne Inheritance内存100%通过观察可以发现拿到所有父母对应的孩子孩子越靠前年纪越大只需要深度优先搜索即可深度优先搜索得到的顺序就是最终的结果Codeclass ThroneInheritance { public: unordered_setstring dea; unordered_mapstring, vectorstring parentchild; vectorstring curOrder; string king; ThroneInheritance(string kingName) { king kingName; } void birth(string parentName, string childName) { parentchild[parentName].push_back(childName); } void death(string name) { dea.insert(name); } void Successor(string x) { if(dea.find(x) dea.end()) curOrder.push_back(x); if(parentchild.count(x) ! 0) { for(string s : parentchild[x]) { Successor(s); } } } vectorstring getInheritanceOrder() { curOrder.clear(); Successor(king); return curOrder; } };

更多文章