Title: #1361 - Validate Binary Tree Nodes - LeetCode JavaScript Solutions
Open Graph Title: #1361 - Validate Binary Tree Nodes - LeetCode JavaScript Solutions
X Title: #1361 - Validate Binary Tree Nodes - LeetCode JavaScript Solutions
Description: You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only ...
Open Graph Description: You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only ...
X Description: You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only ...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/validate-binary-tree-nodes
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/validate-binary-tree-nodes","name":"#1361 - Validate Binary Tree Nodes - LeetCode JavaScript Solutions","description":"You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only ...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * @param {number} n\n * @param {number[]} leftChild\n * @param {number[]} rightChild\n * @return {boolean}\n */\nvar validateBinaryTreeNodes = function(n, leftChild, rightChild) {\n const parentCount = new Array(n).fill(0);\n let rootCandidate = -1;\n\n for (let i = 0; i < n; i++) {\n if (leftChild[i] !== -1) parentCount[leftChild[i]]++;\n if (rightChild[i] !== -1) parentCount[rightChild[i]]++;\n }\n\n for (let i = 0; i < n; i++) {\n if (parentCount[i] === 0) {\n if (rootCandidate !== -1) return false;\n rootCandidate = i;\n }\n if (parentCount[i] > 1) return false;\n }\n\n if (rootCandidate === -1) return false;\n\n return countNodes(rootCandidate, new Set()) === n;\n\n function countNodes(node, visited) {\n if (node === -1) return 0;\n if (visited.has(node)) return -1;\n visited.add(node);\n const leftNodes = countNodes(leftChild[node], visited);\n const rightNodes = countNodes(rightChild[node], visited);\n if (leftNodes === -1 || rightNodes === -1) return -1;\n return 1 + leftNodes + rightNodes;\n }\n};","keywords":"LeetCode 1361, #1361 - Validate Binary Tree Nodes, Medium, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/validate-binary-tree-nodes |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/validate-binary-tree-nodes/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1361-validate-binary-tree-nodes.js |
| Depth-First Search | https://leetcodejavascript.com/tags/depth-first-search |
| Tree | https://leetcodejavascript.com/tags/tree |
| Breadth-First Search | https://leetcodejavascript.com/tags/breadth-first-search |
| Binary Tree | https://leetcodejavascript.com/tags/binary-tree |
| Graph | https://leetcodejavascript.com/tags/graph |
| Union Find | https://leetcodejavascript.com/tags/union-find |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow