LeetCode - Hard - 124. Binary Tree Maximum Path Sum

我不是女神ヾ 2022-10-14 01:12 124阅读 0赞

Topic

  • Tree
  • Depth-first Search
  • Recursion

Description

https://leetcode.com/problems/binary-tree-maximum-path-sum/

A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.

The path sum of a path is the sum of the node’s values in the path.

Given the root of a binary tree, return the maximum path sum of any path.

Example 1:

ca0d0d9f728f2aeda1976de9b08940e5.png

  1. Input: root = [1,2,3]
  2. Output: 6
  3. Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.

Example 2:

730fd58fb57c0fbc6d5f77b18e9d6b50.png

  1. Input: root = [-10,9,20,null,null,15,7]
  2. Output: 42
  3. Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.

Constraints:

  • The number of nodes in the tree is in the range [ 1 , 3 ∗ 1 0 4 ] [1, 3 * 10^4] [1,3∗104].
  • − 1000 < = N o d e . v a l < = 1000 -1000 <= Node.val <= 1000 −1000<=Node.val<=1000

Analysis

判断两节点之间节点值和,最好从树叶到树根的遍历方式,所以用二叉树的后序遍历模式

方法一:我写的。

方法二:别人写的,优雅了许多。

方法二中,当父节点值与左右子孙节点和值相加时,如果左右子孙节点和值是负数,那么干脆不采用它,也就是用0代替这节点值(如和为-9,它的父节点为n, n + ( − 9 ) < n + 0 n + (-9) < n + 0 n+(−9)<n+0

Submission

  1. import com.lun.util.BinaryTree.TreeNode;
  2. public class BinaryTreeMaximumPathSum {
  3. private static int MIN_VALUE = -1001;//不用Integer.MIN_VALUE,原因是用它可能造成溢出情况
  4. //方法一:我写的
  5. public int maxPathSum(TreeNode root) {
  6. int[] maxAlone = { MIN_VALUE};
  7. int max = maxPathSum(root, maxAlone);
  8. return Math.max(maxAlone[0], max);
  9. }
  10. private int maxPathSum(TreeNode node, int[] maxAlone) {
  11. if(node == null) return MIN_VALUE;
  12. int leftMax = maxPathSum(node.left, maxAlone);
  13. int rightMax = maxPathSum(node.right, maxAlone);
  14. int maxOne = Math.max(Math.max(leftMax, rightMax), //
  15. leftMax + rightMax + node.val);
  16. if(maxOne > maxAlone[0]) {
  17. maxAlone[0] = maxOne;
  18. }
  19. return Math.max(Math.max(leftMax + node.val, rightMax + node.val),//
  20. node.val);
  21. }
  22. //方法二:别人写的
  23. public int maxPathSum2(TreeNode root) {
  24. int[] max = { Integer.MIN_VALUE};
  25. maxPathSum2(root, max);
  26. return max[0];
  27. }
  28. private int maxPathSum2(TreeNode node, int[] max) {
  29. if (node == null) return 0;
  30. //如果节点值是负数,那么干脆不采用这节点值,也就是用0代替这节点值
  31. //(如节点值为-9,它的父节点为n,n+(-9) < n + 0
  32. int leftMax = Math.max(0, maxPathSum2(node.left, max));
  33. int rightMax = Math.max(0, maxPathSum2(node.right, max));
  34. max[0] = Math.max(max[0], leftMax + rightMax + node.val);
  35. return Math.max(leftMax, rightMax) + node.val;
  36. }
  37. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. import com.lun.util.BinaryTree;
  4. public class BinaryTreeMaximumPathSumTest {
  5. @Test
  6. public void test() {
  7. BinaryTreeMaximumPathSum obj = new BinaryTreeMaximumPathSum();
  8. assertEquals(6, obj.maxPathSum(BinaryTree.integers2BinaryTree(1, 2, 3)));
  9. assertEquals(42, obj.maxPathSum(BinaryTree.integers2BinaryTree(-10, 9, 20, null, null, 15, 7)));
  10. assertEquals(-3, obj.maxPathSum(BinaryTree.integers2BinaryTree(-3)));
  11. assertEquals(1, obj.maxPathSum(BinaryTree.integers2BinaryTree(-2, 1)));
  12. assertEquals(10, obj.maxPathSum(BinaryTree.integers2BinaryTree(-1,-2,10,-6,null,-3,-6)));
  13. }
  14. @Test
  15. public void test2() {
  16. BinaryTreeMaximumPathSum obj = new BinaryTreeMaximumPathSum();
  17. assertEquals(6, obj.maxPathSum2(BinaryTree.integers2BinaryTree(1, 2, 3)));
  18. assertEquals(42, obj.maxPathSum2(BinaryTree.integers2BinaryTree(-10, 9, 20, null, null, 15, 7)));
  19. assertEquals(-3, obj.maxPathSum2(BinaryTree.integers2BinaryTree(-3)));
  20. assertEquals(1, obj.maxPathSum2(BinaryTree.integers2BinaryTree(-2, 1)));
  21. assertEquals(10, obj.maxPathSum2(BinaryTree.integers2BinaryTree(-1,-2,10,-6,null,-3,-6)));
  22. }
  23. }

发表评论

表情:
评论列表 (有 0 条评论,124人围观)

还没有评论,来说两句吧...

相关阅读