-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange-sum-of-bst_0226.html
44 lines (39 loc) · 1.05 KB
/
range-sum-of-bst_0226.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>range-sum-of-bst</title>
</head>
<body>
</body>
<script>
const root = [10,5,15,3,7,null,18];
const low = 7;
const high = 15;
console.log(rangeSumBST(root, low, high));
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} low
* @param {number} high
* @return {number}
*/
function rangeSumBST(root, low, high) {
let result = 0;
if(!root) return 0;
if(root.val >=low && root.val <= high) result += root.val;
if(root.val >= low) result += rangeSumBST(root.left, low, high);
if(root.val <= high) result += rangeSumBST(root.right, low, high);
return result
};
</script>
</html>