Posted on

Description

Submission

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    Node* findRoot(vector<Node*> tree) {
        unordered_set<int> visited;   

        for(Node* node: tree) {
            for(Node* child: node->children) {
                if(child == nullptr) continue;
                visited.insert(child->val);
            }
        }

        for(Node* node: tree) {
            if(node == nullptr) continue;
            if(visited.find(node->val) == visited.end()) return node;
        }

        return nullptr;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *