<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:
    def validateBinaryTreeNodes(self, node_count: int, left_children: List[int], right_children: List[int]) -> bool:
        # Helper function to find the root of the set in which element x is.
        def find_root(x: int) -> int:
            if parents[x] != x:
                # Path compression: Directly attaching x to its root to optimize further queries
                parents[x] = find_root(parents[x])
            return parents[x]

        # Initialize each node to be its own parent (self loop).
        # This also sets up the forest for the union-find algorithm.
        parents = list(range(node_count))

        # Visited array to keep track if a node has already been assigned a parent.
        visited = [False] * node_count

        # Iterate over each node and their respective left and right children
        for i, (left_child, right_child) in enumerate(zip(left_children, right_children)):
            # Iterate over both children of the current node
            for child in (left_child, right_child):
                # If the child is not a placeholder value (-1 indicates no child)
                if child != -1:
                    # If the child has been visited, or if parent of current node and child node are the same
                    # then the tree is not valid.
                    if visited[child] or find_root(i) == find_root(child):
                        return False
                    # Union operation: set the parent of the current node's parent to the child's parent
                    # effectively linking the trees of the current node and child.
                    parents[find_root(i)] = find_root(child)
                    # Mark the child as visited
                    visited[child] = True
                    # Decrement the node count, aiming to validate if only one root exists at the end
                    node_count -= 1

        # The tree is valid if it has exactly one node that is not a child of any other node (hence one root).
        return node_count == 1

21250