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 node sequence 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.
也就是說 不一定是路徑 可以是任何一條路,而root可以是其中的任何一箇中間點。
just assume any node can be the inner node of the final results.
but my idea is totally wrong. I don’t even know what I’m doing.
class Solution {
private int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
if (root == null) return 0;
maxPass(root);
return Math.max(maxPathSum(root.left), maxPathSum(root.right));
}
private int maxPass(TreeNode root) {
if (root == null) return 0;
max = Math.max(max, root.val + maxPass(root.left) + maxPass(root.right));
return root.val + maxPass(root.left) + maxPass(root.right);
}
}
but the correct solution is as follows, but it’s so hard to understand.
however, I find some good explainations:
Each node actually has two roles when it comes to function maxPathDown. When processing the final result maxValue, the node is treated as the highest point of a path. When calculating its return value, it is only part of a path (left or right part), and this return value will be used to calculate path sum of other paths with some other nodes(above the current one) as their highest point.
class Solution {
private int res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
if(root == null) return 0;
helper(root);
return res;
}
private int helper(TreeNode root){ //
if(root == null) return 0;
int left = Math.max(0, helper(root.left)); //如果左邊要是小於0 那還不如根本不走left
int right = Math.max(0, helper(root.right)); //同理
res = Math.max(res, left + right + root.val); //root作為路徑最高點
return Math.max(left, right) + root.val; //往下走必須選擇左或者右的最高點。
}
}
相關文章
- Leetcode Binary Tree Maximum Path SumLeetCode
- Binary Tree Maximum Path Sum leetcode javaLeetCodeJava
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- Leetcode-Bianry Tree Maximum Path SumLeetCode
- Leetcode Maximum Depth of Binary TreeLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- Leetcode-Maximum Depth of Binary TreeLeetCode
- Maximum Depth of Binary Tree leetcode javaLeetCodeJava
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- 104. Maximum Depth of Binary Tree--LeetCode RecordLeetCode
- Leetcode Path SumLeetCode
- 【LeetCode從零單排】No104 Maximum Depth of Binary TreeLeetCode
- Leetcode Path Sum IILeetCode
- Leetcode-Path SumLeetCode
- Path Sum leetcode javaLeetCodeJava
- 104. Maximum Depth of Binary Tree(圖解)圖解
- 【LeetCode 104_二叉樹_遍歷】Maximum Depth of Binary TreeLeetCode二叉樹
- Leetcode Minimum Path SumLeetCode
- Leetcode-Path Sum IILeetCode
- Path Sum II leetcode javaLeetCodeJava
- Maximum Depth of Binary Tree 二叉樹的深度二叉樹
- Leetcode Binary Tree PathsLeetCode
- LeetCode Invert Binary TreeLeetCode
- Leetcode Balanced Binary TreeLeetCode
- Leetcode-Minimum Path SumLeetCode
- Minimum Path Sum leetcode javaLeetCodeJava
- LeetCode-Maximum Size Subarray Sum Equals kLeetCode
- LeetCode-Binary Tree PathsLeetCode
- leetcode - Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Inorder TraversalLeetCode
- Leetcode Binary Tree Preorder TraversalLeetCode
- Leetcode Binary Tree Postorder TraversalLeetCode
- Leetcode-Balanced Binary TreeLeetCode
- Balanced Binary Tree leetcode javaLeetCodeJava
- LeetCode 112. Path SumLeetCode
- [LeetCode] 2841. Maximum Sum of Almost Unique SubarrayLeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode