Skip to content

Latest commit

 

History

History
27 lines (22 loc) · 852 Bytes

938_rangeSumOfBST.md

File metadata and controls

27 lines (22 loc) · 852 Bytes

Traversal Approaches

  • We can solve this problem with any of the traversal for the tree.
  • We just need to check for the condition of the node value and then add the value to the sum.

BST Traversal

  • We can use BST properties and avoid extra recursive calls.
  • TC: O(N), N is number of nodes in the tree.
  • SC: O(h), h is the height of the tree.
class Solution {
public:
    int rangeSumBST(TreeNode* root, int low, int high)
    {
        if (root == NULL) return 0;
        int sum = 0;
        if (root->val >= low && root->val <= high) sum += root->val;
        if (root->val >= low) sum += rangeSumBST(root->left, low, high);
        if (root->val <= high) sum += rangeSumBST(root->right, low, high);
        return sum;
    }
};