/**
* 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 *的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. [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 ...

  6. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  7. 图解leetcode —— 124. 二叉树中的最大路径和

    前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到 ...

  8. 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 ...

  9. Java实现 LeetCode 124 二叉树中的最大路径和

    124. 二叉树中的最大路径和 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: ...

  10. 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 ...

随机推荐

  1. SHOI 2017 相逢是问候(扩展欧拉定理+线段树)

    题意 https://loj.ac/problem/2142 思路 一个数如果要作为指数,那么它不能直接对模数取模,这是常识: 诸如 \(c^{c^{c^{c..}}}\) 的函数递增飞快,不是高精度 ...

  2. Images之base image

    Create a base image Most Dockerfiles start from a parent image. If you need to completely control th ...

  3. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  4. 7、nginx的upstream及fastcgi模块应用

    ngx_http_proxy_module, ngx_http_upstream_module   ngx_http_proxy_module:实现反向代理及缓存功能 proxy_pass http: ...

  5. python接口测试模版

    """Test case implementation""" import sys import functools import diff ...

  6. android 利用CountDownTimer实现时分秒倒计时效果

    https://blog.csdn.net/mrzhao_perfectcode/article/details/81289578

  7. Ajax_请求get,post案例

    1. 最原始的ajax请求方式 (1). get请求 <%@ Page Language="C#" AutoEventWireup="true" Code ...

  8. eclipse中启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误

    原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...

  9. bitbucket工程改名导致 repository does not exist. fatal: Could not read from remote repository.

    在bitbucket上把工程改名了,就忘了. 结果同步时报错. 先在本地查看一下 git remote -v 果然是工程的老名字 origin git@bitbucket.org:XXX/oldnam ...

  10. java高并发解决方案

    高并发的解决方法有两种: 1.使用缓存 2.使用生成静态页面: (代码质量,不要性能低下的sql和代码.有的一条sql搞定的事,有人用了多个循环才能搞定.取决于程序员的经验!(还有就是从最基础的地方优 ...