Validate Binary Tree Nodes Problem
Description
LeetCode Problem 1361.
You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree.
If node i has no left child then leftChild[i] will equal -1, similarly for the right child.
Note that the nodes have no values and that we only use the node numbers in this problem.
Example 1:

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

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

Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
Output: false
Constraints:
n == leftChild.length == rightChild.length1 <= n <= 10^4-1 <= leftChild[i], rightChild[i] <= n - 1
Solution
Approach: Find the Root, Then Walk Down from It
The nodes form exactly one binary tree when there is a single root and every other node can be reached from it along exactly one path. What can go wrong:
- a node has two parents (it is someone's child twice, maybe even the left and right child of the same node);
- there is a cycle, for example
0 -> 1 -> 0; - the nodes split into several pieces, each with its own root, or a root's tree plus a separate cycle.
So:
- Find the root. Mark every node that appears in
leftChildorrightChild. The root must be a node that is nobody's child. If every node is someone's child, the edges contain a cycle, so returnfalse. If there are several unmarked nodes, just take any one: the next step fails anyway, because it cannot reach the others. - Walk down from the root with a BFS (or DFS), following
leftChildandrightChild. If a child was already visited, it has a second parent or the walk went around a cycle: returnfalse. - Check that every node was reached. If fewer than
nnodes were visited, some nodes are not in the root's tree (another piece, or a cycle with no way in from the root, as inn = 4, leftChild = [1,-1,3,-1], rightChild = [-1,-1,-1,2]).
If all three checks pass, every node except the root was reached exactly once from its single parent, so the structure is one valid tree.
Use an explicit queue rather than recursion: a valid tree may be a chain of 104 nodes.
Complexity: O(n) time and O(n) space for the marks and the queue.
class Solution:
def validateBinaryTreeNodes(self, n: int, leftChild: List[int], rightChild: List[int]) -> bool:
# The root is the node that is nobody's child.
is_child = [False] * n
for c in leftChild + rightChild:
if c != -1:
is_child[c] = True
root = next((i for i in range(n) if not is_child[i]), -1)
if root == -1:
return False # every node has a parent: there is a cycle
# Walk down from the root; a node reached twice has two parents (or closes a cycle).
seen = [False] * n
seen[root] = True
queue = [root]
for node in queue:
for c in (leftChild[node], rightChild[node]):
if c != -1:
if seen[c]:
return False
seen[c] = True
queue.append(c)
return len(queue) == n # every node must hang below this one root