Given a binary tree, find the maximum path sum.

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 does not need to go through the root.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

思路:所求的maximum path sum有可能会包括一个根节点和两条分别在左右子树中的路径,这里构建一个新的树,每个节点存储的值为所给二叉树对应位置节点向下的最大单条路径(不会在根节点分叉)。然后遍历整个树,找到maximum path sum。

代码中copyTree函数为构建树。遍历树并找到maximum path sum在help函数中实现。

 class Solution {
public:
TreeNode* copyTree(TreeNode* root)
{
if (!root) return NULL;
TreeNode* cur = new TreeNode(root->val);
cur->left = copyTree(root->left);
cur->right = copyTree(root->right);
int largest = ;
if (cur->left) largest = max(largest, cur->left->val);
if (cur->right) largest = max(largest, cur->right->val);
cur->val += largest;
return cur;
}
void help(TreeNode* root, TreeNode* cproot, int& res)
{
if (!root) return;
int localSum = root->val;
if (cproot->left && cproot->left->val > )
localSum += cproot->left->val;
if (cproot->right && cproot->right->val > )
localSum += cproot->right->val;
if (localSum > res) res = localSum;
help(root->left, cproot->left, res);
help(root->right, cproot->right, res);
}
int maxPathSum(TreeNode* root) {
TreeNode* cproot = copyTree(root);
int res = INT_MIN;
help(root, cproot, res);
return res;
}
};

Binary Tree Maximum Path Sum - LeetCode的更多相关文章

  1. Binary Tree Maximum Path Sum leetcode java

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

  2. Binary Tree Maximum Path Sum [leetcode] dp

    a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->righ ...

  3. [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 ...

  4. 【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 ...

  5. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  6. LeetCode: Binary Tree Maximum Path Sum 解题报告

    Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...

  7. 【LeetCode】124. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  8. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

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

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

随机推荐

  1. Leetcode 659.分割数组为连续子序列

    分割数组为连续子序列 输入一个按升序排序的整数数组(可能包含重复数字),你需要将它们分割成几个子序列,其中每个子序列至少包含三个连续整数.返回你是否能做出这样的分割? 示例 1: 输入: [1,2,3 ...

  2. tomcat集群和负载均衡的实现(session同步)

      (一)环境说明 (1)服务器有4台,一台安装apache,三台安装tomcat (2)apache2.0.55.tomcat5.5.15.jk2.0.4.jdk1.5.6或jdk1.4.2 (3) ...

  3. SPOJ 364 Pocket Money 简单DP

    跟矩阵链乘同类型的题…… 输出用%llu不是%I64u…… 几组数据: 141+2*4+3*4+5*00*5*6+7*3+23+0+6+7+0+44*5+7*1*1+12*0+3*4*0+5*6+7+ ...

  4. 搭建springmvc项目404,没扫描到包

    搭建简单项目完成之后,曾经出现过一个问题 跳转报了404,控制台忘了没留啊... 反正意思就是说我配置有问题,导致没有扫描到注释的类 <context:component-scan base-p ...

  5. 详解Linux运维工程师应具备的十大技能

    Linux系统如果是学习可以选用Redhat或CentOS,特别是CentOS在企业中用得最多,当然还会有其它版本的,但学习者还是以这2个版本学习就行,因为这两个版本都是兄弟,没区别的,有空可以再研究 ...

  6. nginx限速白名单配置

    在<nginx限制连接数ngx_http_limit_conn_module模块>和<nginx限制请求数ngx_http_limit_req_module模块>中会对所有的I ...

  7. [poj] 2286 The Rotation Game || ID-DFS

    原题 有1234四个数字,每个数字八个.有八种方向的移动,使得操作后中间八个方块的数字相同,求最小操作步数. 对于这种求最小步数的看起来就是dfs的题,就ID-DFS就好了. //不知道为什么都是ID ...

  8. hdu 3717 二分+队列维护

    思路:已知当前的总长度和为len,当前的伤害为sum,伤害次数为 num.那么对下一个点的伤害值sum=sum+2*len+num: 这个是通过(x+1)^2展开化简就能得到. #include< ...

  9. [解决方案]未能找到路径“~\bin\roslyn\csc.exe”的一部分

    我的WebApi项目使用Nuget加载了一些包以后出现了这样的问题,本地可以访问,但发布到线上后,出现这样的报错 这个问题出现的原因是Nuget的时候,多加载了一些项目可能不需要的依赖库所导致的. 解 ...

  10. SICAU-OJ: 数字游戏

    数字游戏 题意:给出一个长度为n的数字,然后抹去k个数,使得剩下的数最大. 题解: 贪心的思想:让答案串中每一位尽可能大. 我们肯定要用完这k次的,假设有一个答案字符串ans,我们现在遍历给出的串,假 ...