aboutsummaryrefslogtreecommitdiffstats
path: root/938/main.go
blob: 89c18d3dbc79c9a5dab829aebbf89bda96f71e4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main

import "fmt"

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func preOrder(node *TreeNode, sum *int, low, high int) {
	if node == nil {
		return
	}

	if node.Val >= low && node.Val <= high {
		*sum += node.Val
	}

	preOrder(node.Left, sum, low, high)
	preOrder(node.Right, sum, low, high)
}

func rangeSumBST(root *TreeNode, low int, high int) int {
	sum := 0

	preOrder(root, &sum, low, high)

	return sum
}

func main() {
	fmt.Println("vim-go")
}