From 351af422667c4055bd806065d8bbafbc73facf0e Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 7 Dec 2022 07:23:52 +0330 Subject: 938 --- 938/go.mod | 3 +++ 938/main.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 938/go.mod create mode 100644 938/main.go (limited to '938') 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") +} -- cgit v1.2.3