Given a non-empty 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 must contain at least one node and does not need to go through the root.

Example 1:

Input: [1,2,3]

       1
/ \
2 3 Output: 6

Example 2:

Input: [-10,9,20,null,null,15,7]

   -10
   / \
  9  20
    /  \
   15   7 Output: 42

思路

dfs:

2

/       \

1         -3

maxPath:  1 + 2 + 0

1. use maxPath to update result from all paths which is max path sum

2. divide a path into 3 parts:

a. left path: from root to left side down to 0 or more steps

b. right path: from root to right side down to 0 or more steps

c. cur root itself

3. maxPath = c + (a>0 ? a: 0 ) + (b > 0 ? b:0)

这是一道关于BST和recursion的经典题,需要掌握

最naive的想法是找到所有BST的path,返回max

发现, 任意一条path都有一个顶点(位置最高点)

我们用这个顶点来分解所有path

这样,以任意一个点为顶点的path就分解为

a. max_sum (左边path)

b. max_sum (右边path)

c. 顶点自己的value

进一步,

a + b + c 组成的人字形path的max path sum

     2
/ \
1 -3

dfs的return value :  2(顶点自己的value必须加上,无论正负) +  1 (正数贡献自己) + 0 (-3为负数不做贡献就是及时止损了)  = 3

跟 [leetcode]543. Diameter of Binary Tree二叉树直径 的思路基本一致。

代码

 class Solution {
public int maxPathSum(TreeNode root) {
// corner case
if(root == null){return 0;}
/* 要么用个global variable放在class下,要么用长度为1的一维数组来存。
maxSum的value,可正可负,初始化为Integer.MIN_VALUE。
*/
int[] maxPath = new int[]{Integer.MIN_VALUE};
dfs(root, maxPath);
return maxPath[0];
}
// 递归求以root为顶点所有直上直下的path中,path sum最大的一条值。没有U-turn的
private int dfs(TreeNode root, int[]maxPath){
// left > 0 benefit sum, add to sum; left < 0 will worsen sum, default 0
int left = root.left != null ? Math.max(dfs(root.left, maxPath), 0) : 0;
int right = root.right != null ? Math.max(dfs(root.right, maxPath), 0) : 0;
int cur = root.val + left + right;
maxPath[0] = Math.max(maxPath[0], cur);
return root.val + Math.max(left, right);
}
}

[leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和的更多相关文章

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

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

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

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

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

  9. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

随机推荐

  1. linux 添加secondary ip

    linux下ip地址除了primary外,还有两种:1. ip alias(子接口)2. secondary ip(辅助ip) 都可在一块物理网卡上添加,alias由ifconfig添加,ifconf ...

  2. [UE4]C++ 动态内存分配(6种情况,好几个例子)

    1.堆内存分配 : C/C++定义了4个内存区间: 代码区,全局变量与静态变量区,局部变量区即栈区,动态存储区,即堆(heap)区或自由存储区(free store). 堆的概念: 通常定义变量(或对 ...

  3. 一点ExtJS开发的感悟

    虽然项目一直采用ExtJS作为前端开发,接触到了一些ExtJS 的一些场景界面,自己也尝试封装一些组件,对于开发70%基本可以满足需求.遇到最为麻烦的就是Ext的模版或者直接拼接字符串再进行eval转 ...

  4. PHP使用mysqli扩展连接MySQL数据库

    这篇文章主要介绍了PHP使用mysqli扩展连接MySQL数据库,需要的朋友可以参考下 1.面向对象的使用方式 $db = new mysqli('localhost', 'root', '12345 ...

  5. Determining IP information for eth0...failed 错误解决

    问题描述:虚拟机使用wget命令上网,执行service network restart后出现如下错误Determining IP information for eth0...failed解决办法: ...

  6. Solr SchemaXml 一些解读

    The schema.xml file contains all of the details about which fields your documents can contain, and h ...

  7. Java读取文件方法大全

    1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {    /**     * 以字节为单位读取文件,常用于读 ...

  8. filter vs servlet

    主要从如下四个方面介绍他们之间的区别:                 1.概念.                 2.生命周期.                 3.职责. 4.执行过程. 一.概念 ...

  9. UVA548

    题意: 根据二叉树中序和后序建立二叉树,从根结点开始计算和到叶子结点,输出总和最小的叶子结点,如果有俩个和一样大,输出叶子结点最小的 AC:80ms #include<stdio.h> # ...

  10. GET和POST的真正区别

    文章来源: http://www.nowamagic.net/librarys/veda/detail/1919 如果有人问你,GET和POST,有什么区别?你会如何回答? 我的经历 前几天有人问我这 ...