<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary tree.</p>

<p>If node <code>i</code> has no left child then <code>leftChild[i]</code> will equal <code>-1</code>, similarly for the right child.</p>

<p>Note that the nodes have no values and that we only use the node numbers in this problem.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/08/23/1503_ex1.png" style="width: 195px; height: 287px;" />
<pre>
<strong>Input:</strong> n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/08/23/1503_ex2.png" style="width: 183px; height: 272px;" />
<pre>
<strong>Input:</strong> n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
<strong>Output:</strong> false
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/08/23/1503_ex3.png" style="width: 82px; height: 174px;" />
<pre>
<strong>Input:</strong> n = 2, leftChild = [1,0], rightChild = [-1,-1]
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == leftChild.length == rightChild.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>-1 &lt;= leftChild[i], rightChild[i] &lt;= n - 1</code></li>
</ul>

class Solution {
public:
    // time/space: O(n)/O(n)
    bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
        // find the parent for each node
        unordered_map<int, int> node2parent;
        for (int node = 0; node < n; node++) {
            if (leftChild[node] != -1) {
                // each node must have only 1 parent
                if (node2parent.count(leftChild[node]) != 0) return false;
                node2parent[leftChild[node]] = node;
            }
            if (rightChild[node] != -1) {
                // each node must have only 1 parent
                if (node2parent.count(rightChild[node]) != 0) return false;
                node2parent[rightChild[node]] = node;
            }
        }

        // find the root of the binary tree
        int root = -1;
        for (int node = 0; node < n; node++) {
            // a root node has no parent
            if (node2parent.count(node) == 0) {
                // return false if there are more than 1 root
                if (root != -1) return false;
                root = node;
            }
        }
        // no root is found
        if (root == -1) return false;

        // travel the binary tree from the root, there must be no cycle
        vector<bool> visited(n, false);
        if (hasCycle(root, leftChild, rightChild, visited)) return false;

        // all nodes must be visited
        for (int node = 0; node < n; node++) {
            if (!visited[node]) return false;
        }
        
        return true;
    }
private:
    bool hasCycle(int node, vector<int>& leftChild, vector<int>& rightChild, vector<bool>& visited) {
        // terminate
        if (node == -1) return false;
        if (visited[node]) return true;
        visited[node] = true;

        // enumerate
        bool is_left_cycle = hasCycle(leftChild[node], leftChild, rightChild, visited);
        bool is_right_cycle = hasCycle(rightChild[node], leftChild, rightChild, visited);
        return (is_left_cycle || is_right_cycle);
    }
};
40857