Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

找树的最大路径和 注意路径可以从任意点起始和结束。

我发现我真的还挺擅长树的题目的,递归不难。就是因为有个需要比较的量(最大和),所以需要再写一个函数。

因为路径可以从任意点起始和结束,所以每次递归的时候左右子树小于等于0的就可以不管了。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; //Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int maxPathSum(TreeNode *root){
if(root == NULL)
{
return ;
}
int MaxPathSum = root->val; //赋的初值一定要小于等于最后的值
maxPathSumCur(root, MaxPathSum);
return MaxPathSum;
}
int maxPathSumCur(TreeNode *root, int& MaxPathSum) {
if(root == NULL)
{
return ;
} int lsum = maxPathSumCur(root->left, MaxPathSum);
int rsum = maxPathSumCur(root->right, MaxPathSum);
int maxPathSumCurrent = root->val; //每次根的值一定要加上 左右子树的就加大于0的
if(lsum > )
{
maxPathSumCurrent += lsum;
}
if(rsum > )
{
maxPathSumCurrent += rsum;
} MaxPathSum = max(maxPathSumCurrent, MaxPathSum);
return max(root->val, max(root->val + lsum, root->val +rsum)); //返回时返回根 节点加左 或右子树 或单独根节点中最大的
}
void create(TreeNode *& root)
{
int d;
scanf("%d", &d);
if(d != )
{
root = new TreeNode(d);
create(root->left);
create(root->right);
}
}
}; int main()
{
Solution s;
TreeNode * T = NULL;
s.create(T);
int sum = s.maxPathSum(T); return ;
}

【leetcode】Binary Tree Maximum Path Sum (medium)的更多相关文章

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

  2. 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 ...

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

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

  4. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

  5. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  6. [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  7. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  8. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

    题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...

  9. leetcode 124. Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

随机推荐

  1. C# Winform 脱离 Framework (一)

    Linker是一个命令行工具,它以将我们的.net程序生成可脱离.net framework环境运行的程序 . Linker不支持中文的路径,在程序中也不能有中文的标识符. Linker 有2种部署方 ...

  2. eclipse emacs

    eclipse emacs 插件 http://www.mulgasoft.com/emacsplus eclipse字体设置: 一.把字体设置为Courier New  操作步骤:打开Elcipse ...

  3. WordPress文章浏览历史插件

    选自:http://www.ludou.org/wordpress-recently-viewed.html 最近有很多网友问我,露兜博客右边栏底部的 您刚刚看过 栏目是怎么实现.其实我也是参考的这篇 ...

  4. HDU 1174 爆头(计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1174 解题报告:就是用到了三维向量的点积来求点到直线的距离,向量(x1,y1,z1)与(x2,y2,z ...

  5. const 和宏的区别

    参考:http://blog.sina.com.cn/s/blog_79b01f6601018xdg.html (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行 ...

  6. linux定时执行脚本

    阅读目录 1. cron服务[Ubuntu环境] 2. crontab用法 3. 编辑crontab文件 4. 流程举例 5. 几个例子 Linux中,周期执行的任务一般由cron这个守护进程来处理. ...

  7. Codeforces Gym 101138 D. Strange Queries

    Description 给你一下长度为 \(n\) 的序列. \(a_i=a_j\) \(l_1 \leqslant i \leqslant r_1\) \(l_2 \leqslant i \leqs ...

  8. uploadify插件的功能应用

    一.相关key值介绍 uploader:uploadify.swf文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击后淡出打开文件对话框,默认值:uploadify.swf. scri ...

  9. php类与对象

    1.类与对象 对象:实际存在该类事物中每个实物的个体.$a =new User(); 实例化后的$a 引用:php的别名,两个不同的变量名字指向相同的内容 封装: 把对象的属性和方法组织在一个类(逻辑 ...

  10. 有向图寻找(一个)奇环 -- find an oddcycle in directed graph

    /// the original blog is http://www.cnblogs.com/tmzbot/p/5579020.html , automatic crawling without l ...