Title: #112 - Path Sum - LeetCode JavaScript Solutions
Open Graph Title: #112 - Path Sum - LeetCode JavaScript Solutions
X Title: #112 - Path Sum - LeetCode JavaScript Solutions
Description: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values...
Open Graph Description: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values...
X Description: Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/path-sum
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/path-sum","name":"#112 - Path Sum - LeetCode JavaScript Solutions","description":"Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values...","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} targetSum\n * @return {boolean}\n */\nvar hasPathSum = function(root, targetSum) {\n if (!root) {\n return false;\n }\n const result = [];\n\n traverse(result, root);\n\n return result.includes(targetSum);\n};\n\nfunction traverse(result, node, sum = 0) {\n if (!node.left && !node.right) {\n result.push(sum + node.val);\n }\n\n if (node.left) traverse(result, node.left, sum + node.val);\n if (node.right) traverse(result, node.right, sum + node.val);\n}","keywords":"LeetCode 112, #112 - Path Sum, Easy, 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/path-sum |
| 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/path-sum/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0112-path-sum.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 |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow