forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
84 lines (65 loc) · 2.12 KB
/
Solution.java
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Oleg Cherednik
* @since 14.02.2019
*/
public class Solution {
public static void main(String... args) {
Node root = createTree();
List<Node> path = getMinSumPath(root);
System.out.println(path.stream().map(node -> String.valueOf(node.data)).collect(Collectors.joining(" -> "))); // 10 -> 5 -> 1 -> -1
System.out.println(path.stream().mapToInt(node -> node.data).sum()); // 15
}
private static Node createTree() {
Node fiveLeft = new Node(5);
fiveLeft.right = new Node(2);
Node one = new Node(1);
one.left = new Node(-1);
Node fiveRight = new Node(5);
fiveRight.right = one;
Node ten = new Node(10);
ten.left = fiveLeft;
ten.right = fiveRight;
return ten;
}
public static List<Node> getMinSumPath(Node node) {
if (node == null)
return Collections.emptyList();
Deque<Node> path = new LinkedList<>();
dfs(path, node, 0);
return new ArrayList<>(path);
}
private static int dfs(Deque<Node> path, Node node, int sum) {
if (node == null)
return Integer.MAX_VALUE;
path.addLast(node);
if (node.left == null && node.right == null)
return sum + node.data;
int size = path.size();
int leftSum = dfs(path, node.left, sum + node.data);
repair(path, size);
int rightSum = dfs(path, node.right, sum + node.data);
if (leftSum < rightSum) {
repair(path, size);
return dfs(path, node.left, sum + node.data);
}
return rightSum;
}
private static void repair(Deque<Node> path, int size) {
while (path.size() > size)
path.removeLast();
}
private static final class Node {
private final int data;
private Node left;
private Node right;
public Node(int data) {
this.data = data;
}
}
}