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. eclipse进阶功法

    先选择要操作的行,在同时按shift+alt+a,会将所选中的文字括起来,鼠标会变成十字图标,按住鼠标左键,在相应输入文字的位置上下拖动,会出现一个竖杠,此时即可开始输入文字了,并且所选中行都有.

  2. 1024Studio官网

    一.开发背景 在工作室成立之后,一直就想为工作室建设一个网站,这次乘着暑假有足够的空余时间,开始着手建设我们1024studio的官方网站. 二.系统设计 1.系统目标 根据网上查找的相关资料以及与工 ...

  3. php利用PHPExcel类导出导入Excel用法

    PHPExcel类是php一个excel表格处理插件了,下面我来给大家介绍利用PHPExcel类来导入与导出excel表格的应用方法,有需要了解的朋友不防参考参考(PHPExcel自己百度下载这里不介 ...

  4. CSLA多语言设置

    1.在程序运行文件夹例如“\Bin\Debug\”中包含csla生成的资源文件: 2.在程序运行时,设置CSLA的当前语言为你想要的语言,例如:Csla.Properties.Resources.Cu ...

  5. Linux学习15_CentOS6.5下netcat工具安装教程

    1.下载 下载地址:http://sourceforge.net/projects/netcat/files/netcat/0.7.1/ 下载的是netcat-0.7.1.tar.gz版本 2.拷贝 ...

  6. vue.$refs 的用法

    官网给出的解释是: 被用来给元素或子组件注册引用信息.引用信息将会注册在父组件的 $refs 对象上. 1.如果在普通的 DOM 元素上使用,引用指向的就是 DOM 元素; 2.如果用在子组件上,引用 ...

  7. [洛谷P3810]【模板】三维偏序(陌上花开)

    题目大意:有$n$个元素,第$i$个元素有三个属性$a_i,b_i,c_i$,设$f(i)=\sum\limits_{i\not = j}[a_j\leqslant a_i,b_j\leqslant ...

  8. Welcome to Workrave

    Welcome to Workrave Workrave is a free program that assists in the recovery and prevention of Repeti ...

  9. SecureCRT指南

    本文主要介绍SecureCRT的使用方法和技巧. [概念解释]什么是SSH? SSH的英文全称是Secure Shell 传统的网络服务程序,如:FTP和telnet在本质上都是不安全的, 因为它们在 ...

  10. 【ZOJ4070】Function and Function(签到)

    题意:求 k 层嵌套的 f(x) 0<=x,k<=1e9 思路:迭代不会很多次后函数里就会=0或者1,再看层数奇偶直接返回答案 #include<cstdio> #includ ...