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