LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和。(点权有负的。)
思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值。要注意如果子树传来的如果是负值,是可以同时丢弃的,但至少要将当前节点的val更新答案。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode* root) {
ans=-;
DFS(root);
return ans;
}
private:
int ans;
int DFS(TreeNode* t) {
if(!t) return ;
int a=max(,DFS(t->left)); //小于0的直接丢弃
int b=max(,DFS(t->right));
ans=max(ans, a+b+t->val); //更新,必须以当前节点为中转
return max(a, b)+t->val; //只能返回一条到叶子的路径
}
};
AC代码
LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)的更多相关文章
- [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 ...
- [LeetCode] Binary Tree Maximum Path Sum(最大路径和)
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 二叉树系列 - 二叉树里的最长路径 例 [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]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 SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 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 ...
- [Leetcode] Binary tree maximum path sum求二叉树最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [leetcode]Binary Tree Maximum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...
随机推荐
- C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\Team Foundation\4.0\Cache\VersionControl.config is not valid and cannot be loaded.
Recently, we experienced a strange problem with TFS 2010. We spent a few days before we figured it o ...
- POJ 2724
Purifying Machine Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4014 Accepted: 1127 ...
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- 2014多校第五场1010 || HDU 4920 Matrix multiplication(矩阵乘法优化)
题目链接 题意 : 给你两个n*n的矩阵,然后两个相乘得出结果是多少. 思路 :一开始因为知道会超时所以没敢用最普通的方法做,所以一直在想要怎么处理,没想到鹏哥告诉我们后台数据是随机跑的,所以极端数据 ...
- 利用securecrt在linux与windows之间传输文件
SecureCRT这款SSH客户端软件同时具备了终端仿真器和文件传输功能.比ftp命令方便多了,而且服务器不用再开FTP服务了.rz,sz是便是Linux/Unix同Windows进行ZModem文件 ...
- Tomcat SSL 设置
1. 先用如下命令生成tomcat 证书 cls rem please set the env JAVA_HOME before run this bat file SET JAVA_HOME=C:\ ...
- 【转】terminal 快捷键
转自:http://www.jb51.net/os/Ubuntu/141723.html 1.gnome-terminal快捷键设置方法: 系统 —> 首选项 ->键盘快捷键 -> ...
- Docker搭建MySQL服务
Docker开源镜像 前面我们已经安装好了Docker,也简单了解了Docker.那么我们可以尝试搭建一个MySQL服务. 要搭建服务就要启动服务容器,要创建容易就要有镜像,Docker提供了一个类似 ...
- 304. Range Sum Query 2D - Immutable
题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...
- iOS:实现MKAnnotation协议,在地图上设置大头针,点击显示具体的位置信息
如何添加大头针(地标): 通过MapView的addAnnotation方法可以添加一个大头针到地图上 通过MapView的addAnnotations方法可以添加多个大头针到地图上 –(void)a ...