Advertisement
CR7CR7

mean

Aug 27th, 2023
1,665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int mean(Node* root, int key) {
  4.         Node* less = NULL;
  5.         Node* greater = NULL;
  6.        
  7.         while (root != NULL) {
  8.             if (root->data == key) {
  9.                 less = root;
  10.                 greater = root;
  11.                 break;
  12.             }
  13.             else if (root->data > key) {
  14.                 greater = root;
  15.                 root = root->left;
  16.             }
  17.             else {
  18.                 less = root;
  19.                 root = root->right;
  20.             }
  21.         }
  22.        
  23.         int lessVal = (less != NULL) ? less->data : -1;
  24.         int greaterVal = (greater != NULL) ? greater->data : -1;
  25.        
  26.         return ceil((lessVal + greaterVal) / 2.0);
  27.     }
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement