commit 527c5d5a74094132209859e0f751b87ceffe945c
parent 965e835300efb747234b5e2f4894f86127305ac5
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date: Sat, 13 May 2023 09:53:41 -0500
Completed range sum of bst using dart and BFS with inorder traversal
Diffstat:
1 file changed, 21 insertions(+), 0 deletions(-)
diff --git a/range-sum-of-bst/range-sum-of-bst.dart b/range-sum-of-bst/range-sum-of-bst.dart
@@ -0,0 +1,21 @@
+//Take in three inputs where the first is the root of a tree, the second is the lowest value to add, and
+//the third is the highest value to add. Given this traverse the tree and return the sum of all values
+//between low and high.
+//The time complexity of this code is O(n) where n is the number of nodes.
+//My solution uses recursion and an inorder BFS algorithm.
+//Time: 295ms Beats: 80%
+//Memory: 164.7MB Beats: 10%
+class Solution {
+
+ int rangeSumBST(TreeNode? root, int low, int high) {
+ if(root== null){
+ return 0;
+ }
+ int sum = rangeSumBST(root.left, low, high);
+ sum += rangeSumBST(root.right, low, high);
+ if((root.val) >= low && (root.val) <= high){
+ sum += (root.val);
+ }
+ return sum;
+ }
+}