Learning GoLang: Algorithm to Compute the Range Sum of a Binary Search Tree

in #programming5 years ago
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:

--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

Delegation Service

  1. Voting Algorithm Updated to Favor those High Delegations!
  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. 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

Sort:  

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)

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.097
BTC 65045.78
ETH 1919.76
USDT 1.00
SBD 0.39