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

Commitbd7db6d

Browse files
committed
add new files
1 parent2560531 commitbd7db6d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎938. Range Sum of BST.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).
3+
4+
The binary search tree is guaranteed to have unique values.
5+
6+
7+
8+
Example 1:
9+
10+
Input: root = [10,5,15,3,7,null,18], L = 7, R = 15
11+
Output: 32
12+
Example 2:
13+
14+
Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10
15+
Output: 23
16+
17+
*/
18+
package main
19+
20+
typeTreeNodestruct {
21+
Valint
22+
Left*TreeNode
23+
Right*TreeNode
24+
}
25+
funcrangeSumBST(root*TreeNode,Lint,Rint)int {
26+
res:=0
27+
help(root,L,R,&res)
28+
returnres
29+
}
30+
31+
funchelper(root*TreeNode,L,Rint,res*int){
32+
ifroot==nil{
33+
return
34+
}
35+
36+
ifroot.Val>=L&&root.Val<=R{
37+
*res+=root.Val
38+
helper(root.Left,L,R,res)
39+
helper(root.Right,L,R,res)
40+
return
41+
}
42+
ifroot.Val<L{
43+
helper(root.Right,L,R,res)
44+
}
45+
ifroot.Val>R{
46+
helper(root.Left,L,R,res)
47+
}
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp