[leetcode]124. Binary Tree Maximum Path Sum@Java解题报告
来源:互联网 发布:机器人声音制作软件 编辑:程序博客网 时间:2024/05/14 21:44
https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/
Given a 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.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
.
package go.jacob.day809;/* * 124. Binary Tree Maximum Path Sum * 解题思路:(转载自:http://blog.csdn.net/linhuanmars/article/details/22969069) * 这道题是求树的路径和的题目,不过和平常不同的是这里的路径不仅可以从根到某一个结点, * 而且路径可以从左子树某一个结点,然后到达右子树的结点,就像题目中所说的可以起始和终结于任何结点。 * 在这里树没有被看成有向图,而是被当成无向图来寻找路径。 * 因为这个路径的灵活性,我们需要对递归返回值进行一些调整,而不是通常的返回要求的结果。 * 在这里,函数的返回值定义为以自己为根的一条从根到子结点的最长路径(这里路径就不是当成无向图了, * 必须往单方向走)。 * 这个返回值是为了提供给它的父结点计算自身的最长路径用, * 而结点自身的最长路径(也就是可以从左到右那种)则只需计算然后更新即可。 * 这样一来,一个结点自身的最长路径就是它的左子树返回值(如果大于0的话), * 加上右子树的返回值(如果大于0的话),再加上自己的值。 * 而返回值则是自己的值加上左子树返回值, * 右子树返回值或者0(注意这里是“或者”,而不是“加上”,因为返回值只取一支的路径和)。 * 在过程中求得当前最长路径时比较一下是不是目前最长的,如果是则更新。 * 算法的本质还是一次树的遍历,所以复杂度是O(n)。而空间上仍然是栈大小O(logn)。 */public class Demo3 {// 因为maxPathSum不一定经过根节点,所以用maxValue整个遍历过程中出现过的最大值int maxValue = 0;public int maxPathSum(TreeNode root) {if (root == null)return 0;maxValue = Integer.MIN_VALUE;getMaxPathSum(root);return maxValue;}private int getMaxPathSum(TreeNode root) {if (root == null)return 0;//因为节点的值可以为负数,所以最大值取0和子树值的较大者int leftMax = Math.max(0, getMaxPathSum(root.left));int rightMax = Math.max(0, getMaxPathSum(root.right));//如果将当前root作为根节点,那么最大值是root.val+左子树最大值+右子树最大值maxValue = Math.max(maxValue, root.val + leftMax + rightMax);//只能返回左右子树中较大值加上root.valreturn Math.max(0, root.val + Math.max(leftMax, rightMax));}}
阅读全文
0 0
- [leetcode]124. Binary Tree Maximum Path Sum@Java解题报告
- [leetcode] 124. Binary Tree Maximum Path Sum 解题报告
- [Leetcode] 124. Binary Tree Maximum Path Sum 解题报告
- LeetCode Binary Tree Maximum Path Sum 解题报告
- [LeetCode]Binary Tree Maximum Path Sum, 解题报告
- 【LeetCode】Binary Tree Maximum Path Sum 解题报告
- LeetCode(124) Binary Tree Maximum Path Sum解题报告
- Leetcode 124. Binary Tree Maximum Path Sum 最大路径和 解题报告
- [Leetcode] Binary Tree Maximum Path Sum (Java)
- [LeetCode][Java] Binary Tree Maximum Path Sum
- LeetCode 124. Binary Tree Maximum Path Sum
- [LeetCode]*124.Binary Tree Maximum Path Sum
- [Leetcode] 124. Binary Tree Maximum Path Sum
- [leetcode] 124. Binary Tree Maximum Path Sum
- [leetcode] 124. Binary Tree Maximum Path Sum
- Leetcode 124. Binary Tree Maximum Path Sum
- Leetcode 124. Binary Tree Maximum Path Sum
- LeetCode 124. Binary Tree Maximum Path Sum
- Caffe dependency
- Python 列表(List)
- 使用angular2的http交互注意的问题
- UVA
- 极简Node教程-七天从小白变大神(四:mongoose)
- [leetcode]124. Binary Tree Maximum Path Sum@Java解题报告
- hdu1054 Strategic Game
- Connection接口中isClosed() 和 isValid() 的区别
- POJ
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5
- Xamarin.Forms 多屏显示深度解析
- Codeforces Round #303 (Div. 2) B
- 2017/8/7 离线赛
- myeclipse中svn突然失效问题