> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Validate Binary Tree Nodes Python Solution

> Tested Python solution for LeetCode 1361 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1361, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Breadth-First Search](/catalog/topics/breadth-first-search), [Union Find](/catalog/topics/union-find), [Graph](/catalog/topics/graph). [View on LeetCode](https://leetcode.com/problems/validate-binary-tree-nodes/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1361   # by problem number
lcpy gen -s validate_binary_tree_nodes   # by problem name
```

## Problem

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 all the given nodes form \<strong>exactly one valid binary tree\</strong>.

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

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

### Examples

```
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
Output: true
```

```
Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
Output: false
```

```
Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
Output: false
```

### Constraints

* n == leftChild.length == rightChild.length
* 1 \<= n \<= 10^4
* -1 \<= leftChild\[i], rightChild\[i] \<= n - 1

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/validate_binary_tree_nodes/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/validate_binary_tree_nodes/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def validate_binary_tree_nodes(
        self, n: int, left_child: list[int], right_child: list[int]
    ) -> bool:
        # A valid binary tree: exactly one root (in-degree 0), every other
        # node has in-degree 1, and all nodes are reachable from the root.
        indegree = [0] * n
        for child in left_child + right_child:
            if child == -1:
                continue
            indegree[child] += 1
            if indegree[child] > 1:
                return False

        roots = [i for i in range(n) if indegree[i] == 0]
        if len(roots) != 1:
            return False

        seen = [False] * n
        stack = [roots[0]]
        count = 0
        while stack:
            node = stack.pop()
            if seen[node]:
                return False
            seen[node] = True
            count += 1
            for child in (left_child[node], right_child[node]):
                if child != -1:
                    stack.append(child)
        return count == n
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.