本文首发于 语雀文档
重点:
代码:
/*** Definition for a binary tree node.* class TreeNode {* val: number* left: TreeNode | null* right: TreeNode | null* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {* this.val = (val===undefined ? 0 : val)* this.left = (left===undefined ? null : left)* this.right = (right===undefined ? null : right)* }* }*/function rangeSumBST(root: TreeNode | null, low: number, high: number): number {if (root === null) return 0let sum = 0const check = (root: TreeNode | null) => {if (root === null) returncheck(root.left)check(root.right)if (root.val >= low && root.val <= high) {sum = sum + root.val}}check(root.left)check(root.right)if (root.val >= low && root.val <= high) {sum = sum + root.val}return sum};
