From 351af422667c4055bd806065d8bbafbc73facf0e Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Wed, 7 Dec 2022 07:23:52 +0330 Subject: 938 --- 938/main.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 938/main.go (limited to '938/main.go') 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