LeetCode124 Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. (Hard)

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.

For example:
Given the below binary tree,

       1
      /      2   3

Return 6.

分析:

问题是从树种任意点到任意点的最大path sum,且至少有一个点在路径上。

先考虑一个简单的问题,从顶点到任意点的最短路径(可以不包含任何点,即为0)

这样可以用一个简单的递归实现:

int singleSum(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        return max(0,root -> val + max(singleSum(root -> left), singleSum(root -> right)));
}

再考虑pathSum与singleSum的关系;

pathSum采用分治的思想,可以求左子树的pathSum和右子树的pathSum,最后剩下的部分就是跨越根节点的pathSum

实际上也就是root->val加上左右子树的singlePath之和(如果小于0就不要了)。

程序:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11     int singleSum(TreeNode* root) {
12         if (root == nullptr) {
13             return 0;
14         }
15         return max(0,root -> val + max(singleSum(root -> left), singleSum(root -> right)));
16     }
17 public:
18     int maxPathSum(TreeNode* root) {
19         if (root == nullptr) {
20             return -0x7FFFFFFF;
21         }
22         int leftSum = maxPathSum(root -> left);
23         int rightSum = maxPathSum(root -> right);
24         int midSum = root -> val + max(0, (singleSum(root -> left) + singleSum(root -> right)));
25         return max(midSum,max(leftSum, rightSum));
26     }
27 };

但是一组大样例居然超时了,考虑一下重复计算确实不少,给singleSum加一个hash表即可。

代码:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11     unordered_map<TreeNode* ,int> hash;
12     int singleSum(TreeNode* root) {
13         if (root == nullptr) {
14             return 0;
15         }
16         if (hash.find(root) != hash.end()) {
17             return hash[root];
18         }
19         int result = max(0,root -> val + max(singleSum(root -> left), singleSum(root -> right)));
20         hash[root] = result;
21         return result;
22     }
23 public:
24     int maxPathSum(TreeNode* root) {
25         if (root == nullptr) {
26             return -0x7FFFFFFF;
27         }
28         int leftSum = maxPathSum(root -> left);
29         int rightSum = maxPathSum(root -> right);
30         int midSum = root -> val + max(0, (singleSum(root -> left) + singleSum(root -> right)));
31         return max(midSum,max(leftSum, rightSum));
32     }
33 };
时间: 2024-08-06 07:56:29

LeetCode124 Binary Tree Maximum Path Sum的相关文章

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

Binary Tree Maximum Path Sum 自底向上求解(重重重)

题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,如果左右子树最大路径和小于0,那么返回零, 用这个最大路径和和根节点的值相加,来更新最大值,同时, 更新返回该树的最大路径值. 代码: class Solution { public: int max = INT_MIN; int maxPathSum(TreeNode *root) { if (root == NULL) return 0; search(root); return max;

第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

[LeetCode]Binary Tree Maximum Path Sum

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /******

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get m

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

【leetcode】Binary Tree Maximum Path Sum

Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 用递归确定每一个节点作为root时,从root出发的最长的路径 在每一次递归中计算maxPath 1 /** 2 * Def

[LeetCode] Binary Tree Maximum Path Sum(递归)

Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

[Leetcode][Tree][Binary Tree Maximum Path Sum]

找书中权值和最大的路径,至少包含一个点. 有点类似LCA(最近公共祖先),树的问题关键是如何划分子问题,然后递归求解. 想到了可以返回两种结果,一个是单独路径的最大和,一种是子树的最大和,然后在求解的过程中不断的更新答案. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val