Learning GoLang: Algorithm to Compute the Range Sum of a Binary Search Tree
Given the root node of a binary search tree, return the sum of values of all nodes with a value in the range [low, high].
GoLang Implementation: Range Sum of a BST via Stack
In Go, we use an list to implement the stack. And we can push the left and/or right branches if it is still falling within the current range.
/**
* 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
}
var st = make([]*TreeNode, 0)
var ans = 0
st = append(st, root)
for len(st) > 0 {
var sz = len(st)
var cur = st[sz - 1]
st = st[:sz - 1]
if cur == nil {
continue
}
if cur.Val >= low && cur.Val <= high {
ans += cur.Val
}
if cur.Val > low {
st = append(st, cur.Left)
}
if cur.Val < high {
st = append(st, cur.Right)
}
}
return ans
}
GoLang Implementation: Range Sum of a BST via Recursion
With Recursion, we can recursively sum up the nodes in the left and right tree respectively that are within the range [low, high]. The time complexity is also O(N). Space complexity is O(N) where N is the number of the nodes in the Binary Search Tree - each node is visited once.
/**
* 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
}
var ans = 0
if low <= root.Val && root.Val <= high {
ans += root.Val
}
if root.Val >= low {
ans += rangeSumBST(root.Left, low, high)
}
if root.Val <= high {
ans += rangeSumBST(root.Right, low, high)
}
return ans
}
See also other implementations of the Range Sum of BST in other programming languages:
- Teaching Kids Programming - Algorithms to Compute the Range Sum of a Binary Search Tree
- How to Sum within A Range in a Binary Search Tree?
- GoLang: Algorithm to Compute the Range Sum of a Binary Search Tree
--EOF (The Ultimate Computing & Technology Blog) --
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses

great post
[WhereIn Android] (http://www.wherein.io)
这是Go语言写的?看起来很像C啊,还有指针呢
[WhereIn Android] (http://www.wherein.io)
对,GO语言,语法有点怪。
Hi, I just approved you as a Witness and delegated 100 SP (all I have). I post mostly about Indonesian history. Nice to know you.
Best regards.
如果我把他作为鲸鱼投给了他,并委托他,为什么他的票值在亚洲很低?@justyy
👍🙏
[WhereIn Android] (http://www.wherein.io)