Title: #124 - Binary Tree Maximum Path Sum - LeetCode JavaScript Solutions
Open Graph Title: #124 - Binary Tree Maximum Path Sum - LeetCode JavaScript Solutions
X Title: #124 - Binary Tree Maximum Path Sum - LeetCode JavaScript Solutions
Description: A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can onl...
Open Graph Description: A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can onl...
X Description: A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can onl...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/binary-tree-maximum-path-sum
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/binary-tree-maximum-path-sum","name":"#124 - Binary Tree Maximum Path Sum - LeetCode JavaScript Solutions","description":"A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can onl...","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 * @return {number}\n */\nvar maxPathSum = function(root) {\n let result = -Infinity;\n\n function traverse(node) {\n if (!node) return 0;\n const leftValue = Math.max(traverse(node.left), 0);\n const rightValue = Math.max(traverse(node.right), 0);\n result = Math.max(result, node.val + leftValue + rightValue);\n return node.val + Math.max(leftValue, rightValue);\n }\n\n traverse(root);\n\n return result;\n};","keywords":"LeetCode 124, #124 - Binary Tree Maximum Path Sum, Hard, 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/binary-tree-maximum-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/binary-tree-maximum-path-sum/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/0124-binary-tree-maximum-path-sum.js |
| Dynamic Programming | https://leetcodejavascript.com/tags/dynamic-programming |
| Depth-First Search | https://leetcodejavascript.com/tags/depth-first-search |
| Tree | https://leetcodejavascript.com/tags/tree |
| Binary Tree | https://leetcodejavascript.com/tags/binary-tree |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width,initial-scale=1
Robots: index, follow