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)


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