https://leetcode.com/problems/binary-tree-maximum-path-sum/

Given a binary tree, find the maximum path sum.

For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

/**
* 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 res = INT_MIN; int dfs(TreeNode* root) {
if(!root) return ; int l = dfs(root->left);
int r = dfs(root->right); int rhs = root->val;
if(l > ) rhs += l;
if(r > ) rhs += r;
res = max(res, rhs); return max(l, r) <= ? root->val: max(l, r) + root->val;
} int maxPathSum(TreeNode* root) {
if(!root) return ; dfs(root); return res;
}
};

leetcode@ [124] Binary Tree Maximum Path Sum (DFS)的更多相关文章

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

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

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

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

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

  5. Java for LeetCode 124 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. ...

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

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

  8. leetcode 124. Binary Tree Maximum Path Sum ----- java

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  9. 【LeetCode】124. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

随机推荐

  1. Jquery-DataTable 使用介绍

    http://dt.thxopen.com/example/server_side/simple.html

  2. codeforces #305 D Mike and Fish

    正解貌似是大暴搜? 首先我们考虑这是一个二分图,建立网络流模型后很容易得出一个算法 S->行 容量为Num[X]/2; 行->列 容量为1 且要求(x,y)这个点存在 列->T 容量 ...

  3. JS插件excanvas的使用方法

     这个还没有想好怎么写,等写好后再发布 试用了excanvas.js,生成静态统计图 IE下使用excanvas.js的注意事项

  4. php简单文件上传类

    <?php header("Content-Type:text/html; charset=utf-8"); if($_POST['submit']){ $upfiles = ...

  5. 退出myeclipse 8.5配置中心

    用myeclipse 8.5没多久,进入软件中心下载插件,找不到退出按钮,唉,木想到就是一图标.

  6. Python学习笔记一--字符串的使用

    一.基本操作 1. 合并字符串:“+” 2. 打印重复的字符串:"*"      3. 按位获取字符串中的字符:索引      4. 按位获取字符串中的子字符串:分片      5 ...

  7. Smallest unused ID

    http://www.codewars.com/kata/smallest-unused-id Description: Hey awesome programmer! You've got much ...

  8. 大四实习准备4_java内部类

    2015-4-30 [昨天又可耻地休息了一天,懒劲比较大啊.跟懒劲有直接关系,这几天对幸福的感知也黯淡了,对未来的幸福不是那么渴望了.表现在对亲情和爱情上. 我想生活的本意是积极进取.茁壮生长并时常感 ...

  9. poj2436,poj3659,poj2430

    这两题都体现了dp的核心:状态 dp做多就发现,状态一设计出来,后面的什么都迎刃而解了(当然需要优化的还要动动脑筋): 先说比较简单的: poj2436 由题得知病毒种数<=15很小,于是我们就 ...

  10. 在Visual Studio 2010中使用DSL Tool特定领域开发 开篇

    本来是很想写关于VS的DSL的文章的,有点小忙,就一直在拖延,忽然有看见了"<在Visual Studio 2012中使用VMSDK开发特定领域语言>",又有写的欲望了 ...