C++ DFS


  • 0

    Use a unordered_map<int, vector<int>> to keep the parent-children relationship and DFS from the kill process towards its children recursively.

    // OJ: https://leetcode.com/problems/kill-process
    // Auther: github.com/lzl124631x
    // Time: O(N)
    // Space: O(N)
    class Solution {
    private:
      unordered_map<int, vector<int>> m;
      vector<int> ans;
      void dfs(int kill) {
        ans.push_back(kill);
        for (int c : m[kill]) dfs(c);
      }
    public:
      vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
        for (int i = 0; i < pid.size(); ++i) m[ppid[i]].push_back(pid[i]);
        dfs(kill);
        return ans;
      }
    };
    

Log in to reply
 

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