Skip to content

Latest commit

 

History

History
25 lines (22 loc) · 530 Bytes

2022-12-07.md

File metadata and controls

25 lines (22 loc) · 530 Bytes

题目

  1. Range Sum of BST

Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

思路

代码

var rangeSumBST = function(root, low, high) {
  let sum = 0
  function preOrder(node) {
    if (!node) {
      return
    }
    if (node.val >= low && node.val <= high) {
      sum += node.val
    }
    preOrder(node.left)
    preOrder(node.right)
  }
  preOrder(root)
  return sum
}