c# solution with stack


  • 0
    N
        public IList<int> KillProcess(IList<int> pid, IList<int> ppid, int kill)
        {
                var list = new List<int>();
                if (!pid.Contains(kill)) return list;
    
                var dict = new Dictionary<int, List<int>>();
                for(var i = 0; i < ppid.Count; ++i)
                {
                    if (!dict.ContainsKey(ppid[i])) dict[ppid[i]] = new List<int>();
                    dict[ppid[i]].Add(pid[i]);
                }
    
                var s = new Stack<int>();
                s.Push(kill);
    
                while (s.Count > 0)
                {
                    var kp = s.Pop();
                    list.Add(kp);
    
                    if (dict.ContainsKey(kp))
                    {
                        foreach(var p in dict[kp])
                        {
                            s.Push(p);
                        }
                    }
                }
    
                return list;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.