Title: #1660 - Correct a Binary Tree - LeetCode JavaScript Solutions
Open Graph Title: #1660 - Correct a Binary Tree - LeetCode JavaScript Solutions
X Title: #1660 - Correct a Binary Tree - LeetCode JavaScript Solutions
Description: You have a binary tree with a small defect. There is exactly one invalid node where its right child incorrectly points to another node at th...
Open Graph Description: You have a binary tree with a small defect. There is exactly one invalid node where its right child incorrectly points to another node at th...
X Description: You have a binary tree with a small defect. There is exactly one invalid node where its right child incorrectly points to another node at th...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/correct-a-binary-tree
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/correct-a-binary-tree","name":"#1660 - Correct a Binary Tree - LeetCode JavaScript Solutions","description":"You have a binary tree with a small defect. There is exactly one invalid node where its right child incorrectly points to another node at th...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n * this.val = (val===undefined ? 0 : val)\n * this.left = (left===undefined ? null : left)\n * this.right = (right===undefined ? null : right)\n * }\n */\n/**\n * @param {TreeNode} root\n * @param {number} from\n * @param {number} to\n * @return {TreeNode}\n */\nvar correctBinaryTree = function(root) {\n const set = new Set();\n return traverse(root, null, false);\n\n function traverse(node, parent, isLeft) {\n if (!node) return null;\n\n if (node.right && set.has(node.right)) {\n if (parent) {\n if (isLeft) {\n parent.left = null;\n } else {\n parent.right = null;\n }\n }\n return null;\n }\n\n set.add(node);\n\n node.right = traverse(node.right, node, false);\n node.left = traverse(node.left, node, true);\n\n return node;\n }\n};","keywords":"LeetCode 1660, #1660 - Correct a Binary Tree, 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/correct-a-binary-tree |
| 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/correct-a-binary-tree/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1660-correct-a-binary-tree.js |
| Hash Table | https://leetcodejavascript.com/tags/hash-table |
| 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 |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow