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 ...
随机推荐
- Android屏幕适应详解(二)
android应用自适应多分辨率的解决方法 1. 首先是建立多个layout文件夹(drawable也一样). 在res目录下建立多个layout文件夹,文件夹名称为layout-800x480等. ...
- Linux信号处理1
函数原型 NAME signal - ANSI C signal handling SYNOPSIS #include <signal.h> typedef void (*sighandl ...
- intelli IDEA node开发代码提示问题
好几天没写代码了,今天新建一个项目,在引入rs这个文件系统模块时却没有关于这个模块的代码提示,着实令人恶心啊.还好最终解决了. 在没有代码提示的时候点击如下图标: 出现如下的界面,其中有个Edit u ...
- 【poj1061-青蛙的约会】拓展欧几里得-不定方程
http://poj.org/problem?id=1061 题意:裸题.注意负数. //poj1061 #include<cstdio> #include<cstdlib> ...
- 多重背包问题II
多重背包问题II 总体积是m,每个小物品的体积是A[i] ,每个小物品的数量是B[i],每个小物品的价值是C[i] 求能够放入背包内的最大物品能够获得的最大价值 和上一个很类似 上一题体积就是价值,这 ...
- updmap-sys failed. Output has been stored in
Ubuntu 12.04升级到Ubuntu 12.04lts的时候,出现错误: Do you want to continue? [Y/n] ySetting up tex-common (4.04) ...
- http://blog.csdn.net/xiamizy/article/details/40781939
http://blog.csdn.net/xiamizy/article/details/40781939
- centos增加网卡
我做了一個傻事,要在Server上新增一張網卡,可是因為一直無法啟動, 所以很自然的以為CentOS 6又多了其他的設定要求, 因此查了兩天的資料,也試過很多方式,但都沒有效用. 今天早上心血來潮,想 ...
- Mybatis SqlSessionTemplate 源码解析
As you may already know, to use MyBatis with Spring you need at least an SqlSessionFactory and at le ...
- openstack配置注意事项(主要是网络相关)
vlan协议应该配置为802.1q,另一个容易混淆的为ISL,配置命令为 switchport trunk encapsulation dot1q /islswitchport mode trunk ...