Skip to content

Commit 3ec9ffc

Browse files
committed
Add solution #1361
1 parent 4657884 commit 3ec9ffc

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,254 LeetCode solutions in JavaScript
1+
# 1,255 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1035,6 +1035,7 @@
10351035
1358|[Number of Substrings Containing All Three Characters](./solutions/1358-number-of-substrings-containing-all-three-characters.js)|Medium|
10361036
1359|[Count All Valid Pickup and Delivery Options](./solutions/1359-count-all-valid-pickup-and-delivery-options.js)|Hard|
10371037
1360|[Number of Days Between Two Dates](./solutions/1360-number-of-days-between-two-dates.js)|Easy|
1038+
1361|[Validate Binary Tree Nodes](./solutions/1361-validate-binary-tree-nodes.js)|Medium|
10381039
1365|[How Many Numbers Are Smaller Than the Current Number](./solutions/1365-how-many-numbers-are-smaller-than-the-current-number.js)|Easy|
10391040
1366|[Rank Teams by Votes](./solutions/1366-rank-teams-by-votes.js)|Medium|
10401041
1368|[Minimum Cost to Make at Least One Valid Path in a Grid](./solutions/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1361. Validate Binary Tree Nodes
3+
* https://leetcode.com/problems/validate-binary-tree-nodes/
4+
* Difficulty: Medium
5+
*
6+
* You have n binary tree nodes numbered from 0 to n - 1 where node i has two children
7+
* leftChild[i] and rightChild[i], return true if and only if all the given nodes form
8+
* exactly one valid binary tree.
9+
*
10+
* If node i has no left child then leftChild[i] will equal -1, similarly for the right child.
11+
*
12+
* Note that the nodes have no values and that we only use the node numbers in this problem.
13+
*/
14+
15+
/**
16+
* @param {number} n
17+
* @param {number[]} leftChild
18+
* @param {number[]} rightChild
19+
* @return {boolean}
20+
*/
21+
var validateBinaryTreeNodes = function(n, leftChild, rightChild) {
22+
const parentCount = new Array(n).fill(0);
23+
let rootCandidate = -1;
24+
25+
for (let i = 0; i < n; i++) {
26+
if (leftChild[i] !== -1) parentCount[leftChild[i]]++;
27+
if (rightChild[i] !== -1) parentCount[rightChild[i]]++;
28+
}
29+
30+
for (let i = 0; i < n; i++) {
31+
if (parentCount[i] === 0) {
32+
if (rootCandidate !== -1) return false;
33+
rootCandidate = i;
34+
}
35+
if (parentCount[i] > 1) return false;
36+
}
37+
38+
if (rootCandidate === -1) return false;
39+
40+
return countNodes(rootCandidate, new Set()) === n;
41+
42+
function countNodes(node, visited) {
43+
if (node === -1) return 0;
44+
if (visited.has(node)) return -1;
45+
visited.add(node);
46+
const leftNodes = countNodes(leftChild[node], visited);
47+
const rightNodes = countNodes(rightChild[node], visited);
48+
if (leftNodes === -1 || rightNodes === -1) return -1;
49+
return 1 + leftNodes + rightNodes;
50+
}
51+
};

0 commit comments

Comments
 (0)