Skip to content

Commit 31d5998

Browse files
committed
Added solution - LeetHub
1 parent baed4a7 commit 31d5998

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

Burning Tree - GFG/burning-tree.cpp

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
//{ Driver Code Starts
2+
//Initial Template for C++
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+
// } Driver Code Ends
83+
//User function Template for C++
84+
85+
/*
86+
struct Node {
87+
int data;
88+
Node *left;
89+
Node *right;
90+
91+
Node(int val) {
92+
data = val;
93+
left = right = NULL;
94+
}
95+
};
96+
*/
97+
class Solution {
98+
public:
99+
// Creates the map and returns the target Node
100+
Node* createParentMapping(Node* root, int target, map<Node*, Node*> &nodeToParent)
101+
{
102+
Node* ans = NULL;
103+
queue<Node*>q;
104+
q.push(root);
105+
nodeToParent[root]=NULL;
106+
107+
while(!q.empty())
108+
{
109+
Node* front=q.front();
110+
q.pop();
111+
if(front->data == target)
112+
ans=front;
113+
114+
if(front->left)
115+
{
116+
nodeToParent[front->left]=front;
117+
q.push(front->left);
118+
}
119+
if(front->right)
120+
{
121+
nodeToParent[front->right]=front;
122+
q.push(front->right);
123+
}
124+
}
125+
return ans;
126+
}
127+
128+
int burnTree(Node* root, map<Node*, Node*>nodeToParent)
129+
{
130+
map<Node*, bool> visited;
131+
queue<Node*>q;
132+
133+
q.push(root);
134+
visited[root]=true;
135+
136+
int ans=0;
137+
while(!q.empty())
138+
{
139+
bool flag=0;
140+
int size=q.size();
141+
for(int i=0; i<size; i++)
142+
{
143+
// Process the neighboring node
144+
Node* front=q.front();
145+
q.pop();
146+
if(front->left && !visited[front->left])
147+
{
148+
flag = 1;
149+
q.push(front->left);
150+
visited[front->left]=1;
151+
}
152+
153+
if(front->right && !visited[front->right])
154+
{
155+
flag = 1;
156+
q.push(front->right);
157+
visited[front->right]=1;
158+
}
159+
160+
if(nodeToParent[front] && !visited[nodeToParent[front]])
161+
{
162+
flag = 1;
163+
q.push(nodeToParent[front]);
164+
visited[nodeToParent[front]]=1;
165+
}
166+
}
167+
if(flag)
168+
ans++;
169+
}
170+
return ans;
171+
}
172+
int minTime(Node* root, int target)
173+
{
174+
// Algorithm:
175+
// Step1: create a nodeToParent mapping
176+
// Step2: find the target node
177+
// Step3: burn the tree in min time
178+
179+
int ans=0;
180+
map<Node*, Node*>nodeToParent;
181+
Node* targetNode = createParentMapping(root, target, nodeToParent);
182+
183+
ans = burnTree(targetNode, nodeToParent);
184+
185+
return ans;
186+
}
187+
};
188+
189+
//{ Driver Code Starts.
190+
191+
int main()
192+
{
193+
int tc;
194+
scanf("%d ", &tc);
195+
while (tc--)
196+
{
197+
string treeString;
198+
getline(cin, treeString);
199+
// cout<<treeString<<"\n";
200+
int target;
201+
cin>>target;
202+
// cout<<target<<"\n";
203+
204+
Node *root = buildTree(treeString);
205+
Solution obj;
206+
cout<<obj.minTime(root, target)<<"\n";
207+
208+
cin.ignore();
209+
210+
}
211+
212+
213+
return 0;
214+
}
215+
216+
// } Driver Code Ends

0 commit comments

Comments
 (0)