Given a binary tree, find the maximum path sum.

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

For example: Given the below binary tree,

   1
/ \
2 3

Return6.

C++

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
int maxDis;
public:
int maxPathSum(TreeNode *root) {
if(!root){
maxDis=0;
return -1e8;
}
int leftMax=maxPathSum(root->left);
int l=max(0,maxDis);
int rightMax=maxPathSum(root->right);
int r=max(0,maxDis);
maxDis=max(l,r)+root->val;
return max(leftMax,max(rightMax,l+r+root->val));
}
};

最新文章

  1. JS 前端格式化JSON字符串工具
  2. hdu.1111.Secret Code(dfs + 秦九韶算法)
  3. IBindCtx接口定义
  4. java语言中数值自动转换的优先顺序
  5. shell script 的追踪与 debug
  6. Anti-Grain Geometry 概述
  7. Oracle GoldenGate中HANDLECOLLISIONS参数使用详解
  8. 怎么选择公司???MVC加jquery-easyui 后端工程师
  9. SpringBoot整合Swagger2,再也不用维护接口文档了!
  10. 总结,为什么要重写hashset的hashcode()和equals()?
  11. exec 命令简单用法 和 find 搭配用法示例
  12. B. Switches and Lamps
  13. bat 命令 常用配置及其用法
  14. [LintCode] Permutations
  15. dbms_stats.gather_table_stats详解
  16. 【MVC】Controller的使用
  17. unity灯光烘焙设置详解
  18. ubuntu 关闭和开启防火墙
  19. 关于webWorker的理解和简单例子
  20. CSDN日报20170401 ——《假设你还是“程序猿”,我劝你别创业!》

热门文章

  1. 前端框架VUE——数据绑定及模板语法
  2. VBox 虚拟机安装 Openwrt 做旁路由
  3. 查看Win10商店应用更新日期
  4. webpack 安装与卸载
  5. php 扫描url死链接
  6. postman 插件安装
  7. 重新嫁接rm命令
  8. AT1983-[AGC001E]BBQ Hard【dp,组合数学】
  9. WPF进阶技巧和实战06-控件模板
  10. python paramiko实现ssh上传下载执行命令