n nodes numbered 0 to n - 1. For each node i you know its left child left[i] and its right child right[i]; -1 means there is no child on that side. The nodes carry no values, only their numbers matter.
Return True if these links form exactly one valid binary tree containing all n nodes, and False otherwise.n=5, left=[3, 2, -1, -1, -1], right=[1, 4, -1, -1, -1] → True
Node 0 is the root with children 3 and 1, and node 1 has children 2 and 4. All five nodes are in one tree.
n=3, left=[1, -1, -1], right=[-1, -1, -1] → False
Node 2 is not linked to anything, so there are two trees instead of one.
n=3, left=[1, 2, -1], right=[2, -1, -1] → False
Node 2 has two parents, 0 and 1, which cannot happen in a tree.
n=5, left=[3, 2, -1, -1, -1], right=[1, 4, -1, -1, -1]
True
Run checks the first 3 tests in your browser. Submit checks all 140 tests on the server, hidden ones included.