diff options
-rw-r--r-- | 938/go.mod | 3 | ||||
-rw-r--r-- | 938/main.go | 34 |
2 files changed, 37 insertions, 0 deletions
diff --git a/938/go.mod b/938/go.mod new file mode 100644 index 0000000..eb411c6 --- /dev/null +++ b/938/go.mod @@ -0,0 +1,3 @@ +module 938 + +go 1.18 diff --git a/938/main.go b/938/main.go new file mode 100644 index 0000000..89c18d3 --- /dev/null +++ b/938/main.go @@ -0,0 +1,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") +} |