<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>

# Data Structures: Array, Set (for tracking parent-child relationships)
# Algorithm: Tree Validation via In-degree and DFS
# Intuition: To determine if all given nodes form exactly one valid binary tree, we must ensure the following conditions:
# 1. **Single root**: The tree must have exactly one root (a node with no parent).
# 2. **No cycles**: The tree must not contain any cycles.
# 3. **All nodes are connected**: Every node must be part of the tree (i.e., no isolated components).
# We can use an in-degree array to check if nodes have more than one parent and perform DFS to ensure there are no cycles and all nodes are connected.

# Detailed Intuition:
# - We use an array to track the in-degree of each node (how many times each node is referenced as a child).
# - A valid binary tree must have exactly one node with in-degree 0 (the root), and all other nodes must have in-degree 1.
# - Using DFS, we ensure there are no cycles and that all nodes are reachable from the root.

# Pseudo code:
# 1. Initialize an in-degree array to track the number of parents each node has.
# 2. Iterate through the leftChild and rightChild arrays to fill the in-degree array and check for multiple parents.
# 3. Ensure there is exactly one root (a node with in-degree 0).
# 4. Use DFS from the root to check if the tree is connected and contains no cycles.
# 5. Return True if all conditions are met; otherwise, return False.

class Solution:
    def validateBinaryTreeNodes(self, n, leftChild, rightChild):
        in_degree = [0] * n  # Step 1: Initialize in-degree array to track parents
        
        # Step 2: Update in-degree based on leftChild and rightChild arrays
        for i in range(n):
            if leftChild[i] != -1:
                in_degree[leftChild[i]] += 1
                if in_degree[leftChild[i]] > 1:  # No node can have more than one parent
                    return False
            if rightChild[i] != -1:
                in_degree[rightChild[i]] += 1
                if in_degree[rightChild[i]] > 1:  # No node can have more than one parent
                    return False
        
        # Step 3: Find the root node (which has in-degree 0)
        root = -1
        for i in range(n):
            if in_degree[i] == 0:
                if root != -1:  # There should be exactly one root
                    return False
                root = i
        
        if root == -1:  # If no root exists
            return False
        
        # Step 4: Use DFS to check if the tree is connected and contains no cycles
        visited = set()

        def dfs(node):
            if node == -1 or node in visited:
                return
            visited.add(node)
            dfs(leftChild[node])
            dfs(rightChild[node])

        dfs(root)

        # Step 5: Ensure all nodes are visited
        return len(visited) == n

# Time Complexity: O(n), where n is the number of nodes. We traverse the node lists once to compute the in-degree and once for DFS.
# Space Complexity: O(n), for the in-degree array and the visited set.

23630