Advertisement
Skos_Martren

Untitled

Sep 22nd, 2023
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. const int ZERO = [](){
  2.  
  3.     ios_base::sync_with_stdio(false);
  4.     cin.tie(nullptr);
  5.    
  6.     return 0;
  7. }();
  8.  
  9. class Solution {
  10.  
  11.     private:
  12.  
  13.     int ans = 0;
  14. public:
  15.  
  16.     void Inorder(TreeNode* root, int low, int high){
  17.  
  18.         if(root == nullptr){return;}
  19.  
  20.         Inorder(root->left, low, high);
  21.  
  22.         if(low<= root->val && root->val <= high){ans+= root->val;}
  23.         if(root->val > high){return;} // эта строка учитывает информацию о том, что дано BST -- при inorder обходе значения в узлах образуют возрастающую последовательность
  24.  
  25.         Inorder(root->right, low, high);
  26.     }
  27.  
  28.     int rangeSumBST(TreeNode* root, int low, int high) {
  29.        
  30.         Inorder(root, low, high);
  31.         return ans;
  32.     }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement