(算法)Binary Tree Max Path Sum
题目:
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 ;
}
(算法)Binary Tree Max Path Sum的更多相关文章
- [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 ...
- 【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 ...
- 26. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 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 ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 【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 ...
随机推荐
- [Winform]缓存处理
摘要 在对winform做的项目优化的时候,首先想到的是对查询,并不经常变化的数据进行缓存,但对web项目来说有System.Web.Caching.Cache类进行缓存,那么winform端该如何呢 ...
- 下载企业级证书打包的app 出现“无法下载应用程序”的问题
问题描述:在下载企业级证书打包的app 出现“无法下载应用程序”的问题 解决办法:原来是生成plist文件时,设置url犯了一个致命的低级错误.如下
- WordPress主题开发:加载脚本和样式
如果只引入style.css,我把这个放头顶就可以了 <link rel="stylesheet" href="<?php echo get_styleshe ...
- Linux netstat命令具体解释
简单介绍 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表.接口状态 (Interface Statistics).masquerade 连接,多播成员 (Multicast Memb ...
- Java List集合冒泡法排序的两种实现
冒泡排序(Bubble Sort)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已 ...
- bat与jscript开发工具时遇到的一些问题
之前使得bat调用luac进行编译时,会弹出一个"黑色的界面",闪烁一下,感觉不太好.而脚本vbs或者jscript调用bat是可以利用Run方法,将其第二个参数设置为0便可以隐藏 ...
- centos7 开机启动管理
查看开机启动项 ls /etc/systemd/system/multi-user.target.wants/ 设置开机启动 systemctl enable mongodb.service
- 如何进行Logstash logstash-input-jdbc插件的离线安装
我们单位的服务器位于隔离区,不允许链接互联网,因此整理了在ELK集群上离线安装Logstash的jdbc input插件的方法,供大家参考. 总体思路是需要一台中转的机器,这台机器需要能够访问互联网, ...
- PHP检测及判断手机登录用户是安卓或爱疯(iPhone)客户端
<?php /* PHP 自动判断客户端平台(PC.安卓.iPhone.平板) * strtolower() 函数把字符串转换为小写: * strpos() 函数返回字符串在另一个字符串中第一次 ...
- Bias(偏差),Error(误差),和Variance(方差)的区别和联系
准: bias描述的是根据样本拟合出的模型的输出预测结果的期望与样本真实结果的差距,简单讲,就是在样本上拟合的好不好.要想在bias上表现好,low bias,就得复杂化模型,增加模型的参数,但这样容 ...