We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个 非空 二叉树,返回其最大路径和。
本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径 至少包含一个节点,且不一定经过根节点。
Input: [1,2,3] 1 / \ 2 3 Output: 6
Input: [-10,9,20,null,null,15,7] -10 / \ 9 20 / \ 15 7 Output: 42
The text was updated successfully, but these errors were encountered:
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */ /** * @param {TreeNode} root * @return {number} */ var maxPathSum = function(root) { let result = -Infinity; findMaxSinglePath(root); return result; function findMaxSinglePath(root) { if (root === null) { return 0; } const leftPath = findMaxSinglePath(root.left); const rightPath = findMaxSinglePath(root.right); result = Math.max(Math.max(leftPath, 0) + Math.max(rightPath, 0) + root.val, result); return Math.max(leftPath, rightPath, 0) + root.val; } };
/** * Definition for a binary tree node. * class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ function maxPathSum(root: TreeNode | null): number { let result = -Infinity; findMaxSinglePath(root); return result; function findMaxSinglePath(root: TreeNode | null): number { if (root === null) { return 0; } const leftPath = findMaxSinglePath(root.left); const rightPath = findMaxSinglePath(root.right); result = Math.max(Math.max(leftPath, 0) + Math.max(rightPath, 0) + root.val, result); return Math.max(leftPath, rightPath, 0) + root.val; } };
Sorry, something went wrong.
No branches or pull requests
124. Binary Tree Maximum Path Sum
给定一个 非空 二叉树,返回其最大路径和。
本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径 至少包含一个节点,且不一定经过根节点。
Example 1
Example 2
The text was updated successfully, but these errors were encountered: