---
# System prepended metadata

title: 1361. Validate Binary Tree Nodes
tags: [graph]

---

---
title: 1361. Validate Binary Tree Nodes
tags: graph
description: share source code.
---

# 1361. Validate Binary Tree Nodes

```java=
class Solution {
    public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {
        int indegrees [] = new int [n];
        
        for(int i = 0; i < n; i++){
            if(leftChild[i] != -1) indegrees[leftChild[i]]++;
            if(rightChild[i] != -1) indegrees[rightChild[i]]++;
            if(indegrees[i] > 1){
                return false;
            }
        }
        
        Queue<Integer> q = new LinkedList<>();
        for(int i = 0; i < n; i ++){
            if(indegrees[i] == 0){
                 q.offer(i);
            }
        }
        
        if(q.size() > 1){
            return false;
        }
       
        while(!q.isEmpty()){
            int u = q.poll();
            if(leftChild[u] != -1 && --indegrees[leftChild[u]] == 0){
                  q.offer(leftChild[u]);
            }
                
            if(rightChild[u] != -1 && --indegrees[rightChild[u]] == 0){
                  q.offer(rightChild[u]); 
            }  
        }
        
        for(int i = 0; i < n; i ++){
            if(indegrees[i] > 0){
                return false;
            }
        }
        
        return true;
        
    }
}
```

