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.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

 
用递归确定每一个节点作为root时,从root出发的最长的路径
在每一次递归中计算maxPath
 
 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxSum=INT_MIN;
int maxPathSum(TreeNode *root) { DFS(root);
return maxSum;
} int DFS(TreeNode *root)
{
if(root==NULL)
{
return ;
} int left=DFS(root->left);
int right=DFS(root->right); int sum=root->val;
if(left>) sum+=left;
if(right>) sum+=right;
if(maxSum<sum) maxSum=sum; return (left>||right>)?root->val+max(left,right):root->val;
}
};

【leetcode】Binary Tree Maximum Path Sum的更多相关文章

  1. 【leetcode】Binary Tree Maximum Path Sum (medium)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

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

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

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

  4. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

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

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

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

  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. 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. OC-类方法

    类方法 1. 基本概念 直接可以用类名来执行的方法(类本身会在内存中占据存储空间,里面有类\对象方法列表) 2. 类方法和对象方法对比 1)  对象方法 以减号-开头 只能让对象调用,没有对象,这个方 ...

  2. apache 的工作模式

    总结:访问量大的时候使用 worker模式:  每个进程,启动多个线程来处理请求,每个线程处理一次请求,对内存要求比较高. prefoek模式 : 每个子进程只有一个线程,一次请求一个进程. 什么是a ...

  3. .NET Framework源码查看及下载

    一.资源 1.http://referencesource.microsoft.com/ 二.备注 1.可在线预览.Net Framework 4.6.1源码实现

  4. Java Programming Test Question 4

    What will be the boolean flag value to reach the finally block? public class JPTQuestion4 { public s ...

  5. Linux服务器管理: 系统管理:进程文件信息lsof

    lsof命令 列出进程打开或使用的文件信息 [root@loclahost/]#lsof [选项] 选项: -c 字符串: 只列出以字符串开头的进程打开的文件 -u 用户名: 只列出某个用户的进程打开 ...

  6. unity资源管理

    Resources.Load(path); 每次执行都会真的去从硬盘加载资源,如果不希望这样做,那就保存第一次返回的引用,下次直接使用即可. Resources.UnloadAsset(obj); 该 ...

  7. 2015年12月12 Node.js实战(一)使用Express+MongoDB搭建多人博客

    序,Node是基于V8引擎的服务器端脚本语言. 基础准备 Node.js: Express:本文用的是3.21.2版本,目前最新版本为4.13.3,Express4和Express3还是有较大区别,可 ...

  8. MMTx使用说明

    MMTx(MetaMap Transfer)是美国国家医学图书馆建立的用于文本数据挖掘的一种工具. 下面以Medine格式数据为例介绍使用方法 1.在PubMed数据库检索相关的文献. 2.将数据结果 ...

  9. Java并发包源码学习之线程池(一)ThreadPoolExecutor源码分析

    Java中使用线程池技术一般都是使用Executors这个工厂类,它提供了非常简单方法来创建各种类型的线程池: public static ExecutorService newFixedThread ...

  10. PHP预编译处理技术简介

    1.提高数据库的效率:减少编译次数,减少连接次数.当出现当量操作sql语句,比如大量将数据插入数据库中,原来的那种单个执行sql语句或者批量执行sql语句的做法,显然是不可行的,因为无论是单个执行还是 ...