classSolution:defvalidateBinaryTreeNodes(self,n:int,leftChild,rightChild) : indeg = [0]*n#统计入度for i inrange(n):if leftChild[i]!=-1: indeg[leftChild[i]]+=1if rightChild[i]!=-1: indeg[rightChild[i]]+=1# print(indeg) root =-1#找一个入度为0的作为rootfor i inrange(n):if indeg[i]==0: root = ibreakif root ==-1:returnFalse q = [root] visited =set([root])while q: node = q.pop(0)#左子 left = leftChild[node]if left !=-1:#入队时就标记访问if left in visited:returnFalse visited.add(left) q.append(left)#右子 right = rightChild[node]if right !=-1:if right in visited:returnFalse visited.add(right) q.append(right)returnlen(visited)== n