aboutsummaryrefslogtreecommitdiff
path: root/range_sum_bst.c
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /range_sum_bst.c
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'range_sum_bst.c')
-rw-r--r--range_sum_bst.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/range_sum_bst.c b/range_sum_bst.c
new file mode 100644
index 0000000..9db0643
--- /dev/null
+++ b/range_sum_bst.c
@@ -0,0 +1,25 @@
+// 938. Range Sum of BST
+#include "leetcode.h"
+
+/*
+ * given the 'root' 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)'
+ */
+
+struct TreeNode {
+ int val;
+ struct TreeNode *left;
+ struct TreeNode *right;
+};
+
+int rangeSumBST(struct TreeNode *root, int low, int high) {
+ if (!root)
+ return 0;
+ else if (root->val >= low && root->val <= high)
+ return root->val + rangeSumBST(root->left, low, high) +
+ rangeSumBST(root->right, low, high);
+ else
+ return rangeSumBST(root->left, low, high) +
+ rangeSumBST(root->right, low, high);
+}