多叉树先序遍历,LeetCode 1600. 王位继承顺序

目录

一、题目

1、题目描述

2、接口描述

python3

cpp

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解

python3

cpp


一、题目

1、题目描述

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

这个王国有一个明确规定的王位继承顺序,第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) ,给定一个人 x 和当前的继承顺序,该函数返回 x 的下一继承人。

Successor(x, curOrder):
    如果 x 没有孩子或者所有 x 的孩子都在 curOrder 中:
        如果 x 是国王,那么返回 null
        否则,返回 Successor(x 的父亲, curOrder)
    否则,返回 x 不在 curOrder 中最年长的孩子

比方说,假设王国由国王,他的孩子 Alice 和 Bob (Alice 比 Bob 年长)和 Alice 的孩子 Jack 组成。

  1. 一开始, curOrder 为 ["king"].
  2. 调用 Successor(king, curOrder) ,返回 Alice ,所以我们将 Alice 放入 curOrder 中,得到 ["king", "Alice"] 。
  3. 调用 Successor(Alice, curOrder) ,返回 Jack ,所以我们将 Jack 放入 curOrder 中,得到 ["king", "Alice", "Jack"] 。
  4. 调用 Successor(Jack, curOrder) ,返回 Bob ,所以我们将 Bob 放入 curOrder 中,得到 ["king", "Alice", "Jack", "Bob"] 。
  5. 调用 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() 返回 除去 死亡人员的当前继承顺序列表。

2、接口描述

python3
class ThroneInheritance:

    def __init__(self, kingName: str):


    def birth(self, parentName: str, childName: str) -> None:


    def death(self, name: str) -> None:


    def getInheritanceOrder(self) -> List[str]:



# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()
cpp
class ThroneInheritance {
public:
    ThroneInheritance(string kingName) {

    }
    
    void birth(string parentName, string childName) {

    }
    
    void death(string name) {

    }
    
    vector<string> getInheritanceOrder() {

    }
};

/**
 * 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();
 */

3、原题链接

1600. 王位继承顺序 - 力扣(LeetCode)


二、解题报告

1、思路分析

题目显然就是让我们维护一棵树,然后要求能够

  1. 删除树中节点(假删除)
  2. 插入子节点
  3. 返回树的遍历序列,要求按照时间戳先后顺序返回

由于插入子节点已经满足了时间先后,所以对于操作3我们直接返回先序序列即可

对于删除的节点我们放到哈希表里面,遍历的时候我计入答案即可

2、复杂度

时间复杂度:birthO(1)        deadO(1)        构造函数O(1)

查询继承序列O(n)

空间复杂度:O(n)

n为树中节点数目,由于字符串很短所以当作常数未列入复杂度计算

3、代码详解

python3
class ThroneInheritance:

    def __init__(self, kingName: str):
        self.king = kingName
        self.g = defaultdict(list)
        self.dead = set()

    def birth(self, parentName: str, childName: str) -> None:
        self.g[parentName].append(childName)

    def death(self, name: str) -> None:
        self.dead.add(name)

    def getInheritanceOrder(self) -> List[str]:
        ret = []
        def dfs(rt: str) -> None:
            if rt not in self.dead:
                ret.append(rt)
            if rt in self.g:
                for son in self.g[rt]:
                    dfs(son)
        dfs(self.king)
        return ret



# Your ThroneInheritance object will be instantiated and called as such:
# obj = ThroneInheritance(kingName)
# obj.birth(parentName,childName)
# obj.death(name)
# param_3 = obj.getInheritanceOrder()
cpp
class ThroneInheritance {
public:
    unordered_map<string, vector<string>> g;
    unordered_set<string> dead;
    string king;
    ThroneInheritance(string kingName): king(move(kingName)) {}
    
    void birth(string parentName, string childName) {
        g[move(parentName)].emplace_back(move(childName));
    }
    
    void death(string name) {
        dead.insert(move(name));
    }
    
    vector<string> getInheritanceOrder() {
        vector<string> ret;
        function<void(const string&)> dfs = [&](const string& rt){
            if(!dead.count(rt))
                ret.emplace_back(rt);
            if(g.count(rt))
                for(const string& son : g[rt])
                    dfs(son);
        };
        dfs(king);
        return ret;
    }
};

/**
 * 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();
 */

相关推荐

  1. LeetCode 1600. 王位继承顺序

    2024-04-08 21:24:02       37 阅读
  2. 题目:王位继承顺序

    2024-04-08 21:24:02       32 阅读
  3. 2024-04-08 21:24:02       49 阅读
  4. LeetCode热题100】【二】二的层

    2024-04-08 21:24:02       44 阅读
  5. ,中,后

    2024-04-08 21:24:02       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-08 21:24:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 21:24:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 21:24:02       87 阅读
  4. Python语言-面向对象

    2024-04-08 21:24:02       96 阅读

热门阅读

  1. 【初识C语言】1

    2024-04-08 21:24:02       44 阅读
  2. GBase 8s Docker镜像说明

    2024-04-08 21:24:02       36 阅读
  3. Apache—POI详解、小案例展示

    2024-04-08 21:24:02       34 阅读
  4. C语言 超市商品(Goods) 销售 (Stock) 信息管理软件

    2024-04-08 21:24:02       31 阅读
  5. PgSQL的with as语法

    2024-04-08 21:24:02       42 阅读
  6. 【LeetCode热题100】【栈】柱状图中最大的矩形

    2024-04-08 21:24:02       29 阅读
  7. vscode配置vue + python

    2024-04-08 21:24:02       44 阅读
  8. Cisco Nexus 93180做为fex使用

    2024-04-08 21:24:02       39 阅读
  9. LeetCode 1600.王位继承顺序:深度优先搜索(DFS)

    2024-04-08 21:24:02       35 阅读
  10. Acwing2024蓝桥杯贡献法

    2024-04-08 21:24:02       33 阅读
  11. KVM基础管理命令

    2024-04-08 21:24:02       34 阅读
  12. C++(10): std::map、std::multimap与std::unordered_map

    2024-04-08 21:24:02       30 阅读
  13. VUE 实现路由的基本原理

    2024-04-08 21:24:02       29 阅读
  14. 如何进行Python代码的调试和测试?

    2024-04-08 21:24:02       31 阅读
  15. C# 一种基于 event 和 委托的事件机制

    2024-04-08 21:24:02       35 阅读
  16. pytest中文使用文档----11测试的参数化

    2024-04-08 21:24:02       33 阅读