1+ #include < iostream>
2+ #include < queue>
3+ #include < unordered_map>
4+ #include < unordered_set>
5+ using namespace std ;
6+
7+
8+ // Definition for a binary tree node.
9+ struct Node
10+ {
11+ int data;
12+ Node *left;
13+ Node *right;
14+ Node (int val)
15+ {
16+ data = val;
17+ left = NULL ;
18+ right = NULL ;
19+ }
20+ };
21+
22+ // Function to build the binary tree from user input
23+ // Input format: Enter node values in pre-order fashion. Use -1 to indicate NULL nodes
24+ Node *buildTree ()
25+ {
26+ int val;
27+ cin >> val;
28+ if (val == -1 )
29+ return NULL ;
30+ Node *root = new Node (val);
31+ root->left = buildTree ();
32+ root->right = buildTree ();
33+ return root;
34+ }
35+
36+ void markParent (Node *root, unordered_map<Node *, Node *> &parent_track)
37+ {
38+ queue<Node *> q;
39+ q.push (root);
40+ while (!q.empty ())
41+ {
42+ Node *curr = q.front ();
43+ q.pop ();
44+ if (curr->left )
45+ {
46+ parent_track[curr->left ] = curr;
47+ q.push (curr->left );
48+ }
49+ if (curr->right )
50+ {
51+ parent_track[curr->right ] = curr;
52+ q.push (curr->right );
53+ }
54+ }
55+ }
56+
57+ // Function to calculate the time to burn the binary tree from the target node
58+ int timeToBurnTree (Node *root, int target)
59+ {
60+ unordered_map<Node *, Node *> parent_track;
61+ markParent (root, parent_track);
62+ queue<Node *> q;
63+ unordered_set<Node *> visited;
64+ int time = 0 ;
65+ // find the target node
66+ Node *targetNode = NULL ;
67+ for (auto it : parent_track)
68+ {
69+ if (it.first ->data == target)
70+ {
71+ targetNode = it.first ;
72+ break ;
73+ }
74+ }
75+ if (targetNode == NULL )
76+ return -1 ; // target not found
77+ q.push (targetNode);
78+ visited.insert (targetNode);
79+ while (!q.empty ())
80+ {
81+ int size = q.size ();
82+ bool flag = false ; // to check if any new node is burned in this time unit
83+ for (int i = 0 ; i < size; i++)
84+ {
85+ Node *curr = q.front ();
86+ q.pop ();
87+ if (curr->left && visited.find (curr->left ) == visited.end ())
88+ {
89+ flag = true ;
90+ visited.insert (curr->left );
91+ q.push (curr->left );
92+ }
93+ if (curr->right && visited.find (curr->right ) == visited.end ())
94+ {
95+ flag = true ;
96+ visited.insert (curr->right );
97+ q.push (curr->right );
98+ }
99+ if (parent_track.find (curr) != parent_track.end () && visited.find (parent_track[curr]) == visited.end ())
100+ {
101+ flag = true ;
102+ visited.insert (parent_track[curr]);
103+ q.push (parent_track[curr]);
104+ }
105+ }
106+ if (flag)
107+ time++;
108+ }
109+ return time;
110+ }
111+
112+ // Input format for building the tree:
113+ // Enter node values in pre-order fashion. Use -1 to indicate NULL nodes.
114+ // Example: 1 2 4 -1 -1 5 -1 -1 3 -1 -1 (represents the tree below)
115+ // target = 2
116+ // time = 2 seconds
117+ // target = 3
118+ // time = 3 seconds
119+
120+ int main ()
121+ {
122+ cout << " Build the binary tree (use -1 for NULL nodes): " ;
123+ Node *root = buildTree ();
124+ cout << " Enter target node value: " ;
125+ int target;
126+ cin >> target;
127+ cout << " Time to burn the tree: " ;
128+ cout << timeToBurnTree (root, target) << endl;
129+ return 0 ;
130+ }
0 commit comments