C++ BFS solution, no recursion, no queue or stack, real O(n) space


  • 0
    M
        vector<int> killProcess(vector<int>& pid, vector<int>& ppid, int kill) {
            unordered_set<int> kil;
            vector<int> res(1, kill);
            int flag=1;
            kil.insert(kill);
            while(flag) {
                flag=0;
                for(int i=0;i<ppid.size();i++) {
                    if(kil.find(ppid[i])!=kil.end()) {
                        kil.insert(pid[i]);
                        res.push_back(pid[i]);
                        ppid[i]=-1;
                        flag=1;
                    }
                }
            }
            return res;
        }

Log in to reply
 

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