leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0938.cpp (618B)


      1 class Solution {
      2   public:
      3     Solution() {
      4         ios_base::sync_with_stdio(false);
      5         cin.tie(nullptr);
      6         cout.tie(nullptr);
      7     }
      8     int rangeSumBST(TreeNode *root, int low, int high) {
      9         int sum = 0;
     10         stack<TreeNode *> st;
     11         st.push(root);
     12         while (!st.empty()) {
     13             TreeNode *root = st.top();
     14             st.pop();
     15             if (root->val >= low && root->val <= high) sum += root->val;
     16             if (root->left && root->val > low) st.push(root->left);
     17             if (root->right && root->val < high) st.push(root->right);
     18         }
     19         return sum;
     20     }
     21 };