Skip to content

Commit 6fab830

Browse files
add 1373
1 parent bb86214 commit 6fab830

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1373|[Maximum Sum BST in Binary Tree](https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1373.java) | |Hard||DP, BST
1112
|1371|[Find the Longest Substring Containing Vowels in Even Counts](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1371.java) | |Medium||String
1213
|1370|[Increasing Decreasing String](https://leetcode.com/problems/increasing-decreasing-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1370.java) | |Easy||String, Sort
1314
|1367|[Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1367.java) | |Medium||DP, Linked List, Tree
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 1373. Maximum Sum BST in Binary Tree
7+
*
8+
* Given a binary tree root, the task is to return the maximum sum of all keys of any sub-tree which is also a Binary Search Tree (BST).
9+
* Assume a BST is defined as follows:
10+
* The left subtree of a node contains only nodes with keys less than the node's key.
11+
* The right subtree of a node contains only nodes with keys greater than the node's key.
12+
* Both the left and right subtrees must also be binary search trees.
13+
*
14+
* Example 1:
15+
* Input: root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
16+
* Output: 20
17+
* Explanation: Maximum sum in a valid Binary search tree is obtained in root node with key equal to 3.
18+
*
19+
* Example 2:
20+
* Input: root = [4,3,null,1,2]
21+
* Output: 2
22+
* Explanation: Maximum sum in a valid Binary search tree is obtained in a single root node with key equal to 2.
23+
*
24+
* Example 3:
25+
* Input: root = [-4,-2,-5]
26+
* Output: 0
27+
* Explanation: All values are negatives. Return an empty BST.
28+
*
29+
* Example 4:
30+
* Input: root = [2,1,3]
31+
* Output: 6
32+
*
33+
* Example 5:
34+
* Input: root = [5,4,8,3,null,6,3]
35+
* Output: 7
36+
*
37+
* Constraints:
38+
* Each tree has at most 40000 nodes..
39+
* Each node's value is between [-4 * 10^4 , 4 * 10^4].
40+
* */
41+
public class _1373 {
42+
public static class Solution1 {
43+
/**
44+
* credit: https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/discuss/532021/Java-Post-Order
45+
* */
46+
public int maxSumBST(TreeNode root) {
47+
return postOrder(root)[4];
48+
}
49+
50+
/**
51+
* result[0] means this tree is a BST
52+
* result[1] means the sum of this tree
53+
* result[2] means the left boundary
54+
* result[3] means the right boundary
55+
* result[4] means the global max sum
56+
* */
57+
private int[] postOrder(TreeNode root) {
58+
if (root == null) {
59+
return new int[]{1, 0, Integer.MAX_VALUE, Integer.MIN_VALUE, 0};
60+
}
61+
int[] leftSide = postOrder(root.left);
62+
int[] rightSide = postOrder(root.right);
63+
int localMax = Math.max(leftSide[4], rightSide[4]);
64+
if (leftSide[0] == 1 && rightSide[0] == 1 && root.val > leftSide[3] && root.val < rightSide[2]) {
65+
int sum = root.val + leftSide[1] + rightSide[1];
66+
return new int[]{1, sum, leftSide[2] == Integer.MAX_VALUE ? root.val : leftSide[2], rightSide[3] == Integer.MIN_VALUE ? root.val : rightSide[3], Math.max(localMax, sum)};
67+
} else {
68+
return new int[]{0, 0, 0, 0, localMax};
69+
}
70+
}
71+
}
72+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1373;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1373Test {
13+
private static _1373.Solution1 solution1;
14+
private static TreeNode root;
15+
16+
@Test
17+
public void test1() {
18+
solution1 = new _1373.Solution1();
19+
root = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 3));
20+
TreeUtils.printBinaryTree(root);
21+
assertEquals(6, solution1.maxSumBST(root));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
solution1 = new _1373.Solution1();
27+
root = TreeUtils.constructBinaryTree(Arrays.asList(5, 4, 8, 3, null, 6, 3));
28+
TreeUtils.printBinaryTree(root);
29+
assertEquals(7, solution1.maxSumBST(root));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
solution1 = new _1373.Solution1();
35+
root = TreeUtils.constructBinaryTree(Arrays.asList(-4, -2, -5));
36+
TreeUtils.printBinaryTree(root);
37+
assertEquals(0, solution1.maxSumBST(root));
38+
}
39+
40+
@Test
41+
public void test4() {
42+
solution1 = new _1373.Solution1();
43+
root = TreeUtils.constructBinaryTree(Arrays.asList(4, 3, null, 1, 2));
44+
TreeUtils.printBinaryTree(root);
45+
assertEquals(2, solution1.maxSumBST(root));
46+
}
47+
48+
@Test
49+
public void test5() {
50+
solution1 = new _1373.Solution1();
51+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 4, 3, 2, 4, 2, 5, null, null, null, null, null, null, 4, 6));
52+
TreeUtils.printBinaryTree(root);
53+
assertEquals(20, solution1.maxSumBST(root));
54+
}
55+
56+
@Test
57+
public void test6() {
58+
solution1 = new _1373.Solution1();
59+
root = TreeUtils.constructBinaryTree(Arrays.asList(4, 8, null, 6, 1, 9, null, -5, 4, null, null, null, -3, null, 10));
60+
TreeUtils.printBinaryTree(root);
61+
assertEquals(14, solution1.maxSumBST(root));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)