皇位继承顺序

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

这个王国有一个明确规定的皇位继承顺序,第一继承人总是国王自己。我们定义递归函数 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() 返回 除去 死亡人员的当前继承顺序列表。

示例:

输入:
["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 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/throne-inheritance

解题思路

错误解题方法

本题的那点在于如何找到正确的位置放置元素。这里的关系有:父亲和儿子,儿子和儿子的儿子的关系。如何处理这两种关系成为解决本题的关键。我的解决办法是比较简单。使用的数据结构是一个数组和一个存放父子关系的table,每次插入的时候,先找到父亲的位置,然后再判断父亲之后的位置,是孩子还是孩子的孩子。因此想着brith函数对应的之后两代关系,因此只是在判断是孩子的孩子的时候,查找是否还是有父亲,如果有则不插入。

代码
type ThroneInheritance struct {
	SunAndParents map[string]string
	Names []string
}


func Constructor(kingName string) ThroneInheritance {
	name := []string{kingName}
	return ThroneInheritance{
		SunAndParents: make(map[string]string),
		Names: name,
	}
}


func (this *ThroneInheritance) Birth(parentName string, childName string)  {
	i := 0
	for i<len(this.Names) {
		// 判断是否等于parentName
		if this.Names[i] ==parentName {
			// 然后从这里开始循环
			y := i+1
			for y< len(this.Names){
				if this.SunAndParents[this.Names[y]] == parentName{
					y++
				}else if this.SunAndParents[this.Names[y]] != parentName{
					_,ok := this.SunAndParents[this.SunAndParents[this.Names[y]]]
					if ok {
						y++
						continue
					}
					tmp := make([]string,len(this.Names[y:]))
					copy(tmp,this.Names[y:])
					this.Names = append(this.Names[:y],childName)
					this.Names = append(this.Names,tmp...)
					this.SunAndParents[childName] = parentName
					break
				}
			}
		}
		i++
	}
	_,ok := this.SunAndParents[childName]
	if !ok {
		this.Names = append(this.Names,childName)
		this.SunAndParents[childName] = parentName
	}
}

func (this *ThroneInheritance) Death(name string)  {
	i := 0
	for i<len(this.Names) {
		if this.Names[i]==name {
			tmp := make([]string,len(this.Names[i+1:]))
			copy(tmp,this.Names[i+1:])
			this.Names = append(this.Names[:i],tmp...)
			break
		}
		i++
	}
}

func (this *ThroneInheritance) GetInheritanceOrder() []string {
	return this.Names
}

这样解法导致,假如:a和b的父亲都是c,然后a生了个孩子d,应该是在b的前面,但是讲过之前的算法,就会导致d在b的后面。

正确做法

不应该将重点放在添加上面,应该将重点放在遍历上。因此为了使得添加方面,只需要在属于同一个父亲的放在同一个地方就可以了。遍历的时候从根父亲开始遍历,遇到有孩子的先便利孩子,遍历完之后再继续遍历后面的元素。在获取值的时候,判断这个人是否已经死亡,如果死亡就不需要加入到结果中。

因此数据结构设置为:根父亲名字、父亲拥有的孩子表、死亡名单。

存储结果例子为:

这个看起来是不是像个多叉树呢!而且根据题目的结果值,可以判定是对整个树进行前序遍历的过程。因此代码为

代码
// 这样表示为树的结构也挺简单的
type ThroneInheritance struct {
	Root string
	Edge map[string][]string    // 这里的key值表示为父亲的名字,value表示为存储孩子
	Dead map[string]bool     // key表示为人 value表示是否已经死亡
}


func Constructor(kingName string) ThroneInheritance {
	return ThroneInheritance{
		Root: kingName,
		Edge: make(map[string][]string),
		Dead: make(map[string]bool),
	}
}

func (this *ThroneInheritance) Birth(parentName string, childName string)  {
	this.Edge[parentName] = append(this.Edge[parentName],childName)
}

func (this *ThroneInheritance) Death(name string)  {
	this.Dead[name] = true
}

func (this *ThroneInheritance) GetInheritanceOrder() []string {
	result := make([]string,0)
	var getOrder func(string)
	getOrder = func(parentName string){
		if !this.Dead[parentName] {
			result = append(result,parentName)
		}
		for i:=0;i<len(this.Edge[parentName]);i++{
			getOrder(this.Edge[parentName][i])
		}
	}
	getOrder(this.Root)
	return result
}
原文地址:https://www.cnblogs.com/MyUniverse/p/14907059.html