leetcode582

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode582相关的知识,希望对你有一定的参考价值。

public class Solution {
    public IList<int> KillProcess(IList<int> pid, IList<int> ppid, int kill)
        {
            if (kill == 0)
            {
                return pid;
            }

            int n = pid.Count;
            Dictionary<int, List<int>> tree = new Dictionary<int, List<int>>();
            for (int i = 0; i < n; i++)
            {
                tree.Add(pid[i], new List<int>());
            }
            for (int i = 0; i < n; i++)
            {
                if (tree.ContainsKey(ppid[i]))
                {
                    var children = tree[ppid[i]];
                    children.Add(pid[i]);
                    if (!tree.ContainsKey(ppid[i]))
                    {
                        tree.Add(ppid[i], children);
                    }
                }
            }

            List<int> result = new List<int>();
            traverse(tree, result, kill);

            return result;
        }

        private void traverse(Dictionary<int, List<int>> tree, List<int> result, int pid)
        {
            result.Add(pid);

            var children = tree[pid];
            foreach (var child in children)
            {
                traverse(tree, result, child);
            }
        }
}

https://leetcode.com/problems/kill-process/#/solutions

以上是关于leetcode582的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode] 582. Kill Process 终止进程

LeetCode 582. Kill Process 解题报告

奇安信集团笔试题:二叉树的最近公共祖先(leetcode236),杀死进程(leetcode582)

582,DFS解二叉树剪枝

LeetCode:Kill Process

LeetCode队列 queue(共8题)