-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeMaximumPathSum.java
More file actions
124 lines (98 loc) · 3.86 KB
/
Copy pathBinaryTreeMaximumPathSum.java
File metadata and controls
124 lines (98 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Given a non-empty 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.
// See: https://leetcode.com/problems/binary-tree-maximum-path-sum/
package leetcode.tree;
import static leetcode.util.tree.BinTreeUtil.initTree;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import leetcode.util.tree.TreeNode;
public class BinaryTreeMaximumPathSum {
/**
* Accepted but slow solution
* TODO: Find faster alternative
*/
private long gobalMax = Integer.MIN_VALUE;
private int tmpMax = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
if (root == null)
return 0;
dfs(root);
return (int)gobalMax;
}
private void dfs(TreeNode root) {
if (root == null)
return;
tmpMax = Integer.MIN_VALUE;
dfsHelper(root.left, 0);
long l = tmpMax;
tmpMax = Integer.MIN_VALUE;
dfsHelper(root.right, 0);
long r = tmpMax;
gobalMax = Math.max(gobalMax, l + r + root.val);
gobalMax = Math.max(gobalMax, Math.max(l + root.val, r + root.val));
gobalMax = Math.max(gobalMax, root.val);
dfs(root.left);
dfs(root.right);
}
private void dfsHelper(TreeNode root, int sum) {
if (root == null)
return;
tmpMax = Math.max(tmpMax, sum + root.val);
dfsHelper(root.left, sum + root.val);
dfsHelper(root.right, sum + root.val);
}
/**
* Not accepted: Time Limit Exceeded
*/
int maxSum = 0;
public int maxPathSum1(TreeNode root) {
if (root == null)
return 0;
maxSum = root.val;
Map<TreeNode, List<TreeNode>> graph = new HashMap<>();
buildAdjGraph(root, null, graph);
for (TreeNode node : graph.keySet())
dfs(node, graph, new HashSet<TreeNode>(), node.val);
return maxSum;
}
private void dfs(TreeNode start, Map<TreeNode, List<TreeNode>> graph, Set<TreeNode> used,
int currSum) {
if (!used.contains(start)) {
maxSum = Math.max(maxSum, currSum);
used.add(start);
List<TreeNode> children = graph.get(start);
for (TreeNode child : children)
dfs(child, graph, used, currSum + child.val);
used.remove(start);
}
}
private void buildAdjGraph(TreeNode root, TreeNode parent,
Map<TreeNode, List<TreeNode>> graph) {
if (root == null)
return;
if (root.left != null)
graph.computeIfAbsent(root, k -> new LinkedList<>()).add(root.left);
if (root.right != null)
graph.computeIfAbsent(root, k -> new LinkedList<>()).add(root.right);
if (parent != null)
graph.computeIfAbsent(root, k -> new LinkedList<>()).add(parent);
buildAdjGraph(root.left, root, graph);
buildAdjGraph(root.right, root, graph);
}
public static void main(String[] args) {
TreeNode n1 = initTree(-10, 9, 20, null, null, 15, 7);
TreeNode n2 = initTree(1, 2, 3);
TreeNode n3 = initTree(9, 6, -3, null, null, -6, 2, null, null, 2, null, -6, -6, -6);
// System.out.println(new BinaryTreeMaximumPathSum().maxPathSum(initTree()));
// System.out.println(new BinaryTreeMaximumPathSum().maxPathSum(initTree(1)));
// System.out.println(new BinaryTreeMaximumPathSum().maxPathSum(n1));
// System.out.println(new BinaryTreeMaximumPathSum().maxPathSum(n2));
System.out.println(new BinaryTreeMaximumPathSum().maxPathSum(n3));
}
}