Skip to content

Commit e361866

Browse files
committed
leetcode
1 parent 3164f3c commit e361866

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
160160
- [**2256.** Minimum Average Difference](MinimumAverageDifference/minimum_average_difference.dart)
161161
- [**876.** Middle of the Linked List](MiddleOfTheLinkedList/middle_of_the_linked_list.dart)
162162
- [**328.** Odd Even Linked List](OddEvenLinkedList/odd_even_linked_list.dart)
163+
- [**938.** Range Sum of BST](RangeSumOfBST/range_sum_of_bst.dart)
163164

164165
## Reach me via
165166

Diff for: RangeSumOfBST/range_sum_of_bst.dart

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
3+
4+
-* 938. Range Sum of BST *-
5+
6+
7+
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].
8+
9+
10+
11+
Example 1:
12+
13+
14+
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
15+
Output: 32
16+
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
17+
Example 2:
18+
19+
20+
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
21+
Output: 23
22+
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
23+
24+
25+
Constraints:
26+
27+
The number of nodes in the tree is in the range [1, 2 * 104].
28+
1 <= Node.val <= 105
29+
1 <= low <= high <= 105
30+
All Node.val are unique.
31+
32+
33+
*/
34+
35+
// Definition for a binary tree node.
36+
class TreeNode {
37+
int val;
38+
TreeNode? left;
39+
TreeNode? right;
40+
TreeNode([this.val = 0, this.left, this.right]);
41+
}
42+
43+
class A {
44+
int rangeSumBST(TreeNode? root, int low, int high) {
45+
if (root == null) return 0; // base case.
46+
if (root.val < low)
47+
return rangeSumBST(root.right, low, high); // left branch excluded.
48+
if (root.val > high)
49+
return rangeSumBST(root.left, low, high); // right branch excluded.
50+
return root.val +
51+
rangeSumBST(root.right, low, high) +
52+
rangeSumBST(root.left, low, high); // count in both children.
53+
}
54+
}
55+
56+
class B {
57+
int rangeSumBST(TreeNode? root, int low, int high) {
58+
if (root == null) return 0; // base case.
59+
return (low <= root.val && root.val <= high ? root.val : 0) +
60+
rangeSumBST(root.right, low, high) +
61+
rangeSumBST(root.left, low, high);
62+
}
63+
}
64+
65+
class C {
66+
int rangeSumBST(TreeNode? root, int low, int high) {
67+
List<TreeNode?> stk = [];
68+
stk.add(root);
69+
int sum = 0;
70+
while (stk.isNotEmpty) {
71+
TreeNode? n = stk.removeLast();
72+
if (n == null) {
73+
continue;
74+
}
75+
if (n.val > low) {
76+
stk.add(n.left);
77+
} // left child is a possible candidate.
78+
if (n.val < high) {
79+
stk.add(n.right);
80+
} // right child is a possible candidate.
81+
if (low <= n.val && n.val <= high) {
82+
sum += n.val;
83+
}
84+
}
85+
return sum;
86+
}
87+
}

Diff for: RangeSumOfBST/range_sum_of_bst.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
// Definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
// func rangeSumBST(root *TreeNode, low int, high int) int {
11+
// if root == nil {
12+
// return 0
13+
// }
14+
// if root.Val < low {
15+
// return rangeSumBST(root.Right, low, high)
16+
// }
17+
// if root.Val > high {
18+
// return rangeSumBST(root.Left, low, high)
19+
// }
20+
// return root.Val + rangeSumBST(root.Right, low, high) + rangeSumBST(root.Left, low, high)
21+
// }
22+
23+
func rangeSumBST(root *TreeNode, low int, high int) int {
24+
var stack []TreeNode
25+
stack = append(stack, *root)
26+
var sum int = 0
27+
for len(stack) != 0 {
28+
var node *TreeNode = &stack[len(stack)-1]
29+
if node == nil {
30+
continue
31+
}
32+
if node.Val > low {
33+
stack = append(stack, *node.Left)
34+
}
35+
if node.Val < high {
36+
stack = append(stack, *node.Right)
37+
}
38+
if low <= node.Val && node.Val <= high {
39+
sum += node.Val
40+
}
41+
}
42+
return sum
43+
}
44+
45+
/*
46+
47+
int rangeSumBST(TreeNode? root, int low, int high) {
48+
List<TreeNode?> stk = [];
49+
stk.add(root);
50+
int sum = 0;
51+
while (stk.isNotEmpty) {
52+
TreeNode? n = stk.removeLast();
53+
if (n == null) {
54+
continue;
55+
}
56+
if (n.val > low) {
57+
stk.add(n.left);
58+
} // left child is a possible candidate.
59+
if (n.val < high) {
60+
stk.add(n.right);
61+
} // right child is a possible candidate.
62+
if (low <= n.val && n.val <= high) {
63+
sum += n.val;
64+
}
65+
}
66+
return sum;
67+
}
68+
69+
*/

Diff for: RangeSumOfBST/range_sum_of_bst.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 🔥 Range Sum of BST 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Definition for a binary tree node
4+
5+
```dart
6+
class TreeNode {
7+
int val;
8+
TreeNode? left;
9+
TreeNode? right;
10+
TreeNode([this.val = 0, this.left, this.right]);
11+
}
12+
```
13+
14+
## Solution - 1
15+
16+
```dart
17+
class Solution {
18+
int rangeSumBST(TreeNode? root, int low, int high) {
19+
if (root == null) return 0; // base case.
20+
if (root.val < low)
21+
return rangeSumBST(root.right, low, high); // left branch excluded.
22+
if (root.val > high)
23+
return rangeSumBST(root.left, low, high); // right branch excluded.
24+
return root.val +
25+
rangeSumBST(root.right, low, high) +
26+
rangeSumBST(root.left, low, high); // count in both children.
27+
}
28+
}
29+
```
30+
31+
## Solution - 2
32+
33+
```dart
34+
class Solution {
35+
int rangeSumBST(TreeNode? root, int low, int high) {
36+
if (root == null) return 0; // base case.
37+
return (low <= root.val && root.val <= high ? root.val : 0) +
38+
rangeSumBST(root.right, low, high) +
39+
rangeSumBST(root.left, low, high);
40+
}
41+
}
42+
```
43+
44+
## Solution - 3
45+
46+
```dart
47+
class Solution {
48+
int rangeSumBST(TreeNode? root, int low, int high) {
49+
List<TreeNode?> stk = [];
50+
stk.add(root);
51+
int sum = 0;
52+
while (stk.isNotEmpty) {
53+
TreeNode? n = stk.removeLast();
54+
if (n == null) {
55+
continue;
56+
}
57+
if (n.val > low) {
58+
stk.add(n.left);
59+
} // left child is a possible candidate.
60+
if (n.val < high) {
61+
stk.add(n.right);
62+
} // right child is a possible candidate.
63+
if (low <= n.val && n.val <= high) {
64+
sum += n.val;
65+
}
66+
}
67+
return sum;
68+
}
69+
}
70+
```

0 commit comments

Comments
 (0)