题目

一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点,这个家庭里有人出生也有人死亡。

这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 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 <= 15
  • kingName,parentName, childName 和 name 仅包含小写英文字母。
  • 所有的参数 childName 和 kingName 互不相同。
  • 所有 death 函数中的死亡名字 name 要么是国王,要么是已经出生了的人员名字。
  • 每次调用 birth(parentName, childName) 时,测试用例都保证 parentName 对应的人员是活着的。
  • 最多调用 105 次birth 和 death 。
  • 最多调用 10 次 getInheritanceOrder 。

解题思路

通过map和list组合,key记录人名,value记录孩子名字,这样就构造出一个具有层级结构的表示继承关系的多层树,根节点是国王,因为list的遍历次序就是插入次序,因此使用深度优先搜索遍历的顺序,就是继承关系。使用set维护去世的人名,避免加入到结果中去。

代码

    class ThroneInheritance {Map<String,List<String>> map=new HashMap<>();Set<String> death=new HashSet<>();String king;public ThroneInheritance(String kingName) {king=kingName;map.put(kingName,new ArrayList<>());}public void birth(String parentName, String childName) {if(!map.containsKey(parentName))map.put(parentName,new ArrayList<>());map.get(parentName).add(childName);}public void death(String name) {death.add(name);}public void dfs(List<String> res,String cur){if(!death.contains(cur)) res.add(cur);if(map.containsKey(cur)){for (String s : map.get(cur)) {dfs(res,s);}}}public List<String> getInheritanceOrder() {ArrayList<String> res = new ArrayList<>();dfs(res,king);return res;}}
/*** Your ThroneInheritance object will be instantiated and called as such:* ThroneInheritance obj = new ThroneInheritance(kingName);* obj.birth(parentName,childName);* obj.death(name);* List<String> param_3 = obj.getInheritanceOrder();*/

leetcode 1600. 皇位继承顺序(dfs)相关推荐

  1. LeetCode 1600. 皇位继承顺序(图的深度优先遍历)

    文章目录 1. 题目 2. 解题 1. 题目 一个王国里住着国王.他的孩子们.他的孙子们等等.每一个时间点,这个家庭里有人出生也有人死亡. 这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己 ...

  2. 1600. 皇位继承顺序

    文章目录 题目描述 解题思路 代码实现 参考文献 1600. 皇位继承顺序 题目描述 解题思路 1.哈希表保存父子间的关系 2.保存死亡名单 3.前序遍历多叉树获取继承顺序并排除已死亡人员 代码实现 ...

  3. LeetCode Minimum Genetic Mutation(dfs,bfs)

    问题: 一条基因序列由一个带有8个字符的字符串表示,其中每个字符都属于 "A", "C", "G", "T"中的任意一个 ...

  4. LeetCode Construct Quad Tree(dfs)

    问题:给出一个n*n由0和1组成的二维数组,用四叉树来表示网格.要求返回四叉树的根结点. (1)如果当前网格值相同,则是叶子结点,将网格值赋值给结点 (2)如果当前网格值不同,则不是叶子结点,结点值可 ...

  5. LeetCode Path Sum II(dfs或者bfs)

    问题:给出一个树和一个数,求出从根结点到叶子结点路径和等于这个数的所有情况 思路: 1.深度优先搜索,在到达一个深度结点时,判断是否是叶子结点,并且判断和是否等于要求的数.如果满足,说明是满足条件的一 ...

  6. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  7. [leetcode]529. 扫雷游戏 DFS递归、BFS、DFS栈实现

    https://leetcode-cn.com/problems/minesweeper/ DFS步骤: 1.给的x,y坐标位置是地雷board[x][y] == 'M',把那个位置标记为地雷·boa ...

  8. leetcode 79.单词搜索 dfs

    原题链接 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单 ...

  9. 符号三角形-计算机算法设计与分析【1600+字解析 dfs全排列 列举情况】【题意分析】【算法分析】【思路是怎么来的】【过程是什么】

    符号三角形 题意分析 思路过程分析 算法分析 下图是由14个"+"和14个"-"组成的符号三角形.2个同号下面都是"+",2个异号下面都是& ...

最新文章

  1. RemoteFX原理简介
  2. android文件的读取方法,Android读取写入文件的方法
  3. secureCRT常用设置
  4. matlab 神经网络编程入门系列(1)
  5. html游戏代码_实现了代码自动生成,开发效率妥妥的提升,升职加薪跟上
  6. 安装配置Mysql主从
  7. 专注于皮肤病理诊断技术研发,贝叶科技赋予AI诊断可解释能力
  8. 如何更换里讯浏览器的皮肤?里讯浏览器更换皮肤的方法
  9. python网络爬虫_一篇文章教会你利用Python网络爬虫获取穷游攻略
  10. 最小二乘法移动最小二乘法
  11. 二叉搜索树的思想,以及增删查改的实现
  12. typescript parseint不能传number_Typescript 使用日志
  13. mathtype 公式分节隐藏
  14. 函数求和公式计算机出库入库,出库入库表格函数公式.doc
  15. Docker容器资源管理
  16. mysql 导出表结构或表数据的操作
  17. android x86玩和平精英,和平精英iOS和安卓可以一起玩吗 和平精英iOS和安卓数据互通吗...
  18. WAV转MP3格式最简单的方法[zz]
  19. Unity3D官方案例--太空射击游戏总结
  20. 计算机上怎么计算x的n次方,计算x的n次方(用函数)

热门文章

  1. 操作系统(五)输入/输出(I/O)管理
  2. 数学函数(C/C++)
  3. 进程间同步(互斥量、信号量)
  4. 【金三银四】启动mysql服务器
  5. 三年Java开发,java基础常问面试题
  6. HTML如何添加锚点,分享一点面试小经验
  7. 安卓开发面试书籍,全世界都在问Android开发凉了吗?建议收藏
  8. 2016面试——腾讯、蚂蚁金服、蘑菇街
  9. 高并发与负载均衡-keepalived-概念介绍
  10. Python 操作 MySQL 的5种方式(转)