#include using namespace std; class Node { public: int data; Node *left; Node *right; Node(int d) { data = d; left = NULL; right = NULL; } }; class Solution { public: Node* insert(Node* root, int data) { if(root == NULL) { return new Node(data); } else { Node* cur; if(data <= root->data) { cur = insert(root->left, data); root->left = cur; } else { cur = insert(root->right, data); root->right = cur; } return root; } } void free_tree(Node* root) { // Deallocates memory corresponding to every node in the tree. Node* temp = root; if (temp == nullptr) return; free_tree(temp->left); free_tree(temp->right); if (!temp->left && !temp->right) { free(temp); temp = nullptr; return; } } int height(Node *root) { if (root == NULL) return -1; else { int left = height(root->left); int right = height(root->right); return 1 + max(left, right); } } }; //End of Solution int main() { multimap,int>> testCases = { // n data height // - ------------- ------ { 7, {{3,5,2,1,4,6,7}, 3 }}, { 1, {{15}, 0 }}, { 5, {{3,1,7,5,4}, 3 }}, }; for (auto& tc: testCases ) { cout << "n: " << tc.first << " data: "; for (auto& y: get<0>(tc.second)) { cout << y << " "; } cout << endl; Solution myTree; Node* root = nullptr; for (auto& data: get<0>(tc.second)) { //cout << "insert " << data << endl; root = myTree.insert(root, data); //cout << "root: " << hex << root << dec << endl; } cout << "expected: " << get<1>(tc.second) << " actual: " << myTree.height(root) << endl; myTree.free_tree(root); } return 0; }