-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion_26.cpp
More file actions
99 lines (90 loc) · 2.08 KB
/
question_26.cpp
File metadata and controls
99 lines (90 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
*
* Starter file for:
*
* W22, Final Exam, Question 26
*
*
*
* Do not try to submit this file to the autograder!
*
* Upload it to Gradescope:
*
* [W22] Final Question 26
*
*
*
* Before uploading, as you "browse" for the file, check the last modified time!
*
* It should be when you last saved the file.
*
* Make sure you are uploading a file named question_26.cpp
*
*/
// DO NOT MODIFY ANYTHING ABOVE THIS LINE!
// You may use whatever you want, we just added some standard includes
// that might be useful for any question. If you want to use something
// else, make sure to #include it.
// Additional includes do not add to your line count.
#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// DO NOT MODIFY struct Node
struct Node {
double value;
Node *left;
Node *right;
Node(double v) : value{ v }, left{ nullptr }, right{ nullptr } {}
};
// DO NOT MODIFY the deallocate() function declaration; do not code it.
extern Node *deallocate(Node *);
// HINT: Depending on how you write the code, you might need helper functions here
map<double, int>mp;
double mod;
void calc(Node* cur) {
if (cur == nullptr)
return;
mp[cur->value]++;
calc(cur->left);
calc(cur->right);
}
Node* helper(Node* cur) {
if (cur == nullptr || cur ->value == mod)
return deallocate(cur);
helper(cur->left);
helper(cur->right);
swap(cur->left, cur->right);
cur->value = 1.0 / cur->value;
return cur;
}
Node *question_26(Node *root) {
// TODO: Write this function
calc(root);
int count = 0;
for (auto &el : mp) {
if (el.second > count) {
count = el.second;
mod = el.first;
}
}
return helper(root);
} // question_26()