|
7 | 7 |
|
8 | 8 | /**
|
9 | 9 | * 124. Binary Tree Maximum Path Sum
|
10 |
| - * Given a binary tree, find the maximum path sum. |
11 |
| - * For this problem, a path is defined as any sequence of nodes from some starting node to any node |
12 |
| - * in the tree along the parent-child connections. |
| 10 | +
|
| 11 | + Given a binary tree, find the maximum path sum. |
| 12 | + For this problem, a path is defined as any sequence of nodes from some starting node to any node |
| 13 | + in the tree along the parent-child connections. |
13 | 14 |
|
14 | 15 | The path must contain at least one node and does not need to go through the root.
|
15 | 16 |
|
|
24 | 25 | */
|
25 | 26 | public class _124 {
|
26 | 27 |
|
27 |
| - public static class Solution1 { |
28 |
| - int max = Integer.MIN_VALUE; |
| 28 | + public static class Solution1 { |
| 29 | + int max = Integer.MIN_VALUE; |
29 | 30 |
|
30 |
| - public int maxPathSum(TreeNode root) { |
31 |
| - dfs(root); |
32 |
| - return max; |
33 |
| - } |
| 31 | + public int maxPathSum(TreeNode root) { |
| 32 | + dfs(root); |
| 33 | + return max; |
| 34 | + } |
34 | 35 |
|
35 |
| - private int dfs(TreeNode root) { |
36 |
| - if (root == null) { |
37 |
| - return 0; |
38 |
| - } |
| 36 | + private int dfs(TreeNode root) { |
| 37 | + if (root == null) { |
| 38 | + return 0; |
| 39 | + } |
39 | 40 |
|
40 |
| - int left = Math.max(dfs(root.left), 0); |
41 |
| - int right = Math.max(dfs(root.right), 0); |
| 41 | + int left = Math.max(dfs(root.left), 0); |
| 42 | + int right = Math.max(dfs(root.right), 0); |
42 | 43 |
|
43 |
| - max = Math.max(max, root.val + left + right); |
| 44 | + max = Math.max(max, root.val + left + right); |
44 | 45 |
|
45 |
| - return root.val + Math.max(left, right); |
46 |
| - } |
| 46 | + return root.val + Math.max(left, right); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static class Solution2 { |
| 51 | + /** |
| 52 | + * This one uses a map to cache, but surprisingly, it's 10% slower than all submissions compared |
| 53 | + * with solution1 |
| 54 | + */ |
| 55 | + int max = Integer.MIN_VALUE; |
| 56 | + |
| 57 | + public int maxPathSum(TreeNode root) { |
| 58 | + Map<TreeNode, Integer> map = new HashMap<>(); |
| 59 | + dfs(root, map); |
| 60 | + return max; |
47 | 61 | }
|
48 | 62 |
|
49 |
| - public static class Solution2 { |
50 |
| - /**This one uses a map to cache, but surprisingly, it's 10% slower than all submissions compared with solution1*/ |
51 |
| - int max = Integer.MIN_VALUE; |
52 |
| - |
53 |
| - public int maxPathSum(TreeNode root) { |
54 |
| - Map<TreeNode, Integer> map = new HashMap<>(); |
55 |
| - dfs(root, map); |
56 |
| - return max; |
57 |
| - } |
58 |
| - |
59 |
| - private int dfs(TreeNode root, Map<TreeNode, Integer> map) { |
60 |
| - if (root == null) { |
61 |
| - return 0; |
62 |
| - } |
63 |
| - if (map.containsKey(root)) { |
64 |
| - return map.get(root); |
65 |
| - } |
66 |
| - int left = Math.max(0, dfs(root.left, map)); |
67 |
| - int right = Math.max(0, dfs(root.right, map)); |
68 |
| - max = Math.max(max, root.val + left + right); |
69 |
| - int pathSum = root.val + Math.max(left, right); |
70 |
| - map.put(root, pathSum); |
71 |
| - return pathSum; |
72 |
| - } |
| 63 | + private int dfs(TreeNode root, Map<TreeNode, Integer> map) { |
| 64 | + if (root == null) { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + if (map.containsKey(root)) { |
| 68 | + return map.get(root); |
| 69 | + } |
| 70 | + int left = Math.max(0, dfs(root.left, map)); |
| 71 | + int right = Math.max(0, dfs(root.right, map)); |
| 72 | + max = Math.max(max, root.val + left + right); |
| 73 | + int pathSum = root.val + Math.max(left, right); |
| 74 | + map.put(root, pathSum); |
| 75 | + return pathSum; |
73 | 76 | }
|
| 77 | + } |
74 | 78 | }
|
0 commit comments