Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- #include <vector>
- #include <sstream>
- #include <algorithm>
- #include <stdexcept>
- struct Node {
- int val;
- Node * left;
- Node * right;
- Node(int x): val(x), left(NULL), right(NULL) {}
- };
- /* For testing purposes */
- void preOrder(Node * root) {
- if (root == NULL) {
- return;
- }
- cout << root->val << " ";
- preOrder(root->left);
- preOrder(root->right);
- }
- Node* insert(Node * root, int value) {
- // Assumption: If node values are equal, push it to the left side of the tree
- if (root == NULL) {
- return new Node(value);
- }
- if (root->val < value) {
- root->right = insert(root->right, value);
- } else {
- root->left = insert(root->left, value);
- }
- return root;
- }
- Node* createBST(const vector < int > & values) {
- if (values.size() == 0) {
- return NULL;
- }
- Node * root = new Node(values[0]);
- //Insert the rest of the values
- for (int i = 1; i < values.size(); ++i) {
- insert(root, values[i]);
- }
- return root;
- }
- int findDistanceToValue(Node * root, int value) {
- if (root->val == value) {
- return 0;
- }
- if (root->val > value) {
- return 1 + findDistanceToValue(root->left, value);
- } else {
- return 1 + findDistanceToValue(root->right, value);
- }
- }
- int findDistanceBetweenTwoValues(Node * root, int key_1, int key_2) {
- if (key_1 > root->val && key_2 > root->val) {
- return findDistanceBetweenTwoValues(root->right, key_1, key_2);
- }
- if (root->val > key_1 && root->val > key_2) {
- return findDistanceBetweenTwoValues(root->left, key_1, key_2);
- }
- //Else we have our least common ancestor at the root, so add up distance to each value
- return findDistanceToValue(root, key_1) + findDistanceToValue(root,
- key_2);
- }
- //Notes:
- //I assumed "in order" in the problem spec to mean that we can't sort the array and that
- //we should try to create the BST in the order that the values were given
- //Thus, I set root as the first element in the array in createBST() and continue inserting elements.
- int main(int argc, char ** argv) {
- //Process input
- vector<string>inputs;
- string line; //temporary buffer
- while (std::getline(std::cin, line)) {
- inputs.push_back(line);
- }
- if (inputs.size() != 3) {
- throw std::runtime_error("Input is not valid, must be 3 lines of integers \n");
- }
- /* Store keys we are searching for */
- vector<int> keys_to_find;
- keys_to_find.push_back(stoi(inputs[0]));
- keys_to_find.push_back(stoi(inputs[1]));
- int found_entered_keys = 0;
- vector<int> values;
- stringstream ss(inputs[2]); // Insert the string of values for the tree into a stream
- line = "";
- while (ss >> line){
- int new_key = stoi(line);
- values.push_back(new_key);
- if(new_key == keys_to_find[0] || new_key == keys_to_find[1]) found_entered_keys += 1; //Ensure we find both keys we are searching for
- }
- if(found_entered_keys < 2){
- // throw std::runtime_error("Entered Keys are not in BST \n");
- cout << "Not found!\n";
- return 0;
- }
- Node * root = createBST(values);
- cout << "The distance between the two nodes: " << (findDistanceBetweenTwoValues(root, keys_to_find[0], keys_to_find[1])) << "\n";
- }
Add Comment
Please, Sign In to add comment