Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int mean(Node* root, int key) {
- Node* less = NULL;
- Node* greater = NULL;
- while (root != NULL) {
- if (root->data == key) {
- less = root;
- greater = root;
- break;
- }
- else if (root->data > key) {
- greater = root;
- root = root->left;
- }
- else {
- less = root;
- root = root->right;
- }
- }
- int lessVal = (less != NULL) ? less->data : -1;
- int greaterVal = (greater != NULL) ? greater->data : -1;
- return ceil((lessVal + greaterVal) / 2.0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement