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. mac安装虚拟机VirtualBox,并在虚拟机上安装centos

    1. 首先从网页上https://www.virtualbox.org/wiki/Downloads下载VirtualBox-6.0.0-127566-OSXdmg文件.我一般把下载的文件放到/opt ...

  2. Python学习-day20 django进阶篇

    Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...

  3. Oz代码梳理

    https://files.cnblogs.com/files/gushiren/oz%E6%B5%81%E7%A8%8B%E5%9B%BE.pdf https://files.cnblogs.com ...

  4. Eclipse下使用SVN插件从服务器获取工程

    1.打开Eclipse 4.4,在Eclipse里打开SVN资源库窗口:点击菜单window-->show view-->other:然后再弹出窗口展开SVN节点,选择SVN资源库   2 ...

  5. stuff使用感悟

    select ),t2.CityId) from t t2 where not exists( from Web_UserCity uc where UserName='user001' and uc ...

  6. 【bzoj2879】[Noi2012]美食节 费用流+动态加边

    原文地址:http://www.cnblogs.com/GXZlegend 题目描述 CZ市为了欢迎全国各地的同学,特地举办了一场盛大的美食节.作为一个喜欢尝鲜的美食客,小M自然不愿意错过这场盛宴.他 ...

  7. Ubuntu扩展系统盘容量,虚拟机下

    安装gparted软件 sudo apt-get install gparted 接下来, 我们开始用Gparted软件扩展Ubuntu目录的容量: 先看操作步骤: 1. 先从windows的 ntf ...

  8. webpack配置优化

    1.使用alias简化路径 alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src') } 2.overlay界面弹出编译错误 devSer ...

  9. Codeforces Round #356 (Div. 2) C

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  10. String中的“equal方法”和“==”

    二话不说,先来说下重写的事情: 在Java中,String .Math.还有Integer.Double....等这些封装类重写了Object中的equals()方法,让它不再比较其对象在内存中的地址 ...