Leetcode 124 *
/**
* 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) {
int res = INT_MIN;
DFS(root,res);
return res;
}
int DFS(TreeNode* root,int &res){
if(!root) return ;
int left = max(DFS(root->left,res),);
int right = max(DFS(root->right,res),);
res = max(res,left+right+root->val);
return max(left,right)+root->val; //res是整个树的最大值,return 的是作为单节点的最大值,不矛盾
}
};
_
Leetcode 124 *的更多相关文章
- [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@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- 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] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- [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 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 图解leetcode —— 124. 二叉树中的最大路径和
前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到 ...
- 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 ...
- Java实现 LeetCode 124 二叉树中的最大路径和
124. 二叉树中的最大路径和 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
随机推荐
- (转载)http和socket之长连接和短连接区别
TCP/IPTCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议.在传输层中有TCP协议与UDP协议.在应用层有: ...
- Js批量下载花瓣网及堆糖网专辑图片
插件作者:SaintIC 文章地址:https://blog.saintic.com/blog/256.html 一.安装 1. 安装Tampermonkey扩展,不同浏览器的支持,参见官网:http ...
- 【ASP.Net】 web api中的media type
1. 有什么用? 通常用来标识http请求中的内容的类型用来告诉server端如何解析client端发送的message, 或者标识client希望从server端得到的资源是什么样的类型.又被称为M ...
- Spring线程池
•依赖 spring-context-support •配置: <bean id ="taskExecutor" class ="org.springframewo ...
- Java处理微信公众号文章图片不显示微信
http://blog.csdn.net/just4you/article/details/52933620
- _instance_reset
制作多功能Item.creature及gameobject 中第19个功能 重置副本,关联到该表. 当该表中配置,且玩家有这个副本这个难度的进度时,功能宝石中会出现该副本的名字,点击可以重置副本 ma ...
- _talent_req
学习天赋时,将产生消耗,当玩家已经学习过该天赋时,不产生消耗 comment 备注 spellId 天赋技能ID reqId 消耗模板ID,对应_req表中reqId
- 如何对接oracle 建立pdb
Oracle数据库的结构是一个数据库实例下有许多用户,每一个用户有自己的表空间,即每一个用户相当于MySQL中的一个数据库.不久前下了oracle 12c的数据库,安装之后建user时才知道oracl ...
- String,StringBuilder区别,一个是常量,一个是可变量
String str="这就是爱的呼唤,这就是爱的奉献!!"; //这个str是不可变的字符串序列,要变会生成新的字符串,原字符串不变,是常量 StringBuilder sBui ...
- 写给前端的Python依赖管理指北
概述 在Python的项目中,我们可以通过pip来安装依赖包,但是不像npm install,pip默认安装的依赖包会挂在全局上,不利于项目工程协作. 这时候需要一款类似npm的工具记录我们的项目依赖 ...