LoginSignup
0
0

More than 3 years have passed since last update.

Leetcode 938: Range Sum of BST

Last updated at Posted at 2020-01-25
#Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).
#The binary search tree is guaranteed to have unique values.
#The number of nodes in the tree is at most 10000.
#The final answer is guaranteed to be less than 2^31.

require 'test/unit'

# https://leetcode.com/problems/range-sum-of-bst/

class RangeSumOfBst < Test::Unit::TestCase

  def test_range_sum_bst
    #  TODO: Add test
  end


  def range_sum_bst(root, l, r)
    result = 0
    unless root.nil?
      puts result

      result += root.val if root.val >= l && root.val <= r
      result += range_sum_bst(root.right, l, r) unless root.right.nil?
      result += range_sum_bst(root.left, l, r) unless root.left.nil?
    end
    result
  end

end

class TreeNode
  attr_accessor :val, :left, :right

  def initialize(val)
    @val = val
    @left, @right = nil, nil
  end
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0