Skip to content

Commit cb98b2b

Browse files
solves #124: Binary Tree Maximum Path Sum in java
1 parent a3715bf commit cb98b2b

File tree

4 files changed

+93
-68
lines changed

4 files changed

+93
-68
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
| 120 | [Triangle](https://leetcode.com/problems/triangle) | [![Java](assets/java.png)](src/Triangle.java) | |
122122
| 121 | [Best Time to Buy and Sell Stocks](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [![Java](assets/java.png)](src/BestTimeToBuyAndSellStock.java) [![Python](assets/python.png)](python/best_time_to_buy_and_sell_stock.py) | |
123123
| 122 | [Best Time to Buy and Sell Stocks II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [![Java](assets/java.png)](src/BestTimeToBuyAndSellStockII.java) [![Python](assets/python.png)](python/best_time_to_buy_and_sell_stock_ii.py) | |
124+
| 123 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum) | [![Java](assets/java.png)](src/BinaryTreeMaximumPathSum.java) | |
124125
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | [![Java](assets/java.png)](src/ValidPalindrome.java) [![Python](assets/python.png)](python/valid_palindrome.py) | |
125126
| 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence) | [![Java](assets/java.png)](src/LongestConsecutiveSequence.java) | |
126127
| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers) | [![Java](assets/java.png)](src/SumRootToLeafNumbers.java) | |

src/BinaryTreeMaximumPathSum.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/binary-tree-maximum-path-sum
2+
// T: O(N)
3+
// S: O(logN)
4+
5+
public class BinaryTreeMaximumPathSum {
6+
private static int result = Integer.MIN_VALUE;
7+
8+
public int maxPathSum(TreeNode root) {
9+
result = Integer.MIN_VALUE;
10+
if (root == null) {
11+
return 0;
12+
}
13+
maxSum(root);
14+
return result;
15+
}
16+
17+
private static int maxSum(TreeNode root) {
18+
if (root == null) {
19+
return 0;
20+
}
21+
22+
final int maxSumLeftSubtree = Math.max(maxSum(root.left), 0);
23+
final int maxSumRightSubtree = Math.max(maxSum(root.right), 0);
24+
final int maxPathSum = root.val + maxSumLeftSubtree + maxSumRightSubtree;
25+
result = Math.max(result, maxPathSum);
26+
27+
return Math.max(root.val + maxSumLeftSubtree, root.val + maxSumRightSubtree);
28+
}
29+
}

src/HelloWorld.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
public class HelloWorld {
2-
public ListNode reverseBetween(ListNode head, int left, int right) {
3-
if (head == null) {
4-
return null;
5-
}
6-
7-
ListNode result = new ListNode(0);
8-
result.next = head;
2+
private static int result = Integer.MIN_VALUE;
93

10-
ListNode start = result;
11-
for (int i = 1 ; i < left ; i++) {
12-
start = start.next;
4+
public static int maxPathSum(TreeNode root) {
5+
result = Integer.MIN_VALUE;
6+
if (root == null) {
7+
return 0;
138
}
9+
maxSum(root);
10+
return result;
11+
}
1412

15-
ListNode startNext = start.next, a = startNext, b = start.next.next;
16-
for (int count = left ; b != null && count < right ; count++) {
17-
ListNode c = b.next;
18-
b.next = a;
19-
a = b;
20-
b = c;
13+
private static int maxSum(TreeNode root) {
14+
if (root == null) {
15+
return 0;
2116
}
2217

23-
start.next = a;
24-
startNext.next = b;
25-
26-
return result.next;
27-
}
18+
final int maxGainLeftSubtree = Math.max(maxSum(root.left), 0);
19+
final int maxGainRightSubtree = Math.max(maxSum(root.right), 0);
20+
final int maxPathSum = root.val + maxGainLeftSubtree + maxGainRightSubtree;
21+
result = Math.max(result, maxPathSum);
2822

29-
public static void main(String[] args) {
30-
ListNode node = new ListNode(1);
31-
node.next = new ListNode(2);
32-
node.next.next = new ListNode(3);
33-
node.next.next.next = new ListNode(4);
23+
return Math.max(root.val + maxGainLeftSubtree, root.val + maxGainRightSubtree);
3424
}
3525
}

src/LRUCache.java

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,82 @@ public class LRUCache {
88
private static class Node {
99
int key;
1010
int value;
11-
Node next;
1211
Node previous;
12+
Node next;
1313

14-
Node() { }
15-
Node(int key, int value) {
14+
public Node(int key, int value) {
1615
this.key = key;
1716
this.value = value;
1817
}
1918
}
2019

21-
private final int capacity;
2220
private final Map<Integer, Node> cache = new HashMap<>();
21+
private final int capacity;
2322
private Node head, tail;
2423

2524
public LRUCache(int capacity) {
2625
this.capacity = capacity;
27-
head = new Node();
28-
tail = new Node();
29-
head.next = tail;
30-
tail.previous = head;
3126
}
3227

3328
public int get(int key) {
34-
Node node = cache.get(key);
35-
if(node == null) {
29+
if (!cache.containsKey(key)) {
3630
return -1;
3731
}
38-
moveToHead(node);
32+
final Node node = cache.get(key);
33+
moveNodeToTail(node);
3934
return node.value;
4035
}
4136

4237
public void put(int key, int value) {
43-
Node node = cache.get(key);
44-
45-
if(node == null) {
46-
Node newNode = new Node(key, value);
47-
this.cache.put(key, newNode);
48-
this.addNode(newNode);
38+
if (cache.containsKey(key)) {
39+
Node node = cache.get(key);
40+
node.value = value;
41+
moveNodeToTail(node);
42+
} else if (cache.size() == capacity) {
43+
Node node = new Node(key, value);
44+
cache.put(key, node);
45+
cache.remove(head.key);
46+
appendToTail(node);
47+
popHead();
48+
} else {
49+
Node node = new Node(key, value);
50+
cache.put(key, node);
4951

50-
if(cache.size() > capacity){
51-
Node tail = popTail();
52-
this.cache.remove(tail.key);
52+
if (cache.size() == 1) {
53+
head = node;
54+
tail = node;
55+
} else {
56+
appendToTail(node);
5357
}
54-
} else {
55-
node.value = value;
56-
moveToHead(node);
5758
}
5859
}
5960

60-
private Node popTail(){
61-
Node res = tail.previous;
62-
this.removeNode(res);
63-
return res;
64-
}
61+
private void moveNodeToTail(Node node) {
62+
if (node == tail) {
63+
return;
64+
}
65+
66+
if (node == head) {
67+
appendToTail(node);
68+
popHead();
69+
return;
70+
}
71+
72+
Node previous = node.previous, next = node.next;
73+
previous.next = next;
74+
next.previous = previous;
6575

66-
private void addNode(Node node) {
67-
node.previous = head;
68-
node.next = head.next;
69-
head.next.previous = node;
70-
head.next = node;
76+
appendToTail(node);
7177
}
7278

73-
private void removeNode(Node node){
74-
Node pre = node.previous;
75-
Node post = node.next;
76-
pre.next = post;
77-
post.previous = pre;
79+
private void appendToTail(Node node) {
80+
tail.next = node;
81+
node.previous = tail;
82+
tail = node;
7883
}
7984

80-
private void moveToHead(Node node){
81-
this.removeNode(node);
82-
this.addNode(node);
85+
private void popHead() {
86+
head = head.next;
87+
head.previous = null;
8388
}
84-
}
89+
}

0 commit comments

Comments
 (0)