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. js之ActiveX控件使用说明 new ActiveXObject()

    什么是 ActiveX 控件? ActiveX 控件广泛用于 Internet.它们可以通过提供视频.动画内容等来增加浏览的乐趣.不过,这些程序可能出问题或者向您提供不需要的内容.在某些情况下,这些程 ...

  2. [UE4]FString常用API

    转自:http://aigo.iteye.com/blog/2279808 将int或float转换为string: 将FString转换为char*: 将string转换为int或者float: 字 ...

  3. eventql操作脚本

    a) standalone mode mkdir -p /var/evql/standalone/usr/local/bin/evqld --standalone --datadir /var/evq ...

  4. Delphi Webbrowser使用方法详解(一)

    1.webbroser介绍 该组件是一个浏览器组件,可以显示一个指定地址的网页.设置网页打开时的主页以及对网页进行相关的操作,同时也可以对HTML文件进行剪切.复制.粘贴.删除等操作.该 组件在Int ...

  5. mysql5.5版本以后插入中午显示问号的解决办法

    先看看中午变问号的结果 现在看看我们建立数据库和建表的操作 看到这里相信大家都知道创建成功了,没错,数据库跟表是创建成功了,可当你录入的信息带中文的时候就显示问号. 现在用传统的解决办法 在查看下表的 ...

  6. gz文件最后四位检测

    [root@node-0 ~]# ll -rw-r--r--  1 root root 24048 Nov 29 11:29 install.log 文件大小为24048 [root@node-0 ~ ...

  7. 并发基础(六) 线程Thread类的start()和run()

    start()和run()方法对于刚接触线程的人来说,会有点混淆,有点难理解,一般都会有以下疑问: 一.start( )方法 1.为什么需要start方法:它的作用是什么: start方法的作用就是将 ...

  8. Linux 各类设置、配置、使用技巧参考,Linux使用集锦

    ========== 参考格式 (新增记录时,复制粘贴在下)============= [日期]: <标题> 参考链接ref1: 参考链接ref2: 正文: ========== 参考格式 ...

  9. uva-10112-计算几何

    题意:给你一些点,求这些点组成的三角形面积最大,而且三角形内不能包含其他点 #include <iostream> #include <math.h> #include< ...

  10. 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv

    使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...