Files
LeetCode-Go/leetcode/0938.Range-Sum-of-BST/938. Range Sum of BST.go
2021-04-28 08:40:36 +08:00

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)
}