|
7 | 7 |
|
8 | 8 | /**
|
9 | 9 | * 124. Binary Tree Maximum Path Sum
|
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. |
14 |
| -
|
15 |
| - The path must contain at least one node and does not need to go through the root. |
16 |
| -
|
17 |
| - For example: |
18 |
| - Given the below binary tree, |
19 |
| -
|
20 |
| - 1 |
21 |
| - / \ |
22 |
| - 2 3 |
23 |
| -
|
24 |
| - Return 6. |
| 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. |
| 14 | + * |
| 15 | + * The path must contain at least one node and does not need to go through the root. |
| 16 | + * |
| 17 | + * For example: |
| 18 | + * Given the below binary tree, |
| 19 | + * |
| 20 | + * 1 |
| 21 | + * / \ |
| 22 | + * 2 3 |
| 23 | + * |
| 24 | + * Return 6. |
25 | 25 | */
|
26 | 26 | public class _124 {
|
27 | 27 |
|
28 |
| - public static class Solution1 { |
29 |
| - int max = Integer.MIN_VALUE; |
| 28 | + public static class Solution1 { |
| 29 | + int max = Integer.MIN_VALUE; |
30 | 30 |
|
31 |
| - public int maxPathSum(TreeNode root) { |
32 |
| - dfs(root); |
33 |
| - return max; |
34 |
| - } |
35 |
| - |
36 |
| - private int dfs(TreeNode root) { |
37 |
| - if (root == null) { |
38 |
| - return 0; |
39 |
| - } |
| 31 | + public int maxPathSum(TreeNode root) { |
| 32 | + dfs(root); |
| 33 | + return max; |
| 34 | + } |
40 | 35 |
|
41 |
| - int left = Math.max(dfs(root.left), 0); |
42 |
| - int right = Math.max(dfs(root.right), 0); |
| 36 | + private int dfs(TreeNode root) { |
| 37 | + if (root == null) { |
| 38 | + return 0; |
| 39 | + } |
43 | 40 |
|
44 |
| - max = Math.max(max, root.val + left + right); |
45 |
| - |
46 |
| - return root.val + Math.max(left, right); |
47 |
| - } |
48 |
| - } |
| 41 | + int left = Math.max(dfs(root.left), 0); |
| 42 | + int right = Math.max(dfs(root.right), 0); |
49 | 43 |
|
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; |
| 44 | + max = Math.max(max, root.val + left + right); |
56 | 45 |
|
57 |
| - public int maxPathSum(TreeNode root) { |
58 |
| - Map<TreeNode, Integer> map = new HashMap<>(); |
59 |
| - dfs(root, map); |
60 |
| - return max; |
| 46 | + return root.val + Math.max(left, right); |
| 47 | + } |
61 | 48 | }
|
62 | 49 |
|
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; |
| 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; |
| 61 | + } |
| 62 | + |
| 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; |
| 76 | + } |
76 | 77 | }
|
77 |
| - } |
78 | 78 | }
|
0 commit comments