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

Commitb272ffa

Browse files
refactor 938
1 parent362aca7 commitb272ffa

File tree

1 file changed

+20
-38
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-38
lines changed
Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,30 @@
11
packagecom.fishercoder.solutions;
22

33
importcom.fishercoder.common.classes.TreeNode;
4+
45
importjava.util.ArrayList;
56
importjava.util.List;
67

7-
/**
8-
* 938. Range Sum of BST
9-
*
10-
* Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).
11-
*
12-
* The binary search tree is guaranteed to have unique values.
13-
*
14-
* Example 1:
15-
* Input: root = [10,5,15,3,7,null,18], L = 7, R = 15
16-
* Output: 32
17-
*
18-
* Example 2:
19-
* Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
20-
* Output: 23
21-
*
22-
* Note:
23-
* The number of nodes in the tree is at most 10000.
24-
* The final answer is guaranteed to be less than 2^31.
25-
*/
268
publicclass_938 {
27-
publicstaticclassSolution1 {
28-
publicintrangeSumBST(TreeNoderoot,intL,intR) {
29-
if (root ==null) {
30-
return0;
31-
}
32-
List<Integer>list =newArrayList<>();
33-
dfs(root,L,R,list);
34-
returnlist.stream().mapToInt(num ->num).sum();
35-
}
9+
publicstaticclassSolution1 {
10+
publicintrangeSumBST(TreeNoderoot,intL,intR) {
11+
if (root ==null) {
12+
return0;
13+
}
14+
List<Integer>list =newArrayList<>();
15+
dfs(root,L,R,list);
16+
returnlist.stream().mapToInt(num ->num).sum();
17+
}
3618

37-
privatevoiddfs(TreeNoderoot,intl,intr,List<Integer>list) {
38-
if (root ==null) {
39-
return;
40-
}
41-
if (root.val <=r &&root.val >=l) {
42-
list.add(root.val);
43-
}
44-
dfs(root.left,l,r,list);
45-
dfs(root.right,l,r,list);
19+
privatevoiddfs(TreeNoderoot,intl,intr,List<Integer>list) {
20+
if (root ==null) {
21+
return;
22+
}
23+
if (root.val <=r &&root.val >=l) {
24+
list.add(root.val);
25+
}
26+
dfs(root.left,l,r,list);
27+
dfs(root.right,l,r,list);
28+
}
4629
}
47-
}
4830
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp