File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } kingName
3
+ */
4
+ const ThroneInheritance = function ( kingName ) {
5
+ this . king = kingName
6
+ this . m = { }
7
+ this . dead = { }
8
+ } ;
9
+
10
+ /**
11
+ * @param {string } parentName
12
+ * @param {string } childName
13
+ * @return {void }
14
+ */
15
+ ThroneInheritance . prototype . birth = function ( parentName , childName ) {
16
+ if ( ! this . m [ parentName ] ) this . m [ parentName ] = [ ]
17
+ this . m [ parentName ] . push ( childName )
18
+ } ;
19
+
20
+ /**
21
+ * @param {string } name
22
+ * @return {void }
23
+ */
24
+ ThroneInheritance . prototype . death = function ( name ) {
25
+ this . dead [ name ] = 1
26
+ } ;
27
+
28
+ /**
29
+ * @return {string[] }
30
+ */
31
+ ThroneInheritance . prototype . getInheritanceOrder = function ( ) {
32
+ const res = [ ]
33
+ this . dfs ( res , this . king )
34
+ return res
35
+ } ;
36
+ ThroneInheritance . prototype . dfs = function ( ans , root ) {
37
+ if ( ! this . dead [ root ] ) {
38
+ ans . push ( root ) ;
39
+ }
40
+ if ( ! this . m [ root ] ) return
41
+ for ( let child of this . m [ root ] ) {
42
+ this . dfs ( ans , child ) ;
43
+ }
44
+ } ;
45
+ /**
46
+ * Your ThroneInheritance object will be instantiated and called as such:
47
+ * var obj = new ThroneInheritance(kingName)
48
+ * obj.birth(parentName,childName)
49
+ * obj.death(name)
50
+ * var param_3 = obj.getInheritanceOrder()
51
+ */
You can’t perform that action at this time.
0 commit comments