|
| 1 | +# Burning Tree |
| 2 | +## Hard |
| 3 | +<div class="problems_problem_content__Xm_eO"><p><span style="font-size:18px">Given a binary tree and a <strong>node data</strong> called <strong>target</strong>. Find the minimum time required to burn the complete binary tree if the target is set on fire. It is known that in 1 second all nodes connected to a given node get burned. That is its left child, right child, and parent.<br> |
| 4 | +<strong>Note:</strong> The tree contains unique values.</span></p> |
| 5 | + |
| 6 | +<p><br> |
| 7 | +<strong><span style="font-size:18px">Example 1:</span></strong></p> |
| 8 | + |
| 9 | +<pre><span style="font-size:18px"><strong>Input: </strong> |
| 10 | + 1 |
| 11 | + / \ |
| 12 | + 2 3 |
| 13 | + / \ \ |
| 14 | + 4 5 6 |
| 15 | + / \ \ |
| 16 | + 7 8 9 |
| 17 | + \ |
| 18 | + 10</span> |
| 19 | +<span style="font-size:18px">Target Node = 8</span> |
| 20 | +<span style="font-size:18px"><strong>Output:</strong> 7</span> |
| 21 | +<span style="font-size:18px"><strong>Explanation:</strong> If leaf with the value |
| 22 | +8 is set on fire. |
| 23 | +After 1 sec: 5 is set on fire. |
| 24 | +After 2 sec: 2, 7 are set to fire. |
| 25 | +After 3 sec: 4, 1 are set to fire. |
| 26 | +After 4 sec: 3 is set to fire. |
| 27 | +After 5 sec: 6 is set to fire. |
| 28 | +After 6 sec: 9 is set to fire. |
| 29 | +After 7 sec: 10 is set to fire. |
| 30 | +It takes 7s to burn the complete tree.</span> |
| 31 | +</pre> |
| 32 | + |
| 33 | +<p><span style="font-size:18px"><strong>Example 2:</strong></span></p> |
| 34 | + |
| 35 | +<pre><span style="font-size:18px"><strong>Input:</strong> |
| 36 | + 1 |
| 37 | + / \ |
| 38 | + 2 3 |
| 39 | + / \ \ |
| 40 | + 4 5 7 |
| 41 | + / / |
| 42 | + 8 10</span> |
| 43 | +<span style="font-size:18px">Target Node = 10</span> |
| 44 | +<span style="font-size:18px"><strong>Output:</strong> 5</span> |
| 45 | +</pre> |
| 46 | + |
| 47 | +<p><br> |
| 48 | +<span style="font-size:18px"><strong>Your Task: </strong><br> |
| 49 | +You don't need to read input or print anything. Complete the function <strong>minTime()</strong> which takes the root of the tree and target as input parameters and returns the minimum time required to burn the complete binary tree if the target is set on fire at the 0th second.</span></p> |
| 50 | + |
| 51 | +<p><br> |
| 52 | +<span style="font-size:18px"><strong>Expected Time Complexity: </strong>O(N)<br> |
| 53 | +<strong>Expected Auxiliary Space: </strong>O(height of tree)</span></p> |
| 54 | + |
| 55 | +<p><br> |
| 56 | +<span style="font-size:18px"><strong>Constraints:</strong><br> |
| 57 | +1 ≤ N ≤ 10<sup>4</sup></span></p> |
| 58 | +</div> |
0 commit comments