Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit54866f7

Browse files
refactor 1373
1 parentdb74ccd commit54866f7

File tree

1 file changed

+2
-38
lines changed

1 file changed

+2
-38
lines changed

‎src/main/java/com/fishercoder/solutions/_1373.java

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,11 @@
22

33
importcom.fishercoder.common.classes.TreeNode;
44

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-
* */
415
publicclass_1373 {
426
publicstaticclassSolution1 {
437
/**
448
* credit: https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/discuss/532021/Java-Post-Order
45-
* */
9+
*/
4610
publicintmaxSumBST(TreeNoderoot) {
4711
returnpostOrder(root)[4];
4812
}
@@ -53,7 +17,7 @@ public int maxSumBST(TreeNode root) {
5317
* result[2] means the left boundary
5418
* result[3] means the right boundary
5519
* result[4] means the global max sum
56-
* */
20+
*/
5721
privateint[]postOrder(TreeNoderoot) {
5822
if (root ==null) {
5923
returnnewint[]{1,0,Integer.MAX_VALUE,Integer.MIN_VALUE,0};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp