|
| 1 | +// Burning tree |
| 2 | + |
| 3 | + |
| 4 | +#include <bits/stdc++.h> |
| 5 | +using namespace std; |
| 6 | + |
| 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 | + |
| 19 | +Node *buildTree (string str) { |
| 20 | + // Corner Case |
| 21 | + if (str.length() == 0 || str[0] == 'N') |
| 22 | + return NULL; |
| 23 | + |
| 24 | + // Creating vector of strings from input |
| 25 | + // string after spliting by space |
| 26 | + vector<string> ip; |
| 27 | + |
| 28 | + istringstream iss(str); |
| 29 | + for (string str; iss >> str;) |
| 30 | + ip.push_back(str); |
| 31 | + |
| 32 | + // Create the root of the tree |
| 33 | + Node *root = new Node (stoi (ip[0])); |
| 34 | + |
| 35 | + // Push the root to the queue |
| 36 | + queue<Node *> queue; |
| 37 | + queue.push(root); |
| 38 | + |
| 39 | + // Starting from the second element |
| 40 | + int i = 1; |
| 41 | + while (!queue.empty() && i < ip.size()) { |
| 42 | + |
| 43 | + // Get and remove the front of the queue |
| 44 | + Node *currNode = queue.front(); |
| 45 | + queue.pop(); |
| 46 | + |
| 47 | + // Get the current Node's value from the string |
| 48 | + string currVal = ip[i]; |
| 49 | + |
| 50 | + // If the left child is not null |
| 51 | + if (currVal != "N") { |
| 52 | + |
| 53 | + // Create the left child for the current Node |
| 54 | + currNode->left = new Node (stoi(currVal)); |
| 55 | + |
| 56 | + // Push it to the queue |
| 57 | + queue.push(currNode->left); |
| 58 | + } |
| 59 | + |
| 60 | + // For the right child |
| 61 | + i++; |
| 62 | + if (i >= ip.size()) |
| 63 | + break; |
| 64 | + currVal = ip[i]; |
| 65 | + |
| 66 | + // If the right child is not null |
| 67 | + if (currVal != "N") { |
| 68 | + |
| 69 | + // Create the right child for the current Node |
| 70 | + currNode->right = new Node (stoi(currVal)); |
| 71 | + |
| 72 | + // Push it to the queue |
| 73 | + queue.push(currNode->right); |
| 74 | + } |
| 75 | + i++; |
| 76 | + } |
| 77 | + |
| 78 | + return root; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +class Solution { |
| 86 | + public: |
| 87 | + Node* createParentMapping(Node* root, int target, map<Node*, Node*> &nodeToParent){ |
| 88 | + Node* res = NULL; |
| 89 | + |
| 90 | + queue<Node*> q; |
| 91 | + q.push(root); |
| 92 | + nodeToParent[root] = NULL; |
| 93 | + |
| 94 | + while(!q.empty()){ |
| 95 | + Node* front = q.front(); |
| 96 | + q.pop(); |
| 97 | + if(front->data == target){ |
| 98 | + res = front; |
| 99 | + } |
| 100 | + if(front->left){ |
| 101 | + nodeToParent[front->left] = front; |
| 102 | + q.push(front->left); |
| 103 | + } |
| 104 | + if(front->right){ |
| 105 | + nodeToParent[front->right] = front; |
| 106 | + q.push(front->right); |
| 107 | + } |
| 108 | + } |
| 109 | + return res; |
| 110 | + } |
| 111 | + |
| 112 | + int burnTree(Node* targetNode, map<Node*, Node*> nodeToParent){ |
| 113 | + map<Node*, bool> visited; |
| 114 | + queue<Node*> q; |
| 115 | + q.push(targetNode); |
| 116 | + visited[targetNode] = true; |
| 117 | + |
| 118 | + int ans = 0; |
| 119 | + |
| 120 | + while(!q.empty()){ |
| 121 | + bool flag = 0; |
| 122 | + int size = q.size(); |
| 123 | + for(int i=0; i<size; i++){ |
| 124 | + Node* front = q.front(); |
| 125 | + q.pop(); |
| 126 | + |
| 127 | + if(front->left && !visited[front->left]){ |
| 128 | + flag = 1; |
| 129 | + q.push(front->left); |
| 130 | + visited[front->left] = 1; |
| 131 | + } |
| 132 | + if(front->right && !visited[front->right]){ |
| 133 | + flag = 1; |
| 134 | + q.push(front->right); |
| 135 | + visited[front->right] = 1; |
| 136 | + } |
| 137 | + if(nodeToParent[front] && !visited[nodeToParent[front]]){ |
| 138 | + flag = 1; |
| 139 | + q.push(nodeToParent[front]); |
| 140 | + visited[nodeToParent[front]] = 1; |
| 141 | + } |
| 142 | + } |
| 143 | + if(flag){ |
| 144 | + ans++; |
| 145 | + } |
| 146 | + } |
| 147 | + return ans; |
| 148 | + } |
| 149 | + |
| 150 | + int minTime(Node* root, int target){ |
| 151 | + map<Node*, Node*> nodeToParent; |
| 152 | + Node* targetNode = createParentMapping(root, target, nodeToParent); |
| 153 | + |
| 154 | + int ans = burnTree(targetNode, nodeToParent); |
| 155 | + return ans; |
| 156 | + } |
| 157 | +}; |
| 158 | + |
| 159 | +int main() { |
| 160 | + int t; |
| 161 | + cin; |
| 162 | + while (t--) { |
| 163 | + string s; |
| 164 | + getline(cin, s); |
| 165 | + Node *root = buildTree(s); |
| 166 | + |
| 167 | + int target; |
| 168 | + cin >> target; |
| 169 | + Solution ob; |
| 170 | + cout << ob.minTime(root, target) << endl; |
| 171 | + } |
| 172 | + return 0; |
| 173 | +} |
| 174 | + |
| 175 | + |
0 commit comments