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. JFinal 添加Druid插件

    第一步:添加依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</a ...

  2. windows 下开发的 .netCore MVC 部署到 Linux(Mint)

    这两天在公司跟同事偶然聊到 .netCore,说到一些趋势什么的.但是说来说去自己也没试过在Linux 机子上部署过,所以就试一下. 尝试之前也在网上看了一些文章,包括 Linux 上.netCore ...

  3. 在Linux下安装ArcGIS10.2

    最近由于工作需要,沉迷可视化无法自拔,一直在研究基于GIS的地图可视化,自己在本机windows搭建了一个ArcGIS服务器,用Tableau和R调用WMS服务成功,不愧是GIS元老级应用,效果超赞. ...

  4. ASP NET Core ---Automapper

    官方文档:http://docs.automapper.org/en/stable/index.html 一.安装和配置: 二.使用: 1.建立 Profile文件: public class Map ...

  5. HDU 4699 Editor(双向链表)

    双向链表直接模拟. 用一个辅助数组maxSum来维护一下前k项中[1,k]的最大和. 因为光标是一格一格的移动,所以每次光标右移的时候动态更新一下即可. 时间复杂度O(n). #include < ...

  6. string 与 byte[] 互转时的注意事项

    1.string 转 byte[] //为UTF8编码 byte[] midbytes=isoString.getBytes("UTF8"); //为ISO-8859-1编码,其中 ...

  7. 物联网第一次作业--我眼中的物联网——从认识RFID开始

    无线射频识别技术(Radio FrequencyIdentification,简称:RFID)是一种非接触式的自动识别技术,其基本原理是利用射频信号和空间耦合(电感或电磁耦合)或雷达反射的传输特性,实 ...

  8. 【bzoj2694】Lcm 莫比乌斯反演+线性筛

    题目描述 求$\sum\limits_{i=1}^n\sum\limits_{j=1}^m|\mu(gcd(i,j))|lcm(i,j)$,即$gcd(i,j)$不存在平方因子的$lcm(i,j)$之 ...

  9. xpath属性值的模糊匹配

    得至:http://bbs.csdn.net/topics/390857942  最后一楼 //div[contains(@class,'Number Skill')]

  10. [poj] 2618 popular cows

    原题 这是一个强连通分量板子题. a thinks b is popular 即为a到b有一条边,要求被所有牛popular的牛的个数. 所求为对图进行强连通分量缩点后,没有出度的强连通分量里的点数( ...