forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1361.js
37 lines (35 loc) · 1012 Bytes
/
1361.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @param {number} n
* @param {number[]} leftChild
* @param {number[]} rightChild
* @return {boolean}
*/
var validateBinaryTreeNodes = function(n, leftChild, rightChild) {
let d = Array(n).fill(0);
for (let i of leftChild) if (i != -1) d[i]++;
for (let i of rightChild) if (i != -1) d[i]++;
let root = -1;
for (let i = 0; i < n; i++) {
if (d[i] == 0) {
if (root != -1) return false;
root = i;
}
}
if (root == -1) return false;
let vis = new Map(), q = [root];
vis.set(root, 1);
while (q.length > 0) {
let pre = q.shift();
if (leftChild[pre] != -1) {
if (vis.has(leftChild[pre])) return false;
vis.set(leftChild[pre], 1);
q.push(leftChild[pre]);
}
if (rightChild[pre] != -1) {
if (vis.has(rightChild[pre])) return false;
vis.set(rightChild[pre], 1);
q.push(rightChild[pre]);
}
}
return vis.size == n;
};