--- 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up