/**
* 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. PTA 7-2 列车调度(25 分)

    7-2 列车调度(25 分) 火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择任意一条轨道 ...

  2. Ural 1297 Palindrome(后缀数组+最长回文子串)

    https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...

  3. bean标签常用属性

    scope属性: singleton:只有一个 prototpye:每次创建都是新的 对象初始化方法: init-method 对象销毁方法: destroy-method

  4. ImgNoGoodWindow

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using UnityEditor; ...

  5. P4001 [BJOI2006]狼抓兔子

    传送门 思路: 不少题解都是用网络流来做最小割(网络流是什么),但对于一个不会网络流的蒟蒻来做这题相当困难. 听机房daolao说可以重构图做最短路.然后就baidu将平面图转换成一个对偶图,因为网络 ...

  6. springboot学习之授权Spring Security

    SpringSecurity核心功能:认证.授权.攻击防护(防止伪造身份) 涉及的依赖如下: <dependency> <groupId>org.springframework ...

  7. STL_map.修改删除

    1.修改示例 int TdrSvgAttr::AttrSet_mem(bool _bAttrInStyle, string &_strAttrName, string& _strAtt ...

  8. jQuery 的 prevObject

    1. prevObject jquery选择器在遍历的过程中都会找到一组元素(一个jQuery对象),然后jQuery会把这组元素推入到栈中.prevObject属性就指向这个对象栈中的前一个对象,而 ...

  9. VNC安装配置

    1,安装VNC yum install tigervnc-server -y2,设定密码: vncpasswd root 3,配置服务文件,下面是配置了6个窗口. cp /lib/systemd/sy ...

  10. Linux性能测试工具-UnixBench

    ■下载路径: unixbench-5.1.2.tar.gz :http://soft.vpser.net/test/unixbench/ unixbench-5.1.3.tar.gz :http:// ...