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. eventql部署过程

    1. 环境准备install cmake make automake autoconf zlib-devel libtoolyum install zlib-devel---------------- ...

  2. js 解决图片居中问题

    下述方法能够解决图片居中问题: (1)宽一些或者高一些(相对父元素的大小):图片在父元素的可视范围内显示图片的中间位置 (2)小一些(相对父元素的大小):图片在父元素的可视范围内居中显示 实现原理:根 ...

  3. push(),pop(),unshift(),shift()

    1.push() 往数组末尾添加一个或多个元素,返回新的长度 2.pop() 删除数组末尾元素,数组长度减1,返回被删除的值 3.unshift() 往数组开头添加一个或多个元素,返回新的长度 4.s ...

  4. HTML|CSS总结与补充

    知识内容: 1.HTML总结 2.CSS总结 一.HTML总结 详细内容见:http://www.cnblogs.com/wyb666/p/8733699.html 1.HTML介绍及前端入门 (1) ...

  5. angularjs探秘<一>认识angularjs

    首先聊聊angularjs是啥. 首先AngularJS 是一个 JavaScript 框架.(PS:其实就是外部引用的js文件) 所以AngularJS的使用依然是外部引用js文件. 附上引用地址 ...

  6. javascript日期相减,求时间差

    //计算时间差 var from_date = new Date(from_time); var end_date = new Date(end_time); var time_different = ...

  7. python作用域与LEGB规则

    作用域 什么是命名空间 比如有一个学校,有10个班级,在7班和8班中都有一个叫“小王”的同学,如果在学校的广播中呼叫“小王”时,7班和8班中的这2个人就纳闷了,你是喊谁呢!!!如果是“7班的小王”的话 ...

  8. selenium自动化测试遇到的问题积累--持续积累中

    引言: 在做UI自动化测试过程中,总是会遇到各种问题,而解决问题总是会花费一些时间和心思,但是解决后对于自己就是一种成长,持续积累,当可能遇到的问题都被你遇到过,并且都知道解决办法,这就是一种经验的价 ...

  9. UVA-712-满二叉树

    一个策略树(S-tree)是一组变量Xn={x1,x2...xn}的表现形式,它代表一个布尔函数f:{0,1}n->{0,1},策略树每条路径从根结点开始由n+1个结点组成,策略树的每一个结点都 ...

  10. memcache.so的报错信息,未解决

    memcache.so php版本5.6 executor_globals_id in Unknown on line 0 编译也成功了,路径也是在其他so文件的目录 但是加载失败的,查看apache ...