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. EF-Linq将查询结果转换为List<string>

    List<int> id_list = new List<int>() { 1 };//测试数据...List<string> guid_list = (from ...

  2. js 日期处理,json处理

    模块化js :requirejshttp://www.requirejs.cn/ 好用的日期控件:http://www.bootcss.com/p/bootstrap-datetimepicker/i ...

  3. MongoDB副本集学习(三):性能和优化相关

    Read Preferences/读写分离 有时候为了考虑应用程序的性能或响应性,为了提高读取操作的吞吐率,一个常见的措施就是进行读写分离,MongoDB副本集对读写分离的支持是通过Read Pref ...

  4. 使用原生JS封装Ajax

    使用原生 的JS封装 Ajax,实现 仿JQuery的Ajax,post,get三种异步请求方式: var MAjax = { //根据浏览器创建异步对象 createXhr: function () ...

  5. 安装 vue.js和第一个hello world

    一.在自己的项目文件中使用npm下载vue npm install vue 二.在文件中引入vue.js 三.第一个hello world 注:scritpt代码必须写在html代码的下面

  6. ajax异步提交数据动态更改select选项

    <!DOCTYPE html> <html> <head> <title></title> <script src="../ ...

  7. 手把手教你crontab排障

    导读 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,cr ...

  8. Mac之vim普通命令使用[转]

    高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx     开始记录宏,并将结果存入寄存器xq     退出记录模式@x     播放记录在x寄存器中的宏命 ...

  9. BZOJ 3270: 博物馆

    Sol 期望DP+高斯消元. 根据本题题意列出期望方程\[E(i,j)=(1-p_i)(1-p_j)E(u,v)+(1-p_i)p_jE(u,j)+p_i(1-p_j)E(i,v)+p_ip_jE(i ...

  10. InputStream,String相互转化

    String --> InputStream InputStream String2InputStream(String str){ ByteArrayInputStream stream = ...