题目:

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.

思路:

递归

代码:

#include<iostream>
#include<stdlib.h> using namespace std; struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
}; int maxPathSum(TreeNode *root,int &maxDist){
if(root==NULL)
return ; int lmax=maxPathSum(root->left,maxDist);
int rmax=maxPathSum(root->right,maxDist); if(lmax+rmax+root->val>maxDist)
maxDist=lmax+rmax+root->val; return max(,root->val+max(lmax,rmax));
} int main(){ return ;
}

最新文章

  1. 【无私分享:ASP.NET CORE 项目实战】目录索引
  2. iOS 疑难杂症 — — 推送本地国际化 loc-key 本地化失败的问题
  3. 在C#中,不安装Oracle客户端如何连接Oracle数据库
  4. nginx缓存模块配置总结proxy_cache(未完)
  5. 怎样用foreach去修改数组之中的数据
  6. ifstat-网络接口监测工具
  7. MySQL不同库名相同表结构实现主从配置
  8. Qt多国语言
  9. C#下解决DrawImage画出来的Image变大了的问题
  10. C++中如何split字符串(转)
  11. UIButton图片文字控件位置自定义(图片居右文字居左、图片居中文字居中、图片居左文字消失等)
  12. 201521123053《Java程序设计》第十一周学习总结
  13. DEBUG经历
  14. java发送http get请求的两种方式
  15. Java网络编程中异步编程的理解
  16. centOS 安装gitlab-runner
  17. HTML5 元素拖拽实现 及 jquery.event.drag插件
  18. SpringMVC中ModelAndView对象与“视图解析器”
  19. React项目中使用Mobx状态管理(二)
  20. 基于bootstrap的后台左侧导航菜单和点击二级菜单刷新二级页面时候菜单展开显示当前菜单

热门文章

  1. PHP 依赖注入(DI) 和 控制反转(IoC)
  2. Unity3D实践系列08, MonoBehaviour类的各种触发事件
  3. 写一个限制上传文件大小和格式的jQuery插件
  4. SpringMVC和Springboot的区别
  5. Tomcat 负载均衡 及Session共享
  6. Python index()方法
  7. Android之找回打包key密码的方法
  8. Java Callable接口、Runable接口、Future接口
  9. PHP xhprof性能优化
  10. .Net-using-Class:String 类