1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func rangeSumBST(root *TreeNode, low int, high int) int { if root == nil { return 0 } if root.Val < low { return rangeSumBST(root.Right, low, high) } else if root.Val > high { return rangeSumBST(root.Left, low, high) } return root.Val + rangeSumBST(root.Left, low, high) + rangeSumBST(root.Right, low, high) }
|