[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 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二叉树最大路径和的更多相关文章
- 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 ...
- 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 ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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. ...
- LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- git如何查看某个人提交的日志。
我们知道,在git进行cherry-pick的时候,找到commit id是至关重要, 如果我们是很多人在一个分支开发,开发完了之后,发现某个人的功能,需要单独cherry-pick到另外一分支上去. ...
- Linux中make, make install命令分别是什么
用于linux源码安装软件,一般下载源码包得到文件:xxxx.tgz====================================1.解包软件 tar zxf xxxx.tgz======= ...
- pyH支持python3
记录下,感谢大神,原地址https://www.cnblogs.com/yunmenzhe/p/6293428.html,侵删 1.修改xxx/python3.5/pyh.py权限 sudo chmo ...
- javascript事件处理程序的3个阶段
第一阶段:HTML事件处理阶段.就是在元素里面添加onclick之类的属性来调用某个函数. <input type="button" value="单击" ...
- System.Security.Authentication.AuthenticationException:根据验证过程,远程证书无效。
好久没写博客了,今天突然遇到个神奇的问题. 做好的网站在win10上和Windows sever 2012 上都没有问题,搬到Windows sever 2003上就出现了这么一个错误: Server ...
- php变量详细讲解
变量是用于存储信息的"容器". 定义一个变量的语法: $变量名 = 值; 使用变量的例子: <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> ...
- css (具体代码看笔记本)
参考:https://www.cnblogs.com/liwenzhou/p/7999532.html 1. CSS语法 选择器 {属性1:值1;...;} 2. CSS导入方式 1. 行 ...
- 解决Sybase PowerDesigner 数据库设计中 Name 自动填充Code
在使用 Sybase PowerDesigner 进行数据库设计时,为了理清思路,需要将name改为中文名称,但是这个软件会自动将name填 充为code,可以通过如下配置修改: 选择tools-&g ...
- python入门-变量和简单数据类型
1 title() 是以首字母大写的方式显示每个单词 lower() 字母小写 upper() 字母大写 2 python使用+号来合并字符串 字符串中使用制表符用\t 字符串中使用换行符\n 用rs ...
- 应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测
应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测 Real-Time Anomaly Detection for Streaming Analytics Subutai Ahmad SAHM ...