|
| 1 | +// { Driver Code Starts |
| 2 | +#include <bits/stdc++.h> |
| 3 | + |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +// Tree Node |
| 7 | +struct Node { |
| 8 | + int data; |
| 9 | + Node *left; |
| 10 | + Node *right; |
| 11 | + |
| 12 | + Node(int val) { |
| 13 | + data = val; |
| 14 | + left = right = NULL; |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +// Function to Build Tree |
| 19 | +Node *buildTree(string str) { |
| 20 | + // Corner Case |
| 21 | + if (str.length() == 0 || str[0] == 'N') return NULL; |
| 22 | + |
| 23 | + // Creating vector of strings from input |
| 24 | + // string after spliting by space |
| 25 | + vector<string> ip; |
| 26 | + |
| 27 | + istringstream iss(str); |
| 28 | + for (string str; iss >> str;) ip.push_back(str); |
| 29 | + |
| 30 | + // Create the root of the tree |
| 31 | + Node *root = new Node(stoi(ip[0])); |
| 32 | + |
| 33 | + // Push the root to the queue |
| 34 | + queue<Node *> queue; |
| 35 | + queue.push(root); |
| 36 | + |
| 37 | + // Starting from the second element |
| 38 | + int i = 1; |
| 39 | + while (!queue.empty() && i < ip.size()) { |
| 40 | + |
| 41 | + // Get and remove the front of the queue |
| 42 | + Node *currNode = queue.front(); |
| 43 | + queue.pop(); |
| 44 | + |
| 45 | + // Get the current Node's value from the string |
| 46 | + string currVal = ip[i]; |
| 47 | + |
| 48 | + // If the left child is not null |
| 49 | + if (currVal != "N") { |
| 50 | + |
| 51 | + // Create the left child for the current Node |
| 52 | + currNode->left = new Node(stoi(currVal)); |
| 53 | + |
| 54 | + // Push it to the queue |
| 55 | + queue.push(currNode->left); |
| 56 | + } |
| 57 | + |
| 58 | + // For the right child |
| 59 | + i++; |
| 60 | + if (i >= ip.size()) break; |
| 61 | + currVal = ip[i]; |
| 62 | + |
| 63 | + // If the right child is not null |
| 64 | + if (currVal != "N") { |
| 65 | + |
| 66 | + // Create the right child for the current Node |
| 67 | + currNode->right = new Node(stoi(currVal)); |
| 68 | + |
| 69 | + // Push it to the queue |
| 70 | + queue.push(currNode->right); |
| 71 | + } |
| 72 | + i++; |
| 73 | + } |
| 74 | + |
| 75 | + return root; |
| 76 | +} |
| 77 | + |
| 78 | +int findDist(Node *, int, int); |
| 79 | + |
| 80 | +int main() { |
| 81 | + int tc; |
| 82 | + scanf("%d ", &tc); |
| 83 | + while (tc--) { |
| 84 | + string treeString; |
| 85 | + getline(cin, treeString); |
| 86 | + Node *root = buildTree(treeString); |
| 87 | + int n1, n2; |
| 88 | + scanf("%d %d ", &n1, &n2); |
| 89 | + cout << findDist(root, n1, n2) << "\n"; |
| 90 | + } |
| 91 | + return 0; |
| 92 | +}// } Driver Code Ends |
| 93 | + |
| 94 | + |
| 95 | +/* A binary tree node |
| 96 | +struct Node |
| 97 | +{ |
| 98 | + int data; |
| 99 | + Node* left, * right; |
| 100 | +}; */ |
| 101 | + |
| 102 | +/* Should return minimum distance between a and b |
| 103 | + in a tree with given root*/ |
| 104 | +Node* lca(Node* root ,int n1 ,int n2 ) // to find lowest common anccesstor |
| 105 | +{ |
| 106 | + if(!root) |
| 107 | + return NULL; |
| 108 | + |
| 109 | + if(root->data == n1 || root->data == n2) { |
| 110 | + return root; |
| 111 | + } |
| 112 | + Node* l = lca(root->left, n1, n2); |
| 113 | + Node* r = lca(root->right, n1, n2); |
| 114 | + |
| 115 | + if(l &&r ) |
| 116 | + return root; |
| 117 | + return l?l:r; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +int findDistanceOfNodeFromRoot(Node *root, int x) { |
| 122 | + if (root == NULL) |
| 123 | + return -1; |
| 124 | + if(root->data == x) |
| 125 | + return 0; |
| 126 | + |
| 127 | + int distl = -1; |
| 128 | + int distr = -1; |
| 129 | + distl = findDistanceOfNodeFromRoot(root->left, x); |
| 130 | + distr = findDistanceOfNodeFromRoot(root->right, x); |
| 131 | + if(distl >= 0) |
| 132 | + return distl + 1; |
| 133 | + if(distr >= 0) |
| 134 | + return distr + 1; |
| 135 | + return -1; |
| 136 | +} |
| 137 | +/* Should return minimum distance between a and b |
| 138 | +in a tree with given root*/ |
| 139 | +int findDist(Node* root, int a, int b) { |
| 140 | + Node* lowestCommonAnccesstor = lca(root,a,b); |
| 141 | + int aDistance = findDistanceOfNodeFromRoot(root,a); |
| 142 | + int bDistance = findDistanceOfNodeFromRoot(root,b); |
| 143 | + int ancDistance = findDistanceOfNodeFromRoot(root,lowestCommonAnccesstor->data); |
| 144 | + return aDistance + bDistance - (2*ancDistance); |
| 145 | +} |
0 commit comments