mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
35 lines
614 B
Go
35 lines
614 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
* type TreeNode struct {
|
|
* Val int
|
|
* Left *TreeNode
|
|
* Right *TreeNode
|
|
* }
|
|
*/
|
|
|
|
func rangeSumBST(root *TreeNode, low int, high int) int {
|
|
res := 0
|
|
preOrder(root, low, high, &res)
|
|
return res
|
|
}
|
|
|
|
func preOrder(root *TreeNode, low, high int, res *int) {
|
|
if root == nil {
|
|
return
|
|
}
|
|
if low <= root.Val && root.Val <= high {
|
|
*res += root.Val
|
|
}
|
|
preOrder(root.Left, low, high, res)
|
|
preOrder(root.Right, low, high, res)
|
|
}
|