id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(std::vector<int>& happiness, int k) {\n std::sort(happiness.begin(), happiness.end(), std::greater<int>());\n\n std::vector<bool> selected(happiness.size(), false);\n\n long long ans = 0;\n int count = 0;\n\n for (int i = 0; i < happiness.size(); ++i) {\n if (!selected[i]) {\n ans += std::max(0, happiness[i] - count);\n ++count;\n selected[i] = true;\n\n if (count >= k) {\n break;\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "108300"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long maximumHappinessSum(vector<int>& h, int k) {\n int n=h.size();\n sort(h.rbegin(),h.rend());\n int cnt=0;\n ll ans=0;\n for(int i=0;i<n && k>0;i++,k--){\n if(h[i]-cnt>0){\n ans+=(h[i]-cnt)*1ll;\n cnt++;\n }else{\n break;\n }\n }\n return ans;\n }\n};",
"memory": "108800"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& h, int k) {\n sort(h.rbegin(), h.rend());\n long long sum = 0;\n for (int i=0; i<h.size() && k--; i++){\n sum+=((h[i]-i>0) ? h[i]-i: 0);\n }\n return sum;\n }\n};",
"memory": "108900"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nlong long max(long long a,long long b) {\n return a>b?a:b;\n}\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n sort(happiness.rbegin(),happiness.rend());\n long long ans=0, cnt=0;\n for(int i=0;i<k;i++){\n ans+=max(happiness[i]-cnt,0);cnt++;\n }\n return ans;\n }\n};",
"memory": "109000"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "\n#define endl '\\n'\n#define pint pair<int, int>\n#define vint vector<int>\n#define vll vector<long long>\n#define vpint vector<pair<int, int>>\n#define vstr vector<string>\n#define uset unordered_set\n#define umap unordered_map\n#define vbool vector<bool>\n#define vvint vector<vector<int>>\n#define vvvint vector<vector<vector<int>>>\n#define ll long long\n#define MOD 1000000007\n\nclass Solution {\npublic:\n ll maximumHappinessSum(vint& happiness, int k) {\n int n = happiness.size();\n\n vint h = happiness;\n sort(h.begin(), h.end(), greater<int>());\n\n ll ans = h[0];\n\n int pos = 1;\n while(pos < n & k - 1 > 0) {\n ans += (h[pos] - pos > 0 ? h[pos] - pos : 0);\n pos++;\n k--;\n }\n\n return ans;\n }\n};",
"memory": "111700"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n int n=happiness.size();\n sort(happiness.begin(),happiness.end());\n vector<int> diff(n,0);\n for(int i=n-1;i>=0;i--){\n diff[i]=(-1)*(n-i-1);\n cout<<diff[i]<<endl;\n }\n long long ans=0;\n int ind=n-1;\n while(k){\n long long res=happiness[ind]+diff[ind];\n if(res>0){\n ans+=res;\n }\n k--;\n ind--;\n }\n return ans;\n }\n};",
"memory": "111800"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n int n=happiness.size();\n priority_queue<int, vector<int>, greater<int>> pq(happiness.begin(), happiness.begin()+k);//minheap\n for(int i=k; i<n; i++){\n pq.push(happiness[i]);\n pq.pop();\n }\n long long sum=0;\n for(int i=k-1; i>=0; i--){\n long long x=max(0, pq.top()-i);\n // cout<<x<<endl;\n pq.pop();\n sum+=x;\n }\n return sum;\n \n }\n};",
"memory": "112200"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n int n=happiness.size();\n sort(happiness.begin(),happiness.end());\n vector<int>temp;\n int pnt=0;\n for(int i=n-1;i>=n-k;i--){\n if(happiness[i]-pnt >=0){\n temp.push_back(happiness[i]-pnt);\n }\n else{\n temp.push_back(0);\n }\n pnt++;\n }\n long long sum=0;\n for(int i=0;i<temp.size();i++){\n sum+=temp[i];\n }\n return sum;\n\n }\n};",
"memory": "113100"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<int, vector<int>, greater<int>> pq;\n for(auto num:happiness){\n pq.push(num);\n if(pq.size()>k) pq.pop();\n }\n long long ans=0;\n while(!pq.empty()){\n int diff = pq.top()-k+1;\n pq.pop();\n ans += diff>=0?diff:0;\n k--;\n }\n return ans;\n }\n};",
"memory": "113300"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<int, vector<int>, greater<int>> pq;\n for(auto num:happiness){\n pq.push(num);\n if(pq.size()>k) pq.pop();\n }\n long long ans=0;\n while(!pq.empty()){\n int diff = pq.top()-k+1;\n pq.pop();\n ans += diff>=0?diff:0;\n k--;\n }\n return ans;\n }\n};",
"memory": "113300"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<int, vector<int>, greater<int>> pq;\n for(auto& h: happiness)\n {\n if(pq.size() == k)\n {\n if(h>pq.top())\n {\n pq.push(h);\n pq.pop();\n }\n \n }\n else \n {\n pq.push(h);\n }\n }\n long long int ans=0;\n while(k)\n {\n ans+=max(pq.top()-k+1,0);\n pq.pop();\n k--;\n }\n return ans;\n }\n};",
"memory": "113400"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& h, int k) {\n long long res = 0;\n int count = 0;\n sort(h.begin(),h.end());\n queue<int>q;\n int n = h.size()-1;\n for(int i=n; i>=0; i--){\n q.push(h[i]);\n }\n while(!q.empty()){\n if(k==0) break;\n if(q.front() - count > 0){\n res += q.front()-count;\n }\n else {\n res += 0;\n }\n q.pop();\n count+=1;\n k-=1;\n }\n return res;\n }\n};",
"memory": "113500"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n \n sort(happiness.begin(), happiness.end());\n int n = happiness.size();\n int count = 0;\n long long m = 0;\n long long i =0;\n std::stack<int> s;\n\n for (int i = 0; i < n; i++) {\n s.push(happiness[i]);\n }\n\n while (k != 0 && !s.empty()) {\n if (s.top() -i >= 0) {\n m += s.top() - i;\n }\n s.pop();\n k--;\n i++;\n }\n // while (k != 0 && n != 0) {\n // for (int i = 0; i < n - 1; i++) {\n // m = happiness[n - 1];\n // if (happiness[i] - 1 >= 0) {\n // happiness[i] -= 1;\n // }\n // }\n // count += m;\n // happiness.pop_back();\n // k--;\n // n = happiness.size();\n // }\n\n return m;\n }\n};",
"memory": "113800"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<long long>pq(happiness.begin(),happiness.end());\n long long sum = pq.top();\n pq.pop();\n k--;\n int t=1;\n while(!pq.empty() && k){\n long long val =pq.top();\n pq.pop();\n if(val-t>0){\n sum+=val-t;\n }\n t++;\n k--;\n }\n cout<<sum;\n return sum;\n \n }\n};",
"memory": "115300"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<long long> q(happiness.begin(), happiness.end());\n int i = 0;\n long long sum = 0;\n while(i < k){\n int num = q.top();\n q.pop();\n if(num - i < 0) num = 0;\n else num -= i;\n sum += num;\n i++;\n }\n return sum;\n }\n};",
"memory": "115600"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& nums, int k) {\n priority_queue<int>q;\n long long ans=0;\n \n for(int i=0; i<nums.size(); i++) q.push(nums[i]);\n int size=0;\n \n while(!q.empty() && size<k){\n ans+=max(0, q.top() - size);\n q.pop();\n size++;\n }\n \n return ans;\n }\n};",
"memory": "117100"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<int>maxHeap;\n\n for(int i=0 ; i<happiness.size() ; i++){\n maxHeap.push(happiness[i]);\n }\n int i=0;\n long long ans = 0;\n while(k--){\n if(maxHeap.top()-i>=0){\n int temp = maxHeap.top()-i;\n maxHeap.pop();\n ans+=temp;\n i++;\n }\n else{\n break;\n }\n }\n return ans;\n }\n};",
"memory": "117200"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<int> pq;\n long long count = 0;\n long long ans = 0;\n for(auto it:happiness)\n {\n pq.push(it);\n }\n while(k--)\n {\n ans+=max(0ll,(long long)pq.top()-count);\n pq.pop();\n count++;\n }\n return ans;\n \n }\n};",
"memory": "117300"
} |
3,351 | <p>You are given an array <code>happiness</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p>
<p>There are <code>n</code> children standing in a queue, where the <code>i<sup>th</sup></code> child has <strong>happiness value</strong> <code>happiness[i]</code>. You want to select <code>k</code> children from these <code>n</code> children in <code>k</code> turns.</p>
<p>In each turn, when you select a child, the <strong>happiness value</strong> of all the children that have <strong>not</strong> been selected till now decreases by <code>1</code>. Note that the happiness value <strong>cannot</strong> become negative and gets decremented <strong>only</strong> if it is positive.</p>
<p>Return <em>the <strong>maximum</strong> sum of the happiness values of the selected children you can achieve by selecting </em><code>k</code> <em>children</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,2,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick the child with the happiness value == 3. The happiness value of the remaining children becomes [0,1].
- Pick the child with the happiness value == 1. The happiness value of the remaining child becomes [0]. Note that the happiness value cannot become less than 0.
The sum of the happiness values of the selected children is 3 + 1 = 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> happiness = [1,1,1,1], k = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can pick 2 children in the following way:
- Pick any child with the happiness value == 1. The happiness value of the remaining children becomes [0,0,0].
- Pick the child with the happiness value == 0. The happiness value of the remaining child becomes [0,0].
The sum of the happiness values of the selected children is 1 + 0 = 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> happiness = [2,3,4,5], k = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> We can pick 1 child in the following way:
- Pick the child with the happiness value == 5. The happiness value of the remaining children becomes [1,2,3].
The sum of the happiness values of the selected children is 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n == happiness.length <= 2 * 10<sup>5</sup></code></li>
<li><code>1 <= happiness[i] <= 10<sup>8</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maximumHappinessSum(vector<int>& happiness, int k) {\n priority_queue<int> pq;\n\n for(auto i:happiness){\n pq.push(i);\n }\n\n int a = 0;\n long long sum = 0;\n while(a<k){\n if(pq.top()<a)\n return sum;\n sum += pq.top()-a;\n pq.pop();\n a++;\n }\n\n return sum;\n }\n};",
"memory": "117300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "static int io_opt = []() {\n std::ios::sync_with_stdio(false);\n return 0;\n}();\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\n\nenum {\n UNKNOWN,\n HAS_START_NODE,\n};\n\nclass Solution {\n public:\n int amountOfTime(TreeNode* root, int start) {\n ans_ = 0;\n search(root, start);\n\n return ans_;\n }\n\n protected:\n // <status, path>\n pair<int, int> search(TreeNode* node, int start) {\n if (nullptr == node) return {UNKNOWN, -1};\n auto ld = search(node->left, start);\n auto rd = search(node->right, start);\n if (start == node->val) {\n ans_ = max(ans_, max(ld.second, rd.second) + 1);\n\n return {HAS_START_NODE, 0};\n }\n if (ld.first == HAS_START_NODE) {\n ans_ = max(ans_, ld.second + rd.second + 2);\n return {HAS_START_NODE, ld.second + 1};\n } else if (rd.first == HAS_START_NODE) {\n ans_ = max(ans_, ld.second + rd.second + 2);\n return {HAS_START_NODE, rd.second + 1};\n }\n\n return {UNKNOWN, max(ld.second, rd.second) + 1};\n }\n\n private:\n int ans_;\n};",
"memory": "93900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n // Approach 1 using parent map\n\n // int amountOfTime(TreeNode* root, int start) \n // {\n // if(root==NULL)\n // {\n // return 0;\n // } \n // map<TreeNode*,TreeNode*> parent;\n // queue<TreeNode*>q;\n // q.push(root);\n // TreeNode* Node;\n // while(!q.empty())\n // {\n // auto temp = q.front();\n // q.pop();\n // if(temp->val==start)\n // {\n // Node=temp;\n // }\n // if(temp->left!=NULL)\n // {\n // parent[temp->left]=temp;\n // q.push(temp->left);\n // }\n // if(temp->right!=NULL)\n // {\n // parent[temp->right]=temp;\n // q.push(temp->right);\n // }\n // }\n \n // queue<TreeNode*>q1;\n // q1.push(Node);\n // map<TreeNode*,int> vis;\n // vis[Node]=1;\n // int ans=0;\n // while(!q1.empty())\n // {\n // int n = q1.size();\n // int change=0;\n // for(int i=0;i<n;i++)\n // {\n // auto temp = q1.front();\n // q1.pop();\n\n // if(temp->left && vis[temp->left]==0)\n // {\n // q1.push(temp->left);\n // vis[temp->left]=1;\n // change++;\n // }\n // if(temp->right && vis[temp->right]==0)\n // {\n // q1.push(temp->right);\n // vis[temp->right]=1;\n // change++;\n // }\n // if(parent[temp] && vis[parent[temp]]==0)\n // {\n // q1.push(parent[temp]);\n // vis[parent[temp]]=1;\n // change++; \n // }\n // }\n // if(change) ans++;\n // }\n // return ans;\n // }\n\n // Approach 2 by doing something in height\n\n int maxi = 0;\n int solve(TreeNode* root, int start)\n {\n if(root==NULL) return 0;\n \n int lh = solve(root->left,start);\n int rh = solve(root->right,start);\n\n if(root->val == start)\n {\n maxi = max(lh,rh);\n return -1;\n }\n else if(lh>=0 && rh>=0)\n {\n return max(lh,rh)+1;\n }\n else \n {\n int d = abs(lh) + abs(rh);\n maxi = max(maxi,d);\n return min(lh,rh)-1;\n }\n //return 0;\n }\n\n int amountOfTime(TreeNode* root, int start) \n {\n solve(root,start);\n return maxi;\n }\n};",
"memory": "93900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int maxTime = INT_MIN;\n int amountOfTime(TreeNode* root, int start) {\n find(root, start);\n return maxTime;\n }\n\n void burnTree(TreeNode* root, TreeNode* block, int time) {\n if(root == nullptr || root == block) {\n return;\n }\n maxTime = max(maxTime, time);\n burnTree(root->left, block, time+1);\n burnTree(root->right, block, time+1);\n }\n\n int find (TreeNode* root, int start) {\n if(root == nullptr) return -1;\n if(root-> val == start) {\n burnTree(root, nullptr, 0);\n return 1;\n }\n int l = find(root->left, start);\n if(l != -1) {\n burnTree(root, root->left, l);\n return l+1;\n }\n int r = find(root->right, start);\n if(r != -1) {\n burnTree(root, root->right, r);\n return r+1;\n }\n return -1;\n }\n};",
"memory": "96700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n pair<bool,int> dfs(TreeNode *node, int start, int &res) {\n if (!node) return {false, 0}; \n\n auto [foundLeft, depthLeft] = dfs(node->left, start, res); \n auto [foundRight, depthRight] = dfs(node->right, start, res); \n int cur = max(depthLeft, depthRight); \n\n if (node->val == start) {\n res = max(res, cur); \n return {true, 0}; \n }\n\n if (foundLeft) {\n int tot = depthLeft + depthRight + 1; \n res = max(res, tot); \n return {true, depthLeft + 1}; \n } else if (foundRight) {\n int tot = depthLeft + depthRight + 1; \n res = max(res, tot); \n return {true, depthRight + 1}; \n }\n\n res = max(res, cur); \n return {false, cur + 1}; \n }\n\n int amountOfTime(TreeNode* root, int start) {\n int res = 0; \n dfs(root, start, res); \n return res; \n }\n};",
"memory": "96700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* target = nullptr;\n int maxTime = 0;\n unordered_set<TreeNode*> visit;\n stack<TreeNode*> path;\n void findStartNode(TreeNode* node, int start, stack<TreeNode*> &path) {\n if (!node) return;\n if (node->val == start) {\n target = node;\n return;\n }\n path.push(node);\n findStartNode(node->left, start, path);\n if (target) return;\n findStartNode(node->right, start, path);\n if (target) return;\n path.pop();\n }\n\n bool infect(TreeNode* node, int start, int &time) {\n if (!node) return false;\n bool foundHere = false;\n bool foundLeft = false;\n bool foundRight = false;\n if (target) {\n time++;\n } else if (node->val == start) {\n cout << \"Found target\" <<endl;\n time = 0;\n target = node;\n foundHere = true;\n }\n cout << \"Node: \"<<node->val << \" time: \"<<time<<endl;\n maxTime = max(maxTime, time);\n foundLeft = infect(node->left, start, time);\n if (foundLeft) time++;\n //maxTime = max(maxTime, time);\n foundRight = infect(node->right, start, time);\n if (foundRight) time++;\n maxTime = max(maxTime, time);\n if (foundRight) {\n //time++;\n infect(node->left, start, time);\n }\n if (!foundLeft && !foundRight && !foundHere && target) {\n time--;\n }\n \n return foundHere || foundLeft || foundRight;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n target = nullptr;\n int time = -1;\n infect(root, start, time);\n return maxTime;\n }\n};",
"memory": "99500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int ans=0;\npublic:\n pair<int,int> func(TreeNode* root,int start){\n\n if(root==NULL){\n return {-1,0};\n }\n pair<int,int> left=func(root->left,start);\n pair<int,int> right=func(root->right,start);\n cout<<left.second<<\" \"<<right.second<<endl;\n if(root->val==start){\n ans=max(ans,max(left.second,right.second));\n cout<<ans<<endl;\n return {1,1};\n }\n if(left.first==1){\n ans=max(ans,left.second+right.second);\n return {1,1+left.second};\n }\n if(right.first==1){\n ans=max(ans,left.second+right.second);\n return {1,1+right.second};\n }\n return {-1,1+max(left.second,right.second)};\n }\n int amountOfTime(TreeNode* root, int start) {\n ans=0;\n func(root,start);\n return ans;\n }\n};",
"memory": "99500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int maxVal = 0;\n unordered_map<int, int> mp;\n int amountOfTime(TreeNode* root, int start) {\n findNode(root, start);\n dfs(root, 0);\n return maxVal;\n }\n\n void dfs(TreeNode * root, int dis) {\n if(!root) return;\n if(mp.contains(root->val)) dis = mp[root->val];\n maxVal = max(dis, maxVal);\n dfs(root->left, dis+1);\n dfs(root->right, dis+1);\n }\n\n int findNode(TreeNode * node, int start) {\n if(!node) return -1;\n\n if(node->val == start) {\n mp[node->val] = 0;\n return 0;\n }\n\n int left = findNode(node->left, start);\n if(left != -1) {\n mp[node->val] = left + 1;\n return left+1;\n } else {\n int right = findNode(node->right, start);\n if(right != -1) {\n mp[node->val] = right+1;\n return right+1;\n }\n }\n\n return -1;\n }\n};",
"memory": "102300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n int ans=0, startNode;\n pair<int,bool> solve(TreeNode* root){\n if(!root){\n return {0,false};\n }\n \n pair<int,bool> leftSubTree = solve(root->left);\n pair<int,bool> rightSubTree = solve(root->right);\n\n if(root->val == startNode){\n ans=max({ans,leftSubTree.first,rightSubTree.first});\n return {1,true};\n }else if(leftSubTree.second){\n ans=max(ans,leftSubTree.first+rightSubTree.first);\n return {leftSubTree.first+1,true};\n }else if(rightSubTree.second){\n ans=max(ans,leftSubTree.first+rightSubTree.first);\n return {rightSubTree.first+1,true};\n }else{\n return {max(leftSubTree.first,rightSubTree.first)+1,false};\n }\n }\n\npublic:\n int amountOfTime(TreeNode* root, int start) {\n startNode = start;\n solve(root);\n return ans;\n }\n};",
"memory": "105100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int maxdistance=0;\n\n int travel(TreeNode* node,int start){\n if(node==NULL) return 0;\n int leftdepth=travel(node->left,start);\n int rightdepth=travel(node->right,start);\n int depth=0;\n if(node->val==start){\n maxdistance=max({leftdepth,rightdepth,maxdistance});\n depth=-1;\n }\n else if(leftdepth>=0&&rightdepth>=0){\n depth=max(leftdepth,rightdepth)+1;\n }\n else{\n maxdistance=max(maxdistance,abs(leftdepth)+abs(rightdepth));\n depth=min(leftdepth,rightdepth)-1;\n }\n return depth;\n\n }\n int amountOfTime(TreeNode* root, int start) {\n travel(root,start);\n return maxdistance;\n }\n};",
"memory": "107900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int height (TreeNode * root) {\n if(root == NULL) {\n return -1;\n }\n if(root->left == NULL && root->right == NULL) {\n return 0;\n }\n return 1 + max(height(root->left), height(root->right));\n }\n\n int findTotalTime (TreeNode * root, TreeNode * prev, unordered_map<int, TreeNode*> &parent, int offset) {\n int ans = 0;\n if(root->left != prev) {\n ans = max(height(root->left) + 1 + offset, ans);\n }\n if(root->right != prev) {\n ans = max(height(root->right) + 1 + offset, ans);\n }\n return parent[root->val] != NULL ? max(ans, findTotalTime(parent[root->val], root, parent, offset+1)) : ans;\n }\n\n TreeNode * findInfectedNode (TreeNode * root, TreeNode * prev, unordered_map<int, TreeNode*> &parent, int start) {\n if(root == NULL) {\n return NULL;\n }\n parent[root->val] = prev;\n if(root->val == start) {\n return root;\n }\n // Skip for NULL\n TreeNode * left = findInfectedNode(root->left, root, parent, start);\n if(left != NULL) {\n return left;\n }\n return findInfectedNode(root->right, root, parent, start);\n }\n\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int, TreeNode*> parent;\n TreeNode * infectedNode = findInfectedNode(root, NULL, parent, start);\n int ans = height(infectedNode);\n if(parent[start] != NULL) {\n ans = max(ans, findTotalTime(parent[start], infectedNode, parent, 1));\n }\n return ans;\n }\n};",
"memory": "107900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n bool helper(TreeNode* node, const int start, unordered_map<TreeNode*, TreeNode*>& parent, TreeNode* &startNode) {\n if (node->val == start) {\n startNode = node;\n return true;\n }\n if (node->left && helper(node->left, start, parent, startNode)) {\n parent[node->left] = node;\n return true;\n }\n if (node->right && helper(node->right, start, parent, startNode)) {\n parent[node->right] = node;\n return true;\n }\n return false;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*, TreeNode*> parent;\n TreeNode* startNode;\n helper(root, start, parent, startNode);\n int time = -1;\n queue<TreeNode*> q;\n q.push(startNode);\n while (!q.empty()) {\n time++;\n auto qs = q.size();\n while (qs--) {\n auto curr = q.front();\n q.pop();\n if (parent.count(curr))\n q.push(parent[curr]);\n if (curr->left && !parent.count(curr->left))\n q.push(curr->left);\n if (curr->right && !parent.count(curr->right))\n q.push(curr->right);\n }\n }\n return time;\n }\n};",
"memory": "110700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n TreeNode* lca(TreeNode* root, TreeNode* p, TreeNode* q) {\n if (!root || root == p || root == q) {\n return root;\n }\n TreeNode* left = lca(root->left, p, q);\n TreeNode* right = lca(root->right, p, q);\n if (!left) {\n return right;\n }\n if (!right) {\n return left;\n }\n return root;\n }\n TreeNode* k = nullptr;\n void getStart(TreeNode* root, int target) {\n queue<TreeNode*> q;\n q.push(root);\n while (!q.empty()) {\n int size = q.size();\n for (int i = 0; i < size; i++) {\n TreeNode* node = q.front();\n q.pop();\n if (node->val == target) {\n k = node;\n return;\n }\n if (node->left) {\n q.push(node->left);\n }\n if (node->right) {\n q.push(node->right);\n }\n }\n }\n }\n int findDistance(TreeNode* root, TreeNode* target) {\n queue<TreeNode*> q;\n q.push(root);\n int level = 0;\n while (!q.empty()) {\n int size = q.size();\n for (int i = 0; i < size; i++) {\n TreeNode* node = q.front();\n q.pop();\n if (node == target) {\n return level;\n }\n if (node->left) {\n q.push(node->left);\n }\n if (node->right) {\n q.push(node->right);\n }\n }\n level++;\n }\n return -1;\n }\n int depth(TreeNode* root) {\n if (!root) {\n return 0;\n }\n int left = depth(root->left);\n int right = depth(root->right);\n return max(left, right) + 1;\n }\n int amountOfTime(TreeNode* root, int start) {\n queue<TreeNode*> q;\n q.push(root);\n getStart(root, start);\n int ans = 0;\n TreeNode* anc = lca(root, k, root);\n int f = findDistance(anc, k);\n int s = findDistance(anc, root);\n ans = max(ans, f + s);\n int dep_l = depth(root->left) + 1;\n int dep_r = depth(root->right) + 1;\n cout << dep_l << ' ' << dep_r;\n int level = 1;\n while (!q.empty()) {\n int size = q.size();\n for (int i = 0; i < size; i++) {\n TreeNode* node = q.front();\n q.pop();\n if (level == dep_l && (i == 0 || i == size - 1)) {\n TreeNode* anc = lca(root, k, node);\n f = findDistance(anc, k);\n s = findDistance(anc, node);\n ans = max(ans, f + s);\n }\n else if (level == dep_r && (i == 0 || i == size - 1)) {\n TreeNode* anc = lca(root, k, node);\n f = findDistance(anc, k);\n s = findDistance(anc, node);\n ans = max(ans, f + s);\n }\n if (node->left) {\n q.push(node->left);\n }\n if (node->right) {\n q.push(node->right);\n }\n }\n level++;\n }\n return ans;\n }\n};",
"memory": "110700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int findTime(TreeNode* root,int lvl,int& maxi,int start){\n if(root==NULL){\n return 0;\n }\n int left = findTime(root->left,lvl+1,maxi,start);\n int right = findTime(root->right,lvl+1,maxi,start);\n\n if(left<0 || right<0){\n maxi = max(maxi,abs(left)+abs(right));\n return (left<0)?left-1:right-1;\n }\n maxi=max({maxi,left,right});\n cout<<maxi<<\" \"<<root->val<<endl;\n if(root->val==start){\n return -1;\n }\n return max(left,right)+1;\n }\n int amountOfTime(TreeNode* root, int start) {\n int maxi=0;\n findTime(root,0,maxi,start);\n return maxi;\n }\n};",
"memory": "113500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int maxx = 0;\n int solve(TreeNode* root, int start) {\n if (!root)\n return 0;\n\n if (root->val == start) {\n return -1;\n }\n\n int l = solve(root->left, start);\n int r = solve(root->right, start);\n\n if (l < 0) {\n maxx = max({maxx, abs(l) + r});\n return l - 1;\n } else if (r < 0) {\n maxx = max({maxx, abs(r) + l});\n return r - 1;\n }\n return max(l, r) + 1;\n }\n void find(TreeNode* root,int start, TreeNode * &bl) {\n if (!root)\n return;\n if (root->val == start) {\n bl = root;\n return;\n }\n find(root->left,start, bl);\n find(root->right,start, bl);\n }\n int height(TreeNode* root)\n {\n if(!root)\n return 0;\n return max({height(root->left) , height(root->right)})+1;\n }\n int amountOfTime(TreeNode* root, int start) {\n\n solve(root, start);\n TreeNode* bl;\n find(root, start, bl);\n int h = height(bl);\n cout<<maxx<<\" \"<<h-1<<endl;\n return max(maxx,h-1);\n }\n};",
"memory": "113500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void Treepath(TreeNode *root,int target,vector<TreeNode*>& path){\n if(root == nullptr) return;\n if(root->val == target){\n path.push_back(root);\n return;\n }\n path.push_back(root);\n Treepath(root->left,target,path);\n Treepath(root->right,target,path);\n if(path[path.size()-1]->val!=target){\n path.pop_back();\n }\n }\n void height(TreeNode * root,int h,int &maxi){\n if(root==nullptr) return;\n height(root->left,h+1,maxi);\n height(root->right,h+1,maxi);\n maxi=max(h,maxi);\n }\n int amountOfTime(TreeNode* root, int start) {\n vector<TreeNode *> path;\n int maxi=0;\n Treepath(root,start,path);\n height(path[path.size()-1],0,maxi);\n int j=1;\n for(int i=(path.size()-2);i>=0;i--){\n int h=0;\n if(path[i]->left==path[i+1])height(path[i]->right,1,h);\n else height(path[i]->left,1,h);\n maxi=max(maxi,(h+j));\n j++;\n }\n return maxi;\n }\n};",
"memory": "116300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void Treepath(TreeNode *root,int target,vector<TreeNode*>& path){\n if(root == nullptr) return;\n if(root->val == target){\n path.push_back(root);\n return;\n }\n path.push_back(root);\n Treepath(root->left,target,path);\n Treepath(root->right,target,path);\n if(path[path.size()-1]->val!=target){\n path.pop_back();\n }\n }\n void height(TreeNode * root,int h,int &maxi){\n if(root==nullptr) return;\n height(root->left,h+1,maxi);\n height(root->right,h+1,maxi);\n maxi=max(h,maxi);\n }\n int amountOfTime(TreeNode* root, int start) {\n vector<TreeNode *> path;\n int maxi=0;\n Treepath(root,start,path);\n height(path[path.size()-1],0,maxi);\n int j=1;\n for(int i=(path.size()-2);i>=0;i--){\n int h=0;\n if(path[i]->left==path[i+1])height(path[i]->right,1,h);\n else height(path[i]->left,1,h);\n maxi=max(maxi,(h+j));\n j++;\n }\n return maxi;\n }\n};",
"memory": "116300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int maxDepthMap[100001];\n queue<pair<TreeNode*, int>> ancestors;\n int maxDepth(TreeNode* root) {\n if(root == nullptr) return 0;\n maxDepthMap[root->val] = 1 + max(maxDepth(root->left), maxDepth(root->right));\n return maxDepthMap[root->val];\n }\n bool getAncestors(TreeNode* root, int target) {\n if(root == nullptr) return false;\n if(root->val == target) {\n ancestors.push({root, 0});\n return true;\n }\n if(getAncestors(root->left, target)) {\n ancestors.push({root, -1});\n return true;\n }\n if(getAncestors(root->right, target)) {\n ancestors.push({root, 1});\n return true;\n }\n return false;\n }\n void printStack(queue<pair<TreeNode*, int>> st) {\n while(!st.empty()) {\n cout << st.front().first->val << ' ';\n st.pop();\n }\n cout << '\\n';\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n fill(maxDepthMap, maxDepthMap + 100001, 0);\n ancestors = queue<pair<TreeNode*, int>>();\n getAncestors(root, start);\n maxDepth(root);\n int ancestorsLevel = 0;\n int ans = 0;\n while(!ancestors.empty()) {\n // printStack(ancestors);\n TreeNode* curr = ancestors.front().first;\n int dir = ancestors.front().second;\n int md = 0;\n if(dir < 0 && curr->right != nullptr) {\n md = maxDepthMap[curr->right->val];\n }\n if(dir > 0 && curr->left != nullptr) {\n md = maxDepthMap[curr->left->val];\n }\n if(dir == 0) {\n md = maxDepthMap[curr->val] - 1;\n }\n // cout << curr->val << ' ' << md << ' ' << ancestorsLevel << '\\n';\n ans = max(ans, md + ancestorsLevel);\n ancestors.pop();\n ancestorsLevel++;\n }\n return ans;\n }\n};",
"memory": "119100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int maxDepthMap[100001];\n queue<pair<TreeNode*, int>> ancestors;\n int maxDepth(TreeNode* root) {\n if(root == nullptr) return 0;\n maxDepthMap[root->val] = 1 + max(maxDepth(root->left), maxDepth(root->right));\n return maxDepthMap[root->val];\n }\n bool getAncestors(TreeNode* root, int target) {\n if(root == nullptr) return false;\n if(root->val == target) {\n ancestors.push({root, 0});\n return true;\n }\n if(getAncestors(root->left, target)) {\n ancestors.push({root, -1});\n return true;\n }\n if(getAncestors(root->right, target)) {\n ancestors.push({root, 1});\n return true;\n }\n return false;\n }\n void printStack(queue<pair<TreeNode*, int>> st) {\n while(!st.empty()) {\n cout << st.front().first->val << ' ';\n st.pop();\n }\n cout << '\\n';\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n fill(maxDepthMap, maxDepthMap + 100001, 0);\n ancestors = queue<pair<TreeNode*, int>>();\n getAncestors(root, start);\n maxDepth(root);\n int ancestorsLevel = 0;\n int ans = 0;\n while(!ancestors.empty()) {\n // printStack(ancestors);\n TreeNode* curr = ancestors.front().first;\n int dir = ancestors.front().second;\n int md = 0;\n if(dir < 0 && curr->right != nullptr) {\n md = maxDepthMap[curr->right->val];\n }\n if(dir > 0 && curr->left != nullptr) {\n md = maxDepthMap[curr->left->val];\n }\n if(dir == 0) {\n md = maxDepthMap[curr->val] - 1;\n }\n // cout << curr->val << ' ' << md << ' ' << ancestorsLevel << '\\n';\n ans = max(ans, md + ancestorsLevel);\n ancestors.pop();\n ancestorsLevel++;\n }\n return ans;\n }\n};",
"memory": "119100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode *get_and_mapping(TreeNode *root, int target, unordered_map<TreeNode*,TreeNode*> &parent)\n{\n // its only map the parent relation from target to root TreeNode path\n if (root == NULL)\n return NULL;\n if (root->val == target)\n return root;\n TreeNode *left = get_and_mapping(root->left, target, parent);\n if (left)\n {\n parent[root->left] = root;\n return left;\n }\n TreeNode *right = get_and_mapping(root->right, target, parent);\n if (right)\n {\n parent[root->right] = root;\n return right;\n }\n // both left and right is NULL\n return NULL;\n}\nvoid get_time(TreeNode *root, unordered_map<TreeNode*,TreeNode*> &parent, int c, int &ans)\n{\n if (root == NULL)\n return;\n if (ans < c)\n ans = c;\n if(root->left && parent.find(root->left)==parent.end())\n get_time(root->left, parent, c + 1, ans);\n if(root->right && parent.find(root->right)==parent.end())\n get_time(root->right, parent, c + 1, ans);\n if(parent.find(root)!=parent.end()) // no mapping of current TreeNode with parent\n //or\n //if(parent[root])\n get_time(parent[root], parent, c + 1, ans);\n}\nint time_for_burning(TreeNode *root, int target_data)\n{\n unordered_map<TreeNode*,TreeNode*> parent;\n // no need to make parent[root]=NULL bz in recusion only it checks it is present or not\n TreeNode *target = get_and_mapping(root, target_data, parent);\n int ans = 0;\n\n get_time(target, parent, 0, ans);\n return ans;\n}\n\n int amountOfTime(TreeNode* root, int start) {\n return time_for_burning(root,start);\n }\n};",
"memory": "121900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int maxDepth(TreeNode* root){\n if(root == nullptr) return 0;\n int left = maxDepth(root->left);\n int right = maxDepth(root->right);\n return 1 + max(left, right);\n }\n int storeAncestors(TreeNode* root, int target, vector<pair<TreeNode*, int>>& ances){\n if(root == nullptr) return 0;\n int found = 0, foundR=0;\n if(root->val == target){\n ances.push_back({root, 0});\n return 1;\n }\n found = storeAncestors(root->left, target, ances);\n if(found) ances.push_back({root, -1});\n foundR = storeAncestors(root->right, target, ances);\n if(foundR) ances.push_back({root, 1});\n\n return found || foundR;\n }\n int amountOfTime(TreeNode* root, int start) {\n vector<pair<TreeNode*, int>> ances;\n storeAncestors(root, start, ances);\n int maxTime = maxDepth(ances[0].first) - 1;\n // cout << (ances[0].first)->val << \" \" << maxTime << endl;\n for(int dep=1; dep<ances.size(); dep++){\n auto ele = ances[dep];\n int time = (ele.second==-1?maxDepth((ele.first)->right):maxDepth((ele.first)->left));\n // cout << (ele.first)->val << \" \" << dep+time << endl;\n maxTime = max(maxTime, dep + time);\n }\n return maxTime;\n }\n};",
"memory": "121900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int maxTime;\n\n tuple<int, int> fun(TreeNode* root, int start) {\n if(root == NULL) {\n return {-1, -1};\n }\n\n if(root->val == start) {\n auto [leftValue, leftHeight] = fun(root->left, start);\n auto [rightValue, rightHeight] = fun(root->right, start);\n\n maxTime = max(maxTime, 1 + max(leftHeight, rightHeight));\n return {1, 1 + max(leftHeight, rightHeight)};\n }\n\n auto [leftValue, leftHeight] = fun(root->left, start);\n auto [rightValue, rightHeight] = fun(root->right, start);\n if(leftValue == -1 && rightValue == -1) {\n return {-1, 1 + max(leftHeight, rightHeight)};\n }\n if(leftValue == -1) {\n maxTime = max(maxTime, rightValue + 1 + leftHeight);\n return {rightValue + 1 ,1 + max(leftHeight, rightHeight)};\n }\n maxTime = max(maxTime, leftValue + 1 + rightHeight);\n return {leftValue + 1 ,1 + max(leftHeight, rightHeight)};\n }\n\n int amountOfTime(TreeNode* root, int start) {\n maxTime = 0;\n fun(root, start);\n return maxTime;\n }\n};",
"memory": "124700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n map<int, TreeNode*> parents;\n TreeNode* target;\n int strt;\n int maxi;\n\n int amountOfTime(TreeNode* root, int start) {\n strt = start;\n recPar(root, NULL);\n iter(target, 0, 0);\n return maxi;\n }\n\n void recPar(TreeNode* node, TreeNode* par){\n if(node->val == strt){\n target = node;\n } \n parents[node->val] = par;\n if(node->left) recPar(node->left, node);\n if(node->right) recPar(node->right, node);\n }\n\n void iter(TreeNode* root, int dir, int dis){\n if(root == NULL) return;\n maxi = max(maxi, dis);\n auto p = parents[root->val]; \n if(dir != 1 && p != NULL){\n if(root == p->left){\n iter(p, 2, dis+1);\n }else{\n iter(p,3, dis+1);\n }\n }\n if(dir != 2){\n iter(root->left, 1, dis+1);\n }\n if(dir != 3){\n iter(root->right, 1, dis+1);\n }\n }\n};",
"memory": "124700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n vector<int> adj[100001];\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()) {\n TreeNode *node = q.front();\n q.pop();\n\n if(node->left) {\n adj[node->val].push_back(node->left->val);\n adj[node->left->val].push_back(node->val);\n q.push(node->left);\n }\n if(node->right) {\n adj[node->val].push_back(node->right->val);\n adj[node->right->val].push_back(node->val);\n q.push(node->right);\n }\n }\n int timeToInfect = 0;\n bitset<100001> vis(false);\n queue<pair<int,int>> infection;\n infection.push({0,start});\n while(!infection.empty()) {\n int node = infection.front().second, lvl = infection.front().first;\n infection.pop();\n vis[node]=1;\n timeToInfect = max(timeToInfect, lvl);\n for(auto &it: adj[node]) {\n if(!vis[it]) infection.push({lvl+1,it});\n }\n }\n return timeToInfect;\n }\n};",
"memory": "127500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n TreeNode* createMapping(TreeNode* root, unordered_map<TreeNode* , TreeNode*>& mp, int target){\n if(!root) return NULL;\n mp[root] = NULL;\n queue<TreeNode*>q;\n q.push(root);\n TreeNode* targetNode = NULL;\n while(!q.empty()){\n int size = q.size();\n for(int i=0; i<size; i++){\n TreeNode* temp = q.front();\n q.pop();\n if(temp->val == target) targetNode = temp;\n if(temp->left){\n mp[temp->left] = temp;\n q.push(temp->left);\n }\n if(temp->right){\n mp[temp->right] = temp;\n q.push(temp->right);\n }\n }\n }\n return targetNode;\n }\n\n int solve(TreeNode* root, unordered_map<TreeNode* , TreeNode*>& parent){\n queue<TreeNode*>q;\n q.push(root);\n root->val = -1;\n int ans = 0;\n while(!q.empty()){\n int n = q.size();\n bool infected = false;\n for(int i=0; i<n; i++){\n TreeNode* temp = q.front();\n q.pop();\n if(temp->left && temp->left->val != -1){\n infected = true;\n q.push(temp->left);\n temp->left->val = -1;\n }\n if(temp->right && temp->right->val != -1){\n infected = true;\n q.push(temp->right);\n temp->right->val = -1;\n }\n if(parent[temp] && parent[temp]->val != -1){\n infected = true;\n q.push(parent[temp]);\n parent[temp]->val = -1;\n }\n }\n if(infected) ans++;\n }\n return ans;\n }\n\n int amountOfTime(TreeNode* root, int target) {\n unordered_map<TreeNode* , TreeNode*>parent;\n TreeNode* targetNode = createMapping(root, parent, target);\n return solve(targetNode, parent);\n }\n};",
"memory": "127500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* markParents(TreeNode* root,unordered_map<TreeNode*,TreeNode*> &um,int start){\n queue<TreeNode*> q;\n TreeNode* a;\n q.push(root);\n while(!q.empty()){\n int s=q.size();\n for(int i=0;i<s;i++){\n TreeNode* t = q.front();\n q.pop();\n if(t->val == start) a=t;\n if(t->left) {q.push(t->left);um[t->left]=t;}\n if(t->right) {q.push(t->right);um[t->right]=t;}\n\n }\n }\n return a; \n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*,TreeNode*> um;\n TreeNode* st = markParents(root,um,start);\n\n queue<TreeNode*> q;\n int time=0;\n // queue<TreeNode*> q;\n // bool visited[100000] = false;\n vector<bool> visited(100000,false);\n visited[st->val]=true;\n q.push(st);\n while(!q.empty()){\n int s=q.size();\n for(int i=0;i<s;i++){\n TreeNode* t = q.front();\n q.pop();\n\n if(t->left && !visited[t->left->val]){\n q.push(t->left);\n visited[t->left->val]=true;\n }\n if(t->right && !visited[t->right->val]){\n q.push(t->right);\n visited[t->right->val]=true;\n }\n if(um[t] && !visited[um[t]->val]){\n q.push(um[t]);\n visited[um[t]->val]=true;\n }\n }\n time++;\n }\n return time-1;\n\n }\n};",
"memory": "130300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\n\nstruct Info {\n bool contains;\n int time;\n int depth;\n Info()\n {\n depth = 0;\n contains = false;\n time = -1;\n }\n};\n\nclass Solution {\n int ans = 0, depthStart = -1;\n\n int dfs(TreeNode* root, int start, Info& info) {\n if (!root) {\n return 0;\n }\n\n Info leftInfo, rightInfo;\n int leftH = dfs(root->left, start, leftInfo);\n int rightH = dfs(root->right, start, rightInfo);\n \n if (root->val == start) {\n\n info.contains = true;\n info.time = 0;\n info.depth = max(leftH, rightH);\n ans = max(ans, info.depth);\n\n return 1 + max(leftH, rightH);\n }\n\n if (leftInfo.contains && root->left) {\n if (leftInfo.time != -1){\n info.time = 1 + leftInfo.time;\n info.depth = leftInfo.depth;\n }\n info.contains = info.contains || true;\n } else if (rightInfo.contains && root->right) {\n if (rightInfo.time != -1) {\n info.time = 1 + rightInfo.time;\n info.depth = rightInfo.depth;\n }\n info.contains = info.contains || true;\n }\n\n\n // cout<<root->val<<\" \"<<leftH<<\" \"<<rightH<<\"\\n\";\n // cout<<info.time<<\" \"<<info.contains<<\"\\n\";\n\n if (info.contains) {\n if (root->left && leftInfo.contains) {\n // cout<<\"left: \"<<leftInfo.depth<<\"\\n\";\n ans = max(ans, max(info.time + rightH,leftInfo.depth));\n } else if (root->right && rightInfo.contains) {\n // cout<<\"right: \"<<rightInfo.depth<<\"\\n\";\n ans = max(ans, max(info.time + leftH, rightInfo.depth));\n }\n }\n // cout<<\"ans: \"<<ans<<\"\\n\";\n\n\n return 1 + max(leftH, rightH);\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n Info info;\n dfs(root, start, info);\n return ans;\n }\n};",
"memory": "130300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int>v[100001];\n int vis[100001];\n int ans=0;\n void makeGraph(TreeNode*root){\n if(root==NULL){\n return;\n }\n if(root->left!=NULL){\n v[root->left->val].push_back(root->val);\n v[root->val].push_back(root->left->val);\n }\n if(root->right!=NULL){\n v[root->right->val].push_back(root->val);\n v[root->val].push_back(root->right->val);\n }\n makeGraph(root->left);\n makeGraph(root->right);\n \n }\n int dfs(int s,int c){\n ans=max(ans,c);\n vis[s]=1;\n for(auto it:v[s]){\n if(vis[it]==0){\n dfs(it,c+1);\n }\n }\n \n return ans;\n }\n int amountOfTime(TreeNode* root, int s) {\n if(root==NULL){\n return 0;\n }\n int i,j;\n makeGraph(root);\n // for(i=1;i<=10;i++){\n // cout<<i<<\"->\";\n // for(j=0;j<v[i].size();j++){\n // cout<<v[i][j]<<\" \";\n // }\n // cout<<endl;\n // }\n \n return dfs(s,0);\n \n }\n};",
"memory": "133100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void cnt(TreeNode* root,int &n){\n if(!root)return;\n cnt(root->left,n);\n n=max(n,root->val);\n cnt(root->right,n);\n }\n void build(TreeNode* root,vector<int>adj[]){\n if(!root)return;\n if(root->left){\n adj[root->val].push_back(root->left->val);\n adj[root->left->val].push_back(root->val);\n build(root->left,adj);\n }\n if(root->right){\n adj[root->val].push_back(root->right->val);\n adj[root->right->val].push_back(root->val);\n build(root->right,adj);\n }\n }\n int amountOfTime(TreeNode* root, int target) {\n int n=0;\n cnt(root,n);\n vector<int>adj[n+1];\n build(root,adj);\n vector<int>vis(n+1);\n queue<pair<int,int>>q;\n q.push({target,0});\n vis[target]=1;\n int ans=0;\n while(!q.empty()){\n int node=q.front().first;\n int tm=q.front().second;\n q.pop();\n ans=max(ans,tm);\n for(auto i:adj[node]){\n if(!vis[i]){\n vis[i]=1;\n q.push({i,tm+1});\n }\n }\n }\n return ans;\n }\n};",
"memory": "133100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n int amountOfTime(TreeNode* root, int start) {\n map<TreeNode*,pair<TreeNode*,bool>> mp;\n queue<TreeNode*>q;\n TreeNode* src;\n q.push(root);\n mp[root]={NULL,0};\n while(!q.empty()){\n TreeNode* curr=q.front();\n if(curr->val==start){\n src=curr;\n }\n q.pop();\n if(curr->left!=NULL){\n q.push(curr->left);\n mp[curr->left]={curr,0};\n }\n if(curr->right!=NULL){\n q.push(curr->right);\n mp[curr->right]={curr,0};\n }\n }\n\n \n q.push(src);\n int ans=0;\n while(!q.empty()){\n int sz=q.size();\n for(int i=0;i<sz;i++){\n auto curr=q.front();\n q.pop();\n if(curr->left!=NULL&&!mp[curr->left].second){\n q.push(curr->left);\n mp[curr->left].second=1;\n }\n if(curr->right!=NULL&&!mp[curr->right].second){\n q.push(curr->right);\n mp[curr->right].second=1;\n }\n if(mp[curr].first!=NULL&&!mp[curr].second){\n q.push(mp[curr].first);\n mp[curr].second=1;\n }\n }\n ans++;\n \n }\n return ans-1;\n }\n};",
"memory": "135900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nTreeNode* startNode = NULL;\nvoid findstart(TreeNode* root, int start)\n{\n if (root == NULL)\n return ;\n \n if (root -> val == start)\n {\n startNode = root;\n return;\n }\n\n findstart(root->left, start);\n findstart(root->right, start);\n\n}\n\npublic:\n int amountOfTime(TreeNode* root, int start) {\n\n unordered_map<TreeNode*,TreeNode*> par;\n vector<bool> vis(1e5+1, false);\n int ans = 0;\n\n queue<TreeNode*> qu;\n qu.push(root);\n\n while (!qu.empty())\n {\n TreeNode *ptr = qu.front();\n qu.pop();\n\n if (ptr->left)\n {\n par[ptr->left] = ptr;\n qu.push(ptr->left);\n }\n\n if (ptr->right)\n {\n par[ptr->right] = ptr;\n qu.push(ptr->right);\n }\n }\n\n findstart(root, start);\n\n queue<pair<TreeNode*, int>> quu;\n quu.push({startNode, 0});\n vis[startNode->val] = true;\n\n while (!quu.empty())\n {\n TreeNode* node = quu.front().first;\n int lvl = quu.front().second;\n ans = max(ans, lvl);\n quu.pop();\n\n if ( node->left && !vis[node->left->val])\n {\n vis[node->left->val] = true;\n quu.push({node->left, lvl + 1});\n }\n\n if ( node->right && !vis[node->right->val])\n {\n vis[node->right->val] = true;\n quu.push({node->right, lvl + 1});\n }\n\n if ( par.find(node) != par.end() && !vis[par[node]->val])\n {\n vis[par[node]->val] = true;\n quu.push({par[node], lvl + 1});\n }\n }\n\n return ans;\n }\n};",
"memory": "135900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> g[300000];\n bool vis[300000];\n int dist[300000];\n int n = 0;\n void dfs(TreeNode* node){\n if(node != NULL){\n n++;\n if(node->left != NULL){\n g[node->val].push_back((node->left)->val);\n g[(node->left)->val].push_back(node->val);\n dfs(node->left);\n }\n if(node->right != NULL){\n g[node->val].push_back((node->right)->val);\n g[(node->right)->val].push_back(node->val);\n dfs(node->right);\n }\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n dfs(root);\n dist[start] = 0;\n vis[start] = true;\n queue<int> q;\n q.push(start);\n while(!q.empty()){\n int x = q.front();\n q.pop();\n vector<int>::iterator y;\n for(y = g[x].begin(); y != g[x].end(); y++){\n if(!vis[*y]){\n q.push(*y);\n dist[*y] = dist[x] + 1;\n vis[*y] = true;\n }\n }\n }\n return *max_element(dist, dist+300000);\n }\n};",
"memory": "138700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void assignParents(TreeNode* root,map<TreeNode*,TreeNode*>&mp)\n {\n if(root->left!=NULL)\n {\n mp[root->left]=root;\n assignParents(root->left,mp);\n }\n if(root->right!=NULL)\n {\n mp[root->right]=root;\n assignParents(root->right,mp);\n }\n }\n int maxTime(TreeNode* root,TreeNode* previous,int k,map<TreeNode*,TreeNode*>&mp)\n {\n if(mp[root]==NULL && previous!=NULL )\n {\n if(root->left==NULL || root->right==NULL)\n return k;\n }\n if(root->left==NULL && root->right==NULL && previous!=NULL)\n return k;\n int a=INT_MIN,b=INT_MIN,c=INT_MIN;\n\n if(root!=NULL)\n {\n \n if(mp[root]!=NULL && mp[root]!=previous)\n a= maxTime(mp[root],root,k+1,mp);\n\n if(root->left!=NULL && root->left!=previous)\n b= maxTime(root->left,root,k+1,mp);\n\n if(root->right!=NULL && root->right!=previous)\n c= maxTime(root->right,root,k+1,mp);\n\n }\n return max(a,max(b,c));\n\n }\n TreeNode* find(TreeNode* root,int start)\n {\n if(root==NULL)\n return NULL;\n\n if(root->val==start)\n return root;\n\n if(root->left!=NULL)\n { \n TreeNode* leftnode=find(root->left,start);\n if(leftnode!=NULL)\n return leftnode;\n }\n if(root->right!=NULL)\n { \n TreeNode* rightnode=find(root->right,start);\n if(rightnode!=NULL)\n return rightnode;\n }\n return NULL;\n \n \n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n \n if(root==NULL )return 0;\n if(root->left==NULL && root->right==NULL)return 0;\n\n TreeNode* node=find(root,start);\n\n \n map<TreeNode*,TreeNode*>mp;\n mp[root]=NULL;\n assignParents(root,mp);\n\n \n\n \n\n return maxTime(node,NULL,0,mp);\n\n }\n};",
"memory": "138700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void f(TreeNode* root,vector<vector<int>>&adj){\n if(!root){\n return;\n }\n if(root->left){\n adj[root->val].push_back((root->left)->val);\n adj[(root->left)->val].push_back(root->val);\n f(root->left,adj);\n }\n if(root->right){\n adj[root->val].push_back((root->right)->val);\n adj[(root->right)->val].push_back(root->val);\n f(root->right,adj);\n }\n return;\n } \n\n int nom(TreeNode* root){\n if(!root){\n return 0;\n }\n return max(root->val,max(nom(root->left),nom(root->right)));\n } \n\n int amountOfTime(TreeNode* root, int start) {\n int n=nom(root);\n // cout<<n<<endl;\n vector<vector<int>>adj(n+1);\n f(root,adj);\n queue<pair<int,int>>q;\n q.push({start,0});\n vector<int>vis(n+1,0);\n int ans=0;\n while(!q.empty()){\n int node=q.front().first;\n int time=q.front().second;\n ans=max(ans,time);\n q.pop();\n vis[node]=1;\n for(auto&it:adj[node]){\n if(!vis[it])\n q.push({it,time+1});\n }\n }\n return ans;\n }\n};",
"memory": "141500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void helper(TreeNode* root, int start, unordered_map<TreeNode*, TreeNode*>& parentChildMap, TreeNode*& target){\n if(root == NULL) return;\n\n if(root -> val == start){\n target = root;\n return;\n }\n\n if(root -> left){\n parentChildMap[root -> left] = root;\n helper(root -> left, start, parentChildMap, target);\n }\n if(root -> right){\n parentChildMap[root -> right] = root;\n helper(root -> right, start, parentChildMap, target);\n }\n }\n\n int heightCalc(TreeNode* node){\n if(node == NULL) return 0;\n\n int left = 1 + heightCalc(node -> left);\n int right = 1 + heightCalc(node -> right);\n return max(left, right);\n }\n\n int parentTreeTime(TreeNode* node, unordered_map<TreeNode*, TreeNode*>& parentChildMap){\n if(parentChildMap[node] == NULL) return 0;\n\n int time = 0;\n unordered_map<int, bool> visited;\n visited[node -> val] = true;\n\n queue<pair<TreeNode*, int>> q;\n q.push({parentChildMap[node], 1});\n\n while(!q.empty()){\n auto frontPair = q.front();\n q.pop();\n auto frontNode = frontPair.first;\n auto timeTillNode = frontPair.second;\n time = max(time, timeTillNode);\n\n visited[frontNode -> val] = true;\n\n if(parentChildMap[frontNode] != NULL && !visited[parentChildMap[frontNode] -> val]){\n q.push({parentChildMap[frontNode], timeTillNode + 1});\n }\n if(frontNode -> left && !visited[frontNode -> left -> val]){\n q.push({frontNode -> left, timeTillNode + 1});\n }\n if(frontNode -> right && !visited[frontNode -> right -> val]){\n q.push({frontNode -> right, timeTillNode + 1});\n }\n }\n return time;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n TreeNode* target = NULL;\n unordered_map<TreeNode*, TreeNode*> parentChildMap;\n parentChildMap[root] = NULL;\n\n helper(root, start, parentChildMap, target);\n \n int startLeftTree = heightCalc(target -> left);\n // cout << startLeftTree << endl;\n int startRightTree = heightCalc(target -> right);\n // cout << startRightTree << endl;\n\n int startParentTree = parentTreeTime(target, parentChildMap);\n // cout << startParentTree << endl;\n\n return max(startLeftTree, max(startRightTree, startParentTree));\n }\n};",
"memory": "141500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n void dfs(TreeNode* root,TreeNode* pp,unordered_map<TreeNode*,TreeNode*>&mpp,int &start){\n \n if(root->left!=NULL){ \n mpp[root->left] = root;\n dfs(root->left,root,mpp,start);\n }\n if(root->right!=NULL) {\n mpp[root->right] = root;\n dfs(root->right,root,mpp,start);\n }\n }\n\n void chk(TreeNode* root,int &start,TreeNode* &req){\n if(!root) return;\n if(root->val == start){\n req = root;\n return;\n }\n if(root->left!=NULL) chk(root->left,start,req);\n if(root->right!=NULL) chk(root->right,start,req);\n }\n void dfs1(TreeNode* root,TreeNode* pp,int dis,unordered_map<TreeNode*,TreeNode*>&mpp,int &ans){\n if(!root) return;\n // cout<<root->val<<\" \"<<pp->val<<\" \"<<dis<<\"\\n\";\n ans = max(ans,dis);\n if(root->left != pp){\n dfs1(root->left,root,dis+1,mpp,ans);\n }\n if(root->right!=pp){\n dfs1(root->right,root,dis+1,mpp,ans);\n }\n if(mpp[root]!=pp){\n dfs1(mpp[root],root,dis+1,mpp,ans);\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n TreeNode* pp = NULL;\n unordered_map<TreeNode*,TreeNode*>mpp;\n int ans = 0;\n TreeNode* req = NULL;\n dfs(root,pp,mpp,start);\n chk(root,start,req);\n unordered_set<TreeNode*>vis;\n dfs1(req,pp,0,mpp,ans);\n return ans;\n\n }\n};",
"memory": "144300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void traverse(TreeNode* node, int & max_nodes){\n if(node == NULL) return;\n\n max_nodes = max(max_nodes, node -> val);\n if(node -> left) traverse(node -> left, max_nodes);\n if(node -> right) traverse(node -> right, max_nodes);\n }\n int amountOfTime(TreeNode* root, int start) {\n queue<TreeNode*> q; q.push(root);\n\n int max_nodes = 0;\n traverse(root, max_nodes);\n vector<int> adj[max_nodes+1]; \n \n while(!q.empty()){\n TreeNode* node = q.front(); q.pop();\n if(node -> left) {\n q.push(node -> left);\n adj[node -> val].push_back(node -> left -> val);\n adj[node -> left -> val].push_back(node -> val);\n }\n if(node -> right) {\n q.push(node -> right);\n adj[node -> val].push_back(node -> right -> val);\n adj[node -> right -> val].push_back(node -> val);\n }\n } \n int count = 0;\n for(auto it : adj){\n cout << count << \" - \" ; count ++;\n for(auto kt : it){\n cout << kt << \" \";\n }cout << endl;\n }\n\n vector<int> vis(max_nodes+1, 0); vis[start] = 1;\n queue<pair<int, int>> q2;\n q2.push({start, 0});\n int total_time = 0;\n while(!q2.empty()){\n int node = q2.front().first, time = q2.front().second; q2.pop();\n total_time = max(total_time, time);\n for(int k = 0; k < adj[node].size(); k++){\n if(!vis[adj[node][k]]){\n vis[adj[node][k]] = 1;\n q2.push({adj[node][k], time+1});\n }\n }\n }\n return total_time; \n }\n};",
"memory": "144300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int max_depth = 0;\n void depth_finder(TreeNode* node, unordered_map<TreeNode*, TreeNode*> & parent, int depth){\n if(node == NULL) return;\n node->val = -1;\n max_depth = max(depth, max_depth);\n if(node->left != NULL && node->left->val != -1) depth_finder(node->left, parent, depth + 1);\n if(node->right != NULL && node->right->val != -1) depth_finder(node->right, parent, depth + 1);\n if(parent[node] != nullptr && parent[node]->val != -1) depth_finder(parent[node], parent, depth + 1);\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*, TreeNode*> parent;\n queue<TreeNode*> q;\n q.push(root);\n TreeNode* start_node;\n while(!q.empty()){\n int n = q.size();\n for(int i = 0; i < n; i++){\n TreeNode* node = q.front();\n q.pop();\n if(node->val == start) start_node = node;\n if(node->left != NULL){\n q.push(node->left);\n parent[node->left] = node; \n }\n if(node->right != NULL){\n q.push(node->right);\n parent[node->right] = node;\n }\n }\n }\n depth_finder(start_node, parent, 0);\n return max_depth;\n }\n};",
"memory": "147100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int max_depth = 0;\n void depth_finder(TreeNode* node, unordered_map<TreeNode*, TreeNode*> & parent, int depth){\n if(node == NULL) return;\n node->val = -1;\n max_depth = max(depth, max_depth);\n if(node->left != NULL && node->left->val != -1) depth_finder(node->left, parent, depth + 1);\n if(node->right != NULL && node->right->val != -1) depth_finder(node->right, parent, depth + 1);\n if(parent[node] != nullptr && parent[node]->val != -1) depth_finder(parent[node], parent, depth + 1);\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*, TreeNode*> parent;\n queue<TreeNode*> q;\n q.push(root);\n TreeNode* start_node;\n while(!q.empty()){\n int n = q.size();\n for(int i = 0; i < n; i++){\n TreeNode* node = q.front();\n q.pop();\n if(node->val == start) start_node = node;\n if(node->left != NULL){\n q.push(node->left);\n parent[node->left] = node; \n }\n if(node->right != NULL){\n q.push(node->right);\n parent[node->right] = node;\n }\n }\n }\n depth_finder(start_node, parent, 0);\n return max_depth;\n }\n};",
"memory": "147100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nTreeNode* find(TreeNode*root,int x){\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()){\n auto p=q.front();\n q.pop();\n TreeNode* node=p;\n if(node->val==x){\n return node;\n }\n if(node->left){\n q.push(node->left);\n }\n if(node->right){\n q.push(node->right);\n }\n }\n return NULL;\n}\nvoid parent (map<TreeNode*,TreeNode*> &m,TreeNode*root){\n if(root==NULL){\n return;\n }\n if(root->right){\n m[root->right]=root;\n parent(m,root->right);\n }\n if(root->left){\n m[root->left]=root;\n parent(m,root->left);\n }\n}\n int amountOfTime(TreeNode* root, int start) {\n if(root==NULL){\n return 0;\n }\n TreeNode* target=find(root,start);\n if (target == nullptr) return 0;\n map<TreeNode*,TreeNode*>m;\n parent(m,root);\n queue<TreeNode*>q;\n map<TreeNode*,bool>vis;\n q.push(target);\n vis[target]=true;\n int ans=0;\n while(!q.empty()){\n int n=q.size();\n for(int i=0;i<n;i++){\n auto p=q.front();\n TreeNode* node=p;\n q.pop();\n if(node->left && !vis[node->left]){\n q.push(node->left);\n vis[node->left]=true;\n }\n if(node->right!=NULL && vis[node->right]!=true){\n q.push(node->right);\n vis[node->right]=true;\n }\n if(m[node]!=NULL && vis[m[node]]!=true){\n q.push(m[node]);\n vis[m[node]]=true;\n }\n }\n ans++;\n }\n return ans-1;\n }\n};",
"memory": "149900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* st;\n void gp(unordered_map<TreeNode* ,TreeNode* > & parent,TreeNode* root,int start){\n if (root == nullptr) {\n return;\n }\n if (root->left) {\n parent[root->left] = root;\n }\n if (root->right) {\n parent[root->right] = root;\n }\n if (root->val == start) {\n st = root;\n }\n gp(parent, root->left, start);\n gp(parent, root->right, start);\n\n\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*,TreeNode* > parent;\n set<TreeNode* > visited;\n parent[root] = nullptr;\n gp(parent,root,start);\n queue<TreeNode*> qu;\n qu.push(st);\n visited.insert(st);\n int min =0;\n while(!qu.empty()){\n int size=qu.size();\n while(size--){\n TreeNode* temp = qu.front();\n qu.pop();\n if(temp->left && visited.find(temp->left)==visited.end()){\n qu.push(temp->left);\n visited.insert(temp->left);\n }\n if(temp->right && visited.find(temp->right)==visited.end()){\n qu.push(temp->right);\n visited.insert(temp->right);\n }\n if((parent[temp]) && (visited.find(parent[temp])==visited.end())){\n qu.push(parent[temp]);\n visited.insert(parent[temp]);\n }\n }\n min++;\n }\n return min-1;\n }\n};",
"memory": "161100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n void inorder(TreeNode* root,TreeNode*&s, int start,unordered_map<TreeNode*,TreeNode*> &m){\n if(!root)return;\n if(root->left){\n m[root->left]=root;\n }\n if(root->right){\n m[root->right]=root;\n }\n \n if(root->val==start){\n s=root;\n }\n inorder(root->left,s,start,m);\n inorder(root->right,s,start,m);\n }\n \n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*,TreeNode*> m;\n TreeNode *s=nullptr;\n inorder(root,s,start,m);\n \n int ans=0;\n queue<TreeNode*> q;\n set<TreeNode*> st;\n st.insert(s);\n q.push(s);\n while(!q.empty()){\n int n=q.size();\n while(n--){\n auto curr=q.front();\n q.pop();\n if(curr->left && !st.count(curr->left)){\n q.push(curr->left);\n st.insert(curr->left);\n }\n if(curr->right && !st.count(curr->right)){\n q.push(curr->right);\n st.insert(curr->right);\n }\n if(m[curr] && !st.count(m[curr])){\n q.push(m[curr]);\n st.insert(m[curr]);\n }\n }\n ans++;\n }\n if(ans>0)ans--;\n return ans;\n }\n};",
"memory": "161100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void makeParents(TreeNode* root, unordered_map<TreeNode*, TreeNode*>& parent){\n queue<TreeNode*> q;\n q.push(root);\n\n while(!q.empty()){\n TreeNode* temp = q.front();\n q.pop();\n if(temp->left){\n parent[temp->left] = temp;\n q.push(temp->left);\n }\n if(temp->right){\n parent[temp->right] = temp;\n q.push(temp->right);\n }\n }\n }\n\n TreeNode* f1(TreeNode*root,int start)\n {\n if(root==NULL || root->val==start)return root;\n TreeNode*left=f1(root->left,start);\n TreeNode*right=f1(root->right,start);\n if(left==NULL)return right;\n else return left;\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*, TreeNode*> parent;\n makeParents(root,parent);\n\n unordered_map<TreeNode*, bool> visited;\n TreeNode*source=f1(root,start);\n queue<TreeNode*> q;\n q.push(source);\n int len = 0;\n while(!q.empty()){\n int x = q.size();\n for(int i = 0; i < x; i++){\n TreeNode* temp = q.front();\n q.pop();\n if(temp->left && visited[temp->left] == false){\n q.push(temp->left);\n }\n if(temp->right && visited[temp->right] == false){\n q.push(temp->right);\n }\n if(parent[temp] != NULL && visited[parent[temp]] == false){\n q.push(parent[temp]);\n }\n visited[temp] = true;\n }\n len++;\n }\n return len-1;\n\n }\n};",
"memory": "163900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n void findParent(TreeNode* root,\n unordered_map<TreeNode*, TreeNode*>& parent) {\n queue<TreeNode*> q;\n q.push(root);\n while (!q.empty()) {\n TreeNode* curr = q.front();\n q.pop();\n if (curr->left) {\n parent[curr->left] = curr;\n q.push(curr->left);\n }\n if (curr->right) {\n parent[curr->right] = curr;\n q.push(curr->right);\n }\n }\n }\n TreeNode* findTarget(TreeNode* root, int target) {\n if (!root)\n return nullptr;\n if (root->val == target)\n return root;\n TreeNode* leftResult = findTarget(root->left, target);\n if (leftResult)\n return leftResult;\n TreeNode* rightResult = findTarget(root->right, target);\n return rightResult;\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*, TreeNode*> parent;\n findParent(root, parent);\n TreeNode* targetNode = findTarget(root, start);\n queue<TreeNode*> q;\n unordered_map<TreeNode*, bool> vis;\n q.push(targetNode);\n vis[targetNode] = true;\n int time = -1;\n while (!q.empty()) {\n int s = q.size();\n time++;\n for (int i = 0; i < s; i++) {\n TreeNode* curr = q.front();\n q.pop();\n if (curr->left && !vis[curr->left]) {\n q.push(curr->left);\n vis[curr->left] = true;\n }\n if (curr->right && !vis[curr->right]) {\n q.push(curr->right);\n vis[curr->right] = true;\n }\n if (parent[curr] && !vis[parent[curr]]) {\n q.push(parent[curr]);\n vis[parent[curr]] = true;\n }\n }\n }\n return time;\n }\n};",
"memory": "163900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*, TreeNode*> mp;\n unordered_map<TreeNode*, int> vis;\n TreeNode* start_node;\n\n if(root == nullptr){return 0;}\n\n queue<pair<TreeNode*, TreeNode*>> q;\n q.push({root, nullptr});\n while(!q.empty()){\n pair<TreeNode*, TreeNode*> top = q.front();\n q.pop();\n TreeNode* node = top.first;\n TreeNode* parent = top.second;\n mp[node] = parent;\n vis[node] = 0;\n if(node->val == start) start_node = node;\n\n if(node->left != nullptr){q.push({node->left, node});}\n if(node->right != nullptr){q.push({node->right, node});}\n }\n \n //target\n queue<pair<TreeNode*, int>> q2;\n q2.push({start_node, 0});\n int maxi = INT_MIN;\n while(!q2.empty()){\n int size = q2.size();\n for(int i=0; i<size; i++){\n pair<TreeNode*, int> top = q2.front();\n TreeNode* node = top.first;\n int distance = top.second;\n maxi = max(distance, maxi);\n vis[node] = 1;\n q2.pop();\n if(node->left != nullptr and vis[node->left]==0){q2.push({node->left, distance+1});}\n if(node->right != nullptr and vis[node->right]==0){q2.push({node->right, distance+1});}\n if(mp[node] != nullptr and vis[mp[node]]==0){q2.push({mp[node], distance+1});}\n }\n }\n\n return maxi;\n }\n};",
"memory": "166700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\n// APPROACH :\n// - To convert the tree into an undirected graph and then apply BFS\n\nclass Solution {\npublic:\n vector<int> adjLs[100001];\n void buildAdjList(TreeNode *root) {\n if(!root) return;\n if(root->left) {\n adjLs[root->val].push_back(root->left->val);\n adjLs[root->left->val].push_back(root->val);\n }\n if(root->right) {\n adjLs[root->val].push_back(root->right->val);\n adjLs[root->right->val].push_back(root->val);\n }\n\n buildAdjList(root->left);\n buildAdjList(root->right);\n }\n int amountOfTime(TreeNode* root, int start) {\n buildAdjList(root);\n vector<int> vis(100001, 0);\n priority_queue<pair<int, int>> q; //{node, time}\n int time = 0;\n q.push({start, 0});\n vis[start] = 1;\n\n while(!q.empty()) {\n auto it = q.top();\n int nodeVal = it.first;\n int t = it.second;\n time = max(time, t);\n q.pop();\n for(auto it : adjLs[nodeVal]) {\n if(!vis[it]) {\n q.push({it, t+1});\n vis[it] = 1;\n }\n }\n }\n return time;\n }\n};",
"memory": "166700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void makeGraph(TreeNode* root,vector<int>adj[],TreeNode* parent){\n if(root==NULL){\n return;\n }\n if(parent!=NULL){\n adj[root->val].push_back(parent->val);\n }\n if(root->left){\n adj[root->val].push_back(root->left->val);\n }\n if(root->right){\n adj[root->val].push_back(root->right->val);\n }\n makeGraph(root->left,adj,root);\n makeGraph(root->right,adj,root);\n }\n int amountOfTime(TreeNode* root, int start) {\n vector<int>adj[100001];\n makeGraph(root,adj,NULL);\n queue<pair<int,int>>q;\n q.push({start,0});\n vector<int>visited(100001,0);\n visited[start]=1;\n int totalTime=0;\n while(!q.empty()){\n auto it=q.front();\n int node=it.first;\n int step=it.second;\n totalTime=max(totalTime,step);\n q.pop();\n for(auto neigh:adj[node]){\n if(!visited[neigh]){\n visited[neigh]=true;\n q.push({neigh,step+1});\n }\n }\n\n }\n return totalTime;\n }\n};",
"memory": "169500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n TreeNode* find_node(TreeNode* root, int start, vector<TreeNode*> &parent) {\n TreeNode* src = nullptr;\n queue<TreeNode*> q;\n q.push(root);\n while(q.empty() == false) {\n int lv_sz = q.size();\n for (int i = 0; i < lv_sz; ++i) {\n auto cur = q.front();\n q.pop();\n if (cur -> val == start) {\n src = cur;\n }\n if (cur -> left != nullptr) {\n q.push(cur -> left);\n parent[cur -> left -> val] = cur;\n }\n if (cur -> right != nullptr) {\n q.push(cur -> right);\n parent[cur -> right -> val] = cur;\n }\n }\n }\n return src;\n }\n \n int amountOfTime(TreeNode* root, int start) {\n vector<TreeNode*> parent(1e5 + 1, nullptr);\n vector<bool> seen(1e5 + 1, false);\n TreeNode* src = find_node(root, start, parent);\n \n queue<TreeNode*> q;\n q.push(src);\n seen[src -> val] = true;\n int t = -1;\n while(q.empty() == false) {\n int lv_sz = q.size();\n ++t;\n for (int i = 0; i < lv_sz; ++i) {\n auto cur = q.front();\n q.pop();\n cout << t << ' ' << cur -> val << '\\n';\n \n if (parent[cur -> val] != nullptr and seen[parent[cur -> val] -> val] == false) {\n q.push(parent[cur -> val]);\n // cout << \"pushing1: \" << parent[cur -> val] -> val << \"from \"<< cur -> val << '\\n';\n seen[parent[cur -> val] -> val] = true;\n }\n \n if (cur -> left != nullptr and seen[cur -> left -> val] == false) {\n q.push(cur -> left);\n seen[cur -> left -> val] = true;\n // cout << \"pushing2: \" << cur -> left -> val << \"from \"<< cur -> val << '\\n';\n }\n if (cur -> right != nullptr and seen[cur -> right -> val] == false) {\n q.push(cur -> right);\n seen[cur -> right -> val] = true;\n // cout << \"pushing3: \" << cur -> right -> val << \"from \"<< cur -> val << '\\n';\n }\n }\n }\n return t;\n }\n};",
"memory": "169500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n void solve(TreeNode* root, map<TreeNode*,TreeNode*>&mp){\n queue<TreeNode*>q;\n if(!root)return;\n q.push(root);\n while(!q.empty()){\n TreeNode* node=q.front();\n q.pop();\n if(node->left){\n q.push(node->left);\n mp[node->left]=node;\n }\n if(node->right){\n q.push(node->right);\n mp[node->right]=node;\n }\n }\n }\n void dfs(TreeNode* root,int target,queue<TreeNode*>&q){\n if(root->val==target){q.push(root);return;}\n if(root->left)dfs(root->left,target,q);\n if(root->right)dfs(root->right,target,q);\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n int ans=0;\n map<TreeNode*,TreeNode*>mp;\n solve(root,mp);\n map<TreeNode*,bool>vis;\n queue<TreeNode*>q;\n dfs(root,start,q);\n vis[q.front()]=1;\n while(!q.empty()){\n int size=q.size();\n ans++;\n for(int i=0;i<size;i++){\n TreeNode* node=q.front();\n q.pop();\n vis[node]=1;\n if(node->left && !vis[node->left]){\n q.push(node->left);\n vis[node->left]=1;\n }\n if(node->right && !vis[node->right]){\n q.push(node->right);\n vis[node->right]=1;\n }\n if(mp[node] && !vis[mp[node]]){\n q.push(mp[node]);\n vis[mp[node]]=1;\n }\n \n }\n }\n return ans-1;\n\n }\n};",
"memory": "172300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\ntypedef int neighbors[3];\nclass Solution {\nprivate:\n int create_graph(int parent,TreeNode* root, unordered_map<int,neighbors>& graph){\n if(root == nullptr) return 0;\n graph[root->val][0] = parent;\n graph[root->val][1] = create_graph(root->val,root->left ,graph);\n graph[root->val][2] = create_graph(root->val,root->right,graph);\n return root->val;\n }\n int minutes(int parent,int curr,unordered_map<int,neighbors>& graph){\n int max = 0;\n for(int neighbor : graph[curr]){\n if(neighbor != 0 && neighbor != parent){\n int m = 1 + minutes(curr,neighbor,graph);\n if(m > max) max = m;\n }\n }\n return max;\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int,neighbors> graph;\n create_graph(0,root,graph);\n return minutes(0,start,graph);\n }\n};",
"memory": "172300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\ntypedef int neighbors[3];\nclass Solution {\nprivate:\n int create_graph(int parent,TreeNode* root, unordered_map<int,neighbors>& graph){\n if(root == nullptr) return 0;\n graph[root->val][0] = parent;\n graph[root->val][1] = create_graph(root->val,root->left ,graph);\n graph[root->val][2] = create_graph(root->val,root->right,graph);\n return root->val;\n }\n int minutes(int parent,int curr,unordered_map<int,neighbors>& graph){\n int max = 0;\n for(int neighbor : graph[curr]){\n if(neighbor != 0 && neighbor != parent){\n int m = 1 + minutes(curr,neighbor,graph);\n if(m > max) max = m;\n }\n }\n return max;\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int,neighbors> graph;\n create_graph(0,root,graph);\n return minutes(0,start,graph);\n }\n};",
"memory": "175100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "typedef int neighbors[3];\nclass Solution {\nprivate:\n int create_graph(int parent,TreeNode* root, unordered_map<int,neighbors>& graph){\n if(root == nullptr) return 0;\n graph[root->val][0] = parent;\n graph[root->val][1] = create_graph(root->val,root->left ,graph);\n graph[root->val][2] = create_graph(root->val,root->right,graph);\n return root->val;\n }\n int minutes(int parent,int curr,unordered_map<int,neighbors>& graph){\n int max = 0;\n for(int neighbor : graph[curr]){\n if(neighbor != 0 && neighbor != parent){\n int m = 1 + minutes(curr,neighbor,graph);\n if(m > max) max = m;\n }\n }\n return max;\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int,neighbors> graph;\n create_graph(0,root,graph);\n return minutes(0,start,graph);\n }\n};",
"memory": "175100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int findInfectedTime(TreeNode* startNode,unordered_map<TreeNode*,TreeNode*>parent)\n {\n unordered_set<TreeNode*>vis;\n queue<TreeNode*>q;\n q.push(startNode);\n vis.insert(startNode);\n int time=0;\n \n\n while(!q.empty())\n {\n int size=q.size();\n bool flag=false;\n while(size--)\n {\n TreeNode* curr=q.front();\n q.pop();\n\n if(curr->left && !vis.count(curr->left))\n {\n vis.insert(curr->left);\n q.push(curr->left);\n flag=true;\n }\n\n if(curr->right && !vis.count(curr->right))\n {\n vis.insert(curr->right);\n q.push(curr->right);\n flag=true;\n }\n\n if(parent[curr] && !vis.count(parent[curr]))\n {\n vis.insert(parent[curr]);\n q.push(parent[curr]);\n flag=true;\n }\n\n }\n if(flag) time++;\n\n }\n return time;\n }\n TreeNode* findParent(TreeNode* root,unordered_map<TreeNode*,TreeNode*>&parent, int start)\n {\n queue<TreeNode*>q;\n q.push(root);\n TreeNode* startNode;\n\n while(!q.empty())\n {\n TreeNode* currNode=q.front();\n q.pop();\n\n if(currNode->val==start)\n {\n startNode=currNode;\n }\n\n if(currNode->left)\n {\n parent[currNode->left]=currNode;\n q.push(currNode->left);\n }\n\n if(currNode->right)\n {\n parent[currNode->right]=currNode;\n q.push(currNode->right);\n }\n\n }\n\n return startNode;\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*,TreeNode*>parent;\n TreeNode* startNode=findParent(root,parent,start);\n\n return findInfectedTime(startNode,parent);\n }\n};",
"memory": "177900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "\nclass Solution {\npublic:\n void makeParents(TreeNode* root , unordered_map<TreeNode* , TreeNode*> &parent){\n queue<TreeNode*> q;\n q.push(root);\n while(!q.empty()){\n auto node = q.front();\n q.pop();\n if(node->left) {\n parent[node->left] = node;\n q.push(node->left);\n }\n if(node->right) {\n parent[node->right] = node;\n q.push(node->right);\n } \n }\n }\n\n TreeNode* findNode(TreeNode* root , int target){\n queue<TreeNode*> q;\n unordered_map<TreeNode*,bool> visited;\n q.push(root);\n while(!q.empty()){\n auto node = q.front();\n q.pop();\n if(node->val == target){\n return node;\n }\n if(node->left && !visited[node->left]){\n q.push(node->left);\n visited[node->left] = true;\n }\n if(node->right && !visited[node->right]){\n q.push(node->right);\n visited[node->right] = true;\n }\n }\n return nullptr;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n int ans = 0;\n if(!root) return ans;\n queue<TreeNode*> q;\n unordered_map<TreeNode* , TreeNode*> mp;\n unordered_map<TreeNode* , bool> visited;\n makeParents(root,mp);\n auto temp = findNode(root,start);\n if(!temp) return ans;\n auto target = temp;\n\n q.push(target);\n visited[target] = true;\n while(!q.empty()){\n int size = q.size();\n bool hasNewNodes = false;\n for(int i = 0 ; i < size ; i++){\n auto node = q.front();\n q.pop();\n int c = 0;\n if(node->left && !visited[node->left]){\n q.push(node->left);\n visited[node->left] = true;\n c++;\n }\n if(node->right && !visited[node->right]){\n q.push(node->right);\n visited[node->right] = true;\n c++;\n }\n if(mp[node] && !visited[mp[node]]){\n q.push(mp[node]);\n visited[mp[node]] = true;\n c++;\n }\n if(c > 0) hasNewNodes = true;;\n }\n if( hasNewNodes) ++ans;\n }\n return ans;\n }\n};",
"memory": "177900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n TreeNode* parentchild(TreeNode* root,unordered_map<TreeNode*, TreeNode*>& m, int start) {\n queue<TreeNode*> q;\n q.push(root);\n TreeNode* curr;\n while (!q.empty()) {\n int queuesize = q.size();\n while (queuesize--) {\n TreeNode* temp = q.front();\n q.pop();\n if (temp->val == start) {\n curr = temp;\n }\n if (temp->left) {\n q.push(temp->left);\n m[temp->left] = temp;\n }\n if (temp->right) {\n q.push(temp->right);\n m[temp->right] = temp;\n }\n }\n }\n return curr;\n }\n int calculateTime(TreeNode* startnode,unordered_map<TreeNode*,TreeNode*> m){\n int time=0;\n queue<TreeNode*> q;\n set<TreeNode*>infected;\n q.push(startnode);\ninfected.insert(startnode);\n while (!q.empty()) {\n int queuesize = q.size();\n bool spread=false;\n while (queuesize--) {\n TreeNode* temp = q.front();\n q.pop();\n if (temp->left and !infected.count(temp->left)) {\n q.push(temp->left);\n infected.insert(temp->left);\n spread=true;\n }\n if (temp->right and !infected.count(temp->right)) {\n q.push(temp->right);\n infected.insert(temp->right);\n spread=true;\n\n }\n if(m.find(temp)!=m.end() and !infected.count(m[temp])){\n q.push(m[temp]);\n infected.insert(m[temp]);\n spread=true;\n\n }\n }\n if(spread==true)time++;\n }\n return time;\n } \n int amountOfTime(TreeNode* root,int start) {\n unordered_map<TreeNode*, TreeNode*> m;\n\n TreeNode* startnode = parentchild(root, m, start);\n\n return calculateTime(startnode, m);\n }\n};",
"memory": "180700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<int,vector<int>>mapi;\n void markConnection(TreeNode* root){\n if (root==NULL) return ;\n if(root->left!=NULL){\n mapi[root->val].push_back(root->left->val);\n mapi[root->left->val].push_back(root->val);\n }\n if(root->right!=NULL){\n mapi[root->val].push_back(root->right->val);\n mapi[root->right->val].push_back(root->val);\n }\n markConnection(root->left);\n markConnection(root->right);\n }\n int amountOfTime(TreeNode* root, int start) {\n markConnection(root);\n queue<int>q;\n q.push(start);\n q.push(-1);\n int time=0;\n while(q.front()!=-1){\n while(q.front()!=-1){\n vector<int>v=mapi[q.front()];\n cout<<\"for: \"<<q.front()<<endl;\n for(int i:v){\n if(mapi.find(i)!=mapi.end()){\n q.push(i);\n cout<<\"pushing: \"<<i<<endl;\n } \n\n }\n mapi.erase(q.front());\n q.pop();\n }\n q.pop();\n q.push(-1);\n time++;\n }\n return time-1;\n }\n};",
"memory": "180700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n unordered_map<int, vector<int>> adj;\n\n int amountOfTime(TreeNode* root, int start) {\n populateAdj(root, nullptr);\n return dfs(start, -1) - 1; // -1 because start node doesn't need any time to get infected\n }\n\n void populateAdj(TreeNode* node, TreeNode* parent) {\n if (node == nullptr) return;\n\n if (parent != nullptr) adj[node->val].push_back(parent->val);\n if (node->left != nullptr) {\n adj[node->val].push_back(node->left->val);\n populateAdj(node->left, node);\n }\n if (node->right != nullptr) {\n adj[node->val].push_back(node->right->val);\n populateAdj(node->right, node);\n }\n }\n\n int dfs(int currNode, int prevNode) {\n int spread = 0;\n for (int neighbor : adj[currNode]) {\n if (neighbor == prevNode) continue;\n spread = max(spread, dfs(neighbor, currNode));\n }\n return 1 + spread;\n }\n};",
"memory": "183500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n unordered_map<int,vector<int>> adj ;\n bool vst[100002] ;\n\n int ans = 0 ;\n\n void makeList(TreeNode* root , TreeNode* parent)\n {\n if (!root) return ;\n \n if (root->left) adj[root->val].push_back(root->left->val) ;\n if (root->right) adj[root->val].push_back(root->right->val) ;\n if (parent) adj[root->val].push_back(parent->val) ;\n\n makeList(root->left,root) ;\n makeList(root->right,root) ;\n }\n void dfs(int curr , int dist)\n {\n ans = max(ans,dist) ;\n\n vst[curr] = true ; \n\n for (auto elm : adj[curr]) \n {\n if (!vst[elm]) \n {\n dfs(elm,dist + 1) ;\n }\n }\n }\n int amountOfTime(TreeNode* root, int start) \n {\n adj.clear() ;\n memset(vst,false,sizeof(vst)) ;\n makeList(root,nullptr) ;\n dfs(start,0) ;\n return ans ;\n }\n};",
"memory": "183500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void getParent(TreeNode*root, map<TreeNode*, TreeNode*>& ans){\n if (root == NULL){\n return;\n }\n if (root -> left){\n ans[root -> left] = root;\n }\n if (root -> right){\n ans[root -> right] = root;\n }\n\n getParent(root -> left, ans);\n getParent(root -> right, ans);\n }\n\n TreeNode* getTargetNode(TreeNode* root, int start){\n if (root == NULL){\n return NULL;\n }\n\n if (root -> val == start){\n return root;\n }\n\n TreeNode* left = getTargetNode(root -> left, start);\n TreeNode* right = getTargetNode(root -> right, start);\n\n if (left == NULL){\n return right;\n }\n return left;\n }\n\n int solve(TreeNode* target, map<TreeNode*, TreeNode*> parentChild){\n \n queue<TreeNode*> q;\n map<TreeNode*, bool> check;\n int ans = 0;\n\n q.push(target);\n check[target] = true;\n\n while (!q.empty()){\n int size = q.size();\n bool flag = false;\n for (int i = 0; i < size; i++){\n\n TreeNode* front = q.front();\n q.pop();\n\n if (front -> left && !check[front -> left]){\n q.push(front -> left);\n check[front -> left] = true;\n flag = true;\n }\n\n if (front -> right && !check[front -> right]){\n q.push(front -> right);\n check[front -> right] = true;\n flag = true;\n }\n\n if (parentChild[front] && !check[parentChild[front]]){\n q.push(parentChild[front]);\n check[parentChild[front]] = true;\n flag = true;\n }\n\n }\n if (flag){\n ans++;\n }\n }\n return ans;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n ios_base::sync_with_stdio(false);\n\t cin.tie(NULL);\n \n int time = 0;\n\n if (root == NULL){\n return time;\n }\n\n map<TreeNode*, TreeNode*> parentChild;\n getParent(root, parentChild);\n\n TreeNode* targetNode = getTargetNode(root, start);\n\n time = solve(targetNode, parentChild);\n return time;\n }\n};",
"memory": "186300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int, vector<int>> adj;\n queue<TreeNode*> q;\n q.push(root);\n while (!q.empty()) {\n TreeNode* node = q.front();\n q.pop();\n if (node->left) {\n adj[node->val].push_back(node->left->val);\n adj[node->left->val].push_back(node->val);\n q.push(node->left);\n }\n if (node->right) {\n adj[node->val].push_back(node->right->val);\n adj[node->right->val].push_back(node->val);\n q.push(node->right);\n }\n }\n\n queue<int> que;\n que.push(start);\n unordered_set<int> visited;\n visited.insert(start);\n int minutes = 0;\n\n while (!que.empty()) {\n int n = que.size();\n\n while (n--) {\n int curr = que.front();\n que.pop();\n\n for (int &ngbr : adj[curr]) {\n if (visited.find(ngbr) == visited.end()) {\n que.push(ngbr);\n visited.insert(ngbr);\n }\n }\n }\n minutes++;\n }\n\n return minutes - 1;\n }\n};",
"memory": "186300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void mapping(TreeNode* root , unordered_map<TreeNode* , TreeNode*> &nodeToParent , TreeNode* &targetNode , int target){\n \n if(root == NULL) return;\n if(root -> val == target){\n targetNode = root;\n }\n if(root -> left){\n nodeToParent[root -> left] = root;\n mapping(root -> left , nodeToParent , targetNode , target);\n }\n if(root -> right){\n nodeToParent[root -> right] = root;\n mapping(root -> right , nodeToParent , targetNode , target);\n }\n }\n \n void burnTree(TreeNode* target , unordered_map<TreeNode* , TreeNode*> nodeToParent,\n int &time){\n queue<TreeNode*> q;\n q.push(target);\n unordered_map<TreeNode* , bool> visited;\n visited[target] = true;\n \n while(!q.empty()){\n int size = q.size();\n bool ifAddition = 0;\n for(int i = 0 ; i < size ; i++){\n \n TreeNode* front = q.front();\n q.pop();\n \n if(front -> left && visited[front -> left] != true){\n ifAddition = 1;\n visited[front -> left] = true;\n q.push(front -> left);\n }\n if(front -> right && visited[front -> right] != true){\n ifAddition = 1;\n visited[front -> right] = true;\n q.push(front -> right);\n }\n if(nodeToParent[front] != NULL && visited[nodeToParent[front]] != true){\n ifAddition = 1;\n visited[nodeToParent[front]] = true;\n q.push(nodeToParent[front]);\n }\n }\n if(ifAddition){\n time++;\n }\n }\n }\n\n\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode* , TreeNode*> nodeToParent;\n unordered_map<TreeNode* , bool> visited;\n TreeNode* targetNode = root;\n nodeToParent[root] = NULL;\n \n mapping(root , nodeToParent , targetNode , start);\n \n int ans = 0;\n burnTree(targetNode , nodeToParent, ans);\n return ans;\n }\n};",
"memory": "189100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n \n //if in the q unique values was not given then we need to make graph of <node*,vector<node*>>\n \n unordered_map<TreeNode*,vector<TreeNode*>> m;\n queue<TreeNode*> q1;\n q1.push(root);\n TreeNode* starting;\n \n while(!q1.empty())\n {\n TreeNode* cur=q1.front();\n q1.pop();\n \n if(cur->left)\n { \n q1.push(cur->left);\n m[cur].push_back(cur->left);\n m[cur->left].push_back(cur);\n }\n if(cur->right)\n { \n q1.push(cur->right);\n m[cur].push_back(cur->right);\n m[cur->right].push_back(cur);\n }\n if(cur->val==start) starting=cur;\n }\n \n int ans=0;\n queue<TreeNode*> q;\n q.push(starting);\n unordered_set<TreeNode*> s;\n s.insert(starting);\n \n while(!q.empty())\n {\n int size=q.size();\n \n for(int i=0;i<size;i++)\n { \n auto cur=q.front();\n q.pop();\n \n for(auto x:m[cur])\n {\n if(s.find(x)==s.end())\n {\n s.insert(x);\n q.push(x);\n }\n }\n }\n ans++;\n }\n \n return ans-1;\n \n } \n \n};",
"memory": "189100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void dfs(TreeNode *root, unordered_map<int,vector<int>> &parent) {\n if(!root)\n return;\n if(root->left) {\n parent[root->val].push_back(root->left->val);\n parent[root->left->val].push_back(root->val);\n dfs(root->left, parent);\n }\n if(root->right) {\n parent[root->val].push_back(root->right->val);\n parent[root->right->val].push_back(root->val);\n dfs(root->right, parent);\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int, vector<int>> graph;\n dfs(root, graph);\n \n queue<int> q {};\n q.push(start);\n set<int> st {};\n st.insert(start);\n \n int time = 0;\n while(!q.empty()) {\n int sz = q.size();\n while(sz--) {\n int top = q.front();\n q.pop();\n\n for(auto nbr: graph[top]) { \n if(st.find(nbr) != st.end())\n continue;\n st.insert(nbr);\n q.push(nbr);\n }\n }\n time++;\n }\n return time - 1;\n }\n};",
"memory": "191900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<int, vector<int>> adjList;\n void inorder(TreeNode* root){\n if(root == NULL){\n return;\n }\n if(root->left != NULL){\n adjList[root->val].push_back(root->left->val);\n adjList[root->left->val].push_back(root->val);\n inorder(root->left);\n }\n if(root->right != NULL){\n adjList[root->val].push_back(root->right->val);\n adjList[root->right->val].push_back(root->val);\n inorder(root->right);\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n inorder(root);\n /*for(auto itr = adjList.begin(); itr!=adjList.end();itr++){\n int u = itr->first;\n cout<<u<<\": \"<<\" \";\n for(int v : itr->second){\n cout<<v<<\" \";\n }\n cout<<endl;\n }*/\n queue<int> q;\n set<int> visited;\n visited.insert(start);\n for(int v : adjList[start]){\n if(visited.find(v) == visited.end()){\n q.push(v);\n }\n }\n int time = 0;\n while(!q.empty()){\n //cout<<\"time: \"<<time<<endl;\n int size = q.size();\n while(size > 0){\n int u = q.front();\n //cout<<endl<<u;\n q.pop();\n visited.insert(u);\n for(int v : adjList[u]){\n if(visited.find(v) == visited.end()){\n q.push(v);\n }\n }\n size--;\n }\n time++;\n }\n return time;\n }\n};\n\n/*\n\nadj list\n\n1 - 5, 3\n2 - 4\n3 - 10, 6, 1\n4 - 2, 5, 9\n5 - 1, 4\n6 - 3\n9 - 4\n10 - 3\n\nBFS - 3 10 6 1 5 4 2 9\ntime = 1 + 1 + 1 + 1\n*/",
"memory": "191900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void mapWithParentNodes(TreeNode* root,unordered_map<TreeNode*,TreeNode*> &mp,int target,TreeNode* &req){\n if(root == NULL) return;\n if(root->left){\n mp[root->left] = root;\n }\n if(root->right){\n mp[root->right] = root;\n }\n mapWithParentNodes(root->left,mp,target,req);\n mapWithParentNodes(root->right,mp,target,req);\n if(root->val == target) req = root;\n }\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*,TreeNode*> mp;\n TreeNode* target = NULL;\n mapWithParentNodes(root,mp,start,target);\n unordered_map<TreeNode*,bool> vis;\n queue<TreeNode*> q;\n int time = 0;\n q.push(target);\n vis[target] = true;\n while(!q.empty()){\n int size = q.size();\n for(int i=0;i<size;i++){\n TreeNode* curr = q.front();\n q.pop();\n if(curr->left && !vis[curr->left]){\n q.push(curr->left);\n vis[curr->left] = 1;\n }\n if(curr->right && !vis[curr->right]){\n q.push(curr->right);\n vis[curr->right] = 1;\n }\n if(mp[curr] && !vis[mp[curr]]){\n q.push(mp[curr]);\n vis[mp[curr]] = 1;\n }\n }\n time++;\n }\n return time-1;\n }\n};",
"memory": "194700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<int, vector<int>>um;\n unordered_map<int,int>vis;\n int maxVal;\n int amountOfTime(TreeNode* root, int start) {\n int ans = 0;\n dfs(root);\n // vector<int>vis(maxVal, 0);\n //cout << \"maxVal\" << maxVal << endl;\n queue<pair<int,int>>q;\n q.push({start, 0});\n vis[start] = 1;\n\n bool flag = false;\n while(!q.empty())\n {\n pair<int,int>temp = q.front();\n\n int node = temp.first;\n int dist = temp.second;\n ans = max(ans, dist);\n q.pop();\n // flag = false;\n // cout << \"node:\" << node << \", adjNodes:\";\n for (auto adjNode : um[node])\n {\n // cout << adjNode << \" \";\n if (vis.find(adjNode) == vis.end())\n {\n \n q.push({adjNode, dist + 1});\n vis[adjNode] = 1;\n }\n }\n //cout << endl;\n }\n return ans;\n }\n\n void dfs(TreeNode *root)\n {\n if (!root)\n return;\n \n // um[root]\n maxVal = max(maxVal, root->val);\n if (root->left)\n {\n um[root->val].push_back(root->left->val);\n um[root->left->val].push_back(root->val);\n }\n\n if (root->right)\n {\n um[root->val].push_back(root->right->val);\n um[root->right->val].push_back(root->val);\n }\n dfs(root->left);\n dfs(root->right);\n return;\n }\n};",
"memory": "194700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\nTreeNode * getNode(TreeNode * root,int target){\n if(root==NULL){return NULL;}\n if(root->val==target){return root;}\n TreeNode * lft=getNode(root->left,target);\n if(lft!=NULL){return lft;}\n return getNode(root->right,target);\n}\n\nvoid getParent(TreeNode * root, unordered_map<TreeNode *, pair<TreeNode *,int>> &mp){\n if(root->left!=NULL){\n mp[root->left].first=root;\n mp[root->left].second=1;\n getParent(root->left,mp);\n }\n if(root->right!=NULL){\n mp[root->right].first=root;\n mp[root->right].second=0;\n getParent(root->right,mp);\n }\n}\n\n void traversal(TreeNode * root, unordered_map<TreeNode *, int> &cache, int time, int dir){\n if(root==NULL){return ;}\n if(cache.find(root)==cache.end()){\n cache[root]=time;\n }\n if(dir==0){\n traversal(root->left,cache,time+1,2);\n } else if(dir==1){\n traversal(root->right,cache,time+1,2);\n } else{\n traversal(root->left,cache,time+1,2);\n traversal(root->right,cache,time+1,2);\n }\n}\n\n int amountOfTime(TreeNode* root, int start) {\n TreeNode * node=getNode(root,start);\n unordered_map<TreeNode *, pair<TreeNode *,int>> mp;\n getParent(root,mp);\n unordered_map<TreeNode *, int> cache;\n int time=0;\n traversal(node,cache,time,2);\n while(mp.find(node)!=mp.end()){\n time+=1;\n int dir=mp[node].second;\n node=mp[node].first;\n traversal(node,cache,time,dir);\n }\n int ans=INT_MIN;\n for(auto x:cache){\n ans=max(ans,x.second);\n }\n return ans;\n }\n};",
"memory": "197500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*, vector<TreeNode*>> mp;\n queue<TreeNode*> q;\n q.push(root);\n TreeNode* head = NULL;\n while(!q.empty()) {\n TreeNode* node = q.front();\n if(head == NULL && node->val == start) {\n head = node;\n }\n if(node -> left) {\n mp[node].push_back(node->left);\n mp[node->left].push_back(node);\n q.push(node->left);\n }\n if(node->right) {\n mp[node].push_back(node->right);\n mp[node->right].push_back(node);\n q.push(node->right);\n }\n q.pop();\n }\n\n int ans = 0;\n unordered_map<TreeNode*, int> visitedMap;\n queue<pair<TreeNode*, int>> q2;\n q2.push({head, 0});\n // cout<<head -> val <<endl;\n visitedMap[head] = 1;\n while(!q2.empty()) {\n TreeNode* node = q2.front().first;\n int n = q2.front().second;\n ans = max(ans, n);\n for(TreeNode* nodeing: mp[node]) {\n if(visitedMap[nodeing] != 1) {\n visitedMap[nodeing] = 1;\n q2.push({nodeing, n+1}); \n }\n }\n q2.pop();\n }\n return ans;\n }\n};",
"memory": "197500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int target) {\n unordered_map<int, list<int>> adjList;\n\n queue<TreeNode*> q;\n q.push(root);\n while (!q.empty()) {\n TreeNode* fnode = q.front();\n q.pop();\n\n if (fnode->left) {\n q.push(fnode->left);\n adjList[fnode->val].push_back(fnode->left->val);\n adjList[fnode->left->val].push_back(fnode->val);\n }\n if (fnode->right) {\n q.push(fnode->right);\n adjList[fnode->val].push_back(fnode->right->val);\n adjList[fnode->right->val].push_back(fnode->val);\n }\n }\n\n queue<pair<int, int>> qq;\n unordered_map<int, bool> visited;\n int minTime = INT_MIN;\n visited[target] = true;\n qq.push({0, target});\n\n while (!qq.empty()) {\n pair<int, int> p = qq.front();\n qq.pop();\n int src = p.second;\n int curD = p.first;\n\n for (auto nbr : adjList[src]) {\n if (!visited[nbr]) {\n visited[nbr] = true;\n qq.push({curD + 1, nbr});\n minTime = max(curD + 1, minTime);\n }\n }\n }\n if (minTime == INT_MIN)\n return 0;\n return minTime;\n }\n};",
"memory": "200300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int target) {\n unordered_map<int, list<int>> adjList;\n\n queue<TreeNode*> q;\n q.push(root);\n while (!q.empty()) {\n TreeNode* fnode = q.front();\n q.pop();\n\n if (fnode->left) {\n q.push(fnode->left);\n adjList[fnode->val].push_back(fnode->left->val);\n adjList[fnode->left->val].push_back(fnode->val);\n }\n if (fnode->right) {\n q.push(fnode->right);\n adjList[fnode->val].push_back(fnode->right->val);\n adjList[fnode->right->val].push_back(fnode->val);\n }\n }\n\n queue<pair<int, int>> qq;\n unordered_map<int, bool> visited;\n int minTime = INT_MIN;\n visited[target] = true;\n qq.push({0, target});\n\n while (!qq.empty()) {\n pair<int, int> p = qq.front();\n qq.pop();\n int src = p.second;\n int curD = p.first;\n\n for (auto nbr : adjList[src]) {\n if (!visited[nbr]) {\n visited[nbr] = true;\n qq.push({curD + 1, nbr});\n minTime = max(curD + 1, minTime);\n }\n }\n }\n if (minTime == INT_MIN)\n return 0;\n return minTime;\n }\n};",
"memory": "200300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void solve(unordered_map<int,list<int>> &mp,TreeNode* root){\n if(root->left){\n mp[root->val].push_back(root->left->val);\n mp[root->left->val].push_back(root->val);\n solve(mp,root->left);\n }\n if(root->right){\n mp[root->val].push_back(root->right->val);\n mp[root->right->val].push_back(root->val);\n solve(mp,root->right);\n }\n }\n\n\n\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int,list<int>> mp;\n queue<int> q;\n int time=0;\n unordered_map<int,int> visited;\n solve(mp,root);\n q.push(start);\n while(!q.empty()){\n int sz=q.size();\n while(sz--){\n auto top=q.front();q.pop();\n visited[top]=1;\n for(auto i:mp[top]){\n if(!visited[i])q.push(i);\n }\n }\n if(!q.empty())time++; \n }\n return time;\n }\n};",
"memory": "203100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n void solve(TreeNode* root,map <TreeNode*,TreeNode*>& parent,map <TreeNode*,bool>& check,int& ans,int lvl){\n\n if (root == nullptr){\n return;\n }\n\n if (check[root] == true){\n return;\n }\n\n check[root] = true;\n ans = max(ans,lvl);\n if (root -> left != nullptr){\n solve(root->left,parent,check,ans,lvl+1);\n }\n if (parent[root] != nullptr){\n solve(parent[root],parent,check,ans,lvl+1);\n }\n if (root -> right != nullptr){\n solve(root->right,parent,check,ans,lvl+1);\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n \n map <TreeNode*,TreeNode*> parent;\n\n parent[root] = nullptr;\n\n queue <TreeNode*> value;\n value.push(root);\n\n TreeNode* target = nullptr;\n\n while (!value.empty()){\n\n TreeNode* first = value.front();\n\n if (first -> val == start){\n target = first;\n }\n\n value.pop();\n\n if (first -> left != nullptr){\n parent[first->left] = first;\n value.push(first ->left);\n }\n if (first -> right != nullptr){\n parent[first->right] = first;\n value.push(first -> right);\n }\n\n }\n\n map <TreeNode*,bool> check;\n int ans = 0 ;\n\n solve(target,parent,check,ans,0);\n\n return ans;\n\n }\n};",
"memory": "203100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n unordered_map<TreeNode*, TreeNode*> par;\n\n TreeNode* srcNode = nullptr;\n int srcVal = -1;\n void traverse(TreeNode* root, TreeNode* parent) {\n if(!root) return;\n\n\n if(root->val == srcVal) srcNode = root;\n\n par[root] = parent;\n traverse(root->left, root);\n traverse(root->right, root);\n } \n\n int bfs() {\n \n int time = 0;\n unordered_map<TreeNode*, bool> vis;\n\n queue<TreeNode*> q;\n q.push(srcNode);\n vis[srcNode] = 1;\n\n while(!q.empty()) {\n \n int qSize = q.size();\n while(qSize--) {\n TreeNode* cur = q.front();\n q.pop();\n\n \n\n for(TreeNode* adjNode : {cur->left, cur->right, par[cur]}) {\n if(adjNode == nullptr) continue;\n if(adjNode && !vis[adjNode]) {\n vis[adjNode] = 1;\n q.push(adjNode);\n }\n }\n }\n time++;\n }\n \n return time-1;\n }\n \npublic:\n int amountOfTime(TreeNode* root, int start) {\n srcVal = start;\n traverse(root, nullptr);\n return bfs();\n }\n};",
"memory": "205900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<TreeNode*,TreeNode*> parent;\n unordered_set<TreeNode*> infected;\n int result=0;\n TreeNode* target=NULL;\n findparent(root,parent);\n findstart(root,start,target);\n infect(target,parent,infected,0,result);\n return result;\n }\n\n void findparent(TreeNode* root,unordered_map<TreeNode*,TreeNode*> &parent){\n if(!root) return;\n if(root->left){\n parent[root->left]=root;\n findparent(root->left,parent);\n }\n if(root->right){\n parent[root->right]=root;\n findparent(root->right,parent);\n }\n }\n\n void findstart(TreeNode* root,int &start,TreeNode* &target){\n if(!root) return;\n if(root->val==start){\n target=root;\n return;\n }\n findstart(root->left, start, target);\n findstart(root->right, start, target);\n }\n\n void infect(TreeNode* root,unordered_map<TreeNode*,TreeNode*> &parent,unordered_set<TreeNode*> &infected,int minutes,int &result){\n if(!root){\n result=max(result,minutes-1);\n return;\n }\n if(infected.count(root)) return;\n infected.insert(root);\n\n infect(root->left,parent,infected,minutes+1,result);\n infect(root->right,parent,infected,minutes+1,result);\n infect(parent[root],parent,infected,minutes+1,result);\n }\n};",
"memory": "205900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n //parent question (code copied from, later modified; comments are outdated/wrong)- https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/\n //NOTE- Almost similar to parent question, we simply run DFS and return the distance of farthest away node from start (start is assumed to be target node)\n //Working- we intend to do DFS from root. For this we first calculate distance of target from root. Suppose a node is at distance 'x'\n //from target then it's child will be distance 'x+1' from target (if target is not in child subtree), but child will be at dist 'x-1'\n //if target is in subtree of child. To know whether the target is subtree of a given node or not. We calculate path from root to target\n //and add all the path nodes in a set (presence of node in set denotes target is n subtree of node) and moving downwards from node's parent\n //to that node will actually reduce the distance. If node is not in set, moving from parent to that node will increase the distance\n //Note: all the nodes in root to target path are actually the ancestor nodes of the target\n int dfsApproachByPreparingAncestorNodesSet(TreeNode* root, int targetNodeVal){\n //vector<int> answerNodes; //our answer (nodes at distance k from target; \n unordered_set<int> ancestorNodes; //set to store all the nodes of \"root to target\" path (ancestor nodes of target)\n prepareAncestorNodesSetUsingDFS(root, targetNodeVal, ancestorNodes);\n int distanceBetweenRootAndTarget = ancestorNodes.size() - 1; //this is the distance between root node and target node\n //to start the DFS from root we need it's distance from target, so we can calculate it's child's distance from target \n //why we are doing + 2? in dfs function we will realise that target is in subtree of root. That function assumes that parent node always increases the distance by 1\n //to correct the distance (in case target is in subtree of currNode) it will reduce the distance by 2. If confused see comments on dfsAndCalculateDistanceFromTarget\n return dfsAndCalculateDistanceFromTarget(root, distanceBetweenRootAndTarget + 2, ancestorNodes); //prepare answer nodes by doing dfs on tree from root\n }\n\n //traverse over tree in dfs style and prepare a vector containing all the nodes from root to target. return value- whether targetNode is present in subtree of currNode\n //or not (need this to determine whether to keep or remove currNode while backtracking)\n bool prepareAncestorNodesSetUsingDFS(TreeNode* currNode, int targetNodeVal, unordered_set<int>& ancestorNodes){\n if(currNode == NULL){\n return false;\n }\n ancestorNodes.insert(currNode->val); //we assume currNode is in path from root to target\n if(currNode->val == targetNodeVal){ //currNode is actually the target node\n return true; //return true so that previous recursion call (parent) knows that target is in that node's subtree\n }\n //call to left and right subtree\n bool isTargetPresentInLeftSubtree = prepareAncestorNodesSetUsingDFS(currNode->left, targetNodeVal, ancestorNodes);\n bool isTargetPresentInRightSubtree = prepareAncestorNodesSetUsingDFS(currNode->right, targetNodeVal, ancestorNodes);\n if(isTargetPresentInLeftSubtree || isTargetPresentInRightSubtree){ //if target is actuallu present in curent subtree\n return true; //return true and do not remove currNode from ancestorNodes\n }\n else{ //if target node is not found in currNode's subtree\n ancestorNodes.erase(currNode->val); //remove currNode from ancestors\n return false; //return false denoting target was not found in currNode's subtree\n }\n }\n\n //do a dfs from root. if dist of currNode from target = x, then distance of currnode's child from target = x+1 (while making dfs calls we assume that \n //target is not in child's subtree). While processing child we check if target is in it's (child's) subtree, if it is we do currDistance-=2 (refer below)\n int dfsAndCalculateDistanceFromTarget(TreeNode* currNode, int currNodeDistFromTarget, unordered_set<int>& ancestorNodes){\n if(currNode == NULL){\n return INT_MIN;\n }\n if(ancestorNodes.find(currNode->val) != ancestorNodes.end()){ //the target node is actually in currNode's subtree, so instead of increasing the distance by 1\n //we should've reduced the distance by 1, so now we reduce it by 2\n currNodeDistFromTarget -= 2;\n }\n\n //cout<<currNode->val<<\" \"<<currNodeDistFromTarget<<endl;\n\n //(while making dfs calls) we assume that target is not in subtree of any child so we increase their distances by 1\n //however if target is actually present in their (child's) subtree, we will correct it later (when function runs for those childs)\n int maxDistFromLeftSubtree = dfsAndCalculateDistanceFromTarget(currNode->left, currNodeDistFromTarget + 1, ancestorNodes);\n int maxDistFromRightSubtree = dfsAndCalculateDistanceFromTarget(currNode->right, currNodeDistFromTarget + 1, ancestorNodes);\n //why we are considering currNodeDist also? in case currNode is a leaf node\n return max(currNodeDistFromTarget, max(maxDistFromLeftSubtree, maxDistFromRightSubtree));\n }\n\n //for all the nodes prepare a parent vector such that vec[i] gives the parent node of the node with value 'i'\n //we also save target node in a reference variable when we find it\n void dfsTraverseAndPrepareParents(TreeNode* currNode, vector<TreeNode*>& parentNodes, int targetNodeVal, TreeNode*& target){\n if(currNode->val == targetNodeVal){\n target = currNode;\n }\n if(currNode->left != NULL){\n parentNodes[currNode->left->val] = currNode; //the parent node for left child's (value) is currNode\n dfsTraverseAndPrepareParents(currNode->left, parentNodes, targetNodeVal, target);\n }\n if(currNode->right != NULL){\n parentNodes[currNode->right->val] = currNode; //the parent node for right child's (value) is currNode\n dfsTraverseAndPrepareParents(currNode->right, parentNodes, targetNodeVal, target);\n }\n }\n\n\n //parent question (code copied from, later modified; COMMENTS ARE COPIED/WRONG)- https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/\n //NOTE- Almost similar to parent question, we simply run BFS from target/start node and return the distance of farthest away node from start/target\n //Intuition: We want to do a BFS from the target node so we can visit and store all nodes at 'k' distance from target.\n //In tree however by default we can't move in all the possible direction and can only move in downward direction.\n //To move in upward direction we need to store parents of all nodes in some DS like array/ map, using which we can move upwards also.\n //Effectively we are converting the tree to a undirected graph and then running BFS from target node upto k distance nodes \n int storeParentAndDoBFS(TreeNode* root, int targetNodeVal){\n //parent of any node say x can be found as parentNodes[x->val];\n vector<TreeNode*> parentNodes((1e5)+2, NULL); //why (1e5)+1? because 1 <= Node.val <= 10^5\n TreeNode* target;\n dfsTraverseAndPrepareParents(root, parentNodes, targetNodeVal, target); //prepare parentNodes vector, also save target node\n vector<int> answerNodes; //our answer (nodes at distance k from target)\n //do a BFS from currNode\n queue<TreeNode*> nextNodesToVisit;\n nextNodesToVisit.push(target); //start BFS traversal from target/start node\n //just after/before the start of the for loop, all the nodes present in queue will be at same distance\n int distanceOfCurrentNodesInQueue = 0; //distance of current nodes in queue from target at the abovementioned point\n vector<int> visited((1e5)+2, false); //if we have visited a node say x, then visited[x->val] will be true, this is needed\n //so that we don't visit previous node from the next node again (in a circular manner)\n while(!nextNodesToVisit.empty()){\n int nodesAtCurrentDistance = nextNodesToVisit.size(); //at this point the queue will only contain all (and only) the nodes of tree \n //whose distance from target = distanceOfCurrentNodesInQueue\n for(int index = 0; index < nodesAtCurrentDistance; ++index){ //process all the node with a specific distance from target in this loop \n TreeNode* currNode = nextNodesToVisit.front();\n nextNodesToVisit.pop();\n visited[currNode->val] = true;\n //move towards tree parent (not the bfs parent), it will be at currNodeDistance + 1 distance from target \n //and will be traversed in next for loop and not this one\n if(parentNodes[currNode->val] != NULL && (!visited[parentNodes[currNode->val]->val])){ //if currNode has a unvisited parent\n nextNodesToVisit.push(parentNodes[currNode->val]); //add parent to BFS queue\n }\n //add left/right unvisited child in BFS queue as usual (they will be at currNodeDistance + 1 distance from target)\n if(currNode->left != NULL && (!visited[currNode->left->val])){\n nextNodesToVisit.push(currNode->left);\n }\n if(currNode->right != NULL && (!visited[currNode->right->val])){\n nextNodesToVisit.push(currNode->right);\n }\n }\n distanceOfCurrentNodesInQueue += 1; //at this point, all the nodes present in queue will be at this distance\n }\n return distanceOfCurrentNodesInQueue-1; //last increment was done when queue was empty (there wee no nodes at that distance)\n }\n\n //TODO- update comments, they are copied from parent question and are wrong\n //TODO- Other approaches, DFS approach is quite slow\n int amountOfTime(TreeNode* root, int start) {\n //return dfsApproachByPreparingAncestorNodesSet(root, start);\n\n return storeParentAndDoBFS(root, start);\n }\n};",
"memory": "208700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n void dfs(TreeNode* curr, TreeNode* prev,\n unordered_map<TreeNode*, TreeNode*>& m) {\n if (!curr)\n return;\n m[curr] = prev;\n dfs(curr->left, curr, m);\n dfs(curr->right, curr, m);\n }\n int bfs(TreeNode* curr, unordered_map<TreeNode*, TreeNode*>& m) {\n int ans = 0;\n unordered_map<TreeNode*, int> visited;\n queue<TreeNode*> q;\n q.push(curr);\n visited[curr] = 1;\n int level = 0;\n while (!q.empty()) {\n int size = q.size();\n ans = max(level, ans);\n for (int i = 0; i < size; i++) {\n TreeNode* front = q.front();\n q.pop();\n if (front->left and !visited[front->left]) {\n q.push(front->left);\n visited[front->left] = 1;\n }\n if (front->right and !visited[front->right]) {\n q.push(front->right);\n visited[front->right] = 1;\n }\n if (m[front] and !visited[m[front]]) {\n q.push(m[front]);\n visited[m[front]] = 1;\n }\n }\n level++;\n }\n return ans;\n }\n\n bool findNode(TreeNode* root, int t, TreeNode* &ans) {\n if (!root)\n return false;\n if (root->val == t) {\n ans = root;\n return true;\n }\n if (findNode(root->left, t, ans))\n return true;\n if (findNode(root->right, t, ans))\n return true;\n return false;\n }\n\npublic:\n int amountOfTime(TreeNode* root, int start) {\n TreeNode* st = NULL;\n unordered_map<TreeNode*, TreeNode*> par;\n findNode(root, start, st);\n dfs(root, NULL, par);\n return bfs(st, par);\n }\n};",
"memory": "208700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n TreeNode* get(TreeNode* root,int start){\n if(root==nullptr)return root;\n // if(root->left==nullptr and root->right==nullptr and root->val!=start)return nullptr;\n if(root->val==start){\n return root;\n }\n TreeNode* le=nullptr;\n TreeNode* ri=nullptr;\n if(root->left){\n le= get(root->left,start);\n }\n if(root->right){\n ri= get(root->right,start);\n }\n if(le)return le;\n return ri;\n }\n int time(unordered_map<TreeNode*,TreeNode*> &map,TreeNode* st,set<int> &visit){\n int ans=0;\n visit.insert(st->val);\n if(map[st] and visit.find(map[st]->val)==visit.end()){\n ans=max(ans,1+time(map,map[st],visit));\n }\n if(st->left and visit.find(st->left->val)==visit.end()){\n ans=max(ans,1+time(map,st->left,visit));\n }\n if(st->right and visit.find(st->right->val)==visit.end()){\n ans=max(ans,1+time(map,st->right,visit));\n }\n return ans;\n }\n int amountOfTime(TreeNode* root, int start) {\n TreeNode* node=get(root,start);\n unordered_map<TreeNode*,TreeNode*> map;\n queue<TreeNode*> q;\n q.push(root);\n map[root]=nullptr;\n while(!q.empty()){\n auto t=q.front();\n q.pop();\n if(t->left){\n q.push(t->left);\n map[t->left]=t;\n }\n if(t->right){\n q.push(t->right);\n map[t->right]=t;\n }\n }\n set<int> visit;\n return time(map,node,visit);\n\n }\n};",
"memory": "211500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n TreeNode* first = NULL;\n \n void find(TreeNode* root, int start){\n if(root==NULL) return;\n if(root->val==start){\n first=root;\n return;\n }\n find(root->left,start);\n find(root->right,start);\n }\n\n void mapParent(TreeNode* root, unordered_map<TreeNode*,TreeNode*> &mp){\n if(root==NULL) return;\n if(root->left) mp.insert({root->left,root});\n if(root->right) mp.insert({root->right,root});\n mapParent(root->left,mp);\n mapParent(root->right,mp);\n }\n \n\n int amountOfTime(TreeNode* root, int start) {\n find(root,start);\n set<TreeNode*> s;\n s.insert(first);\n\n unordered_map<TreeNode*,TreeNode*> mp;\n mapParent(root,mp);\n\n queue<pair<TreeNode*,int>> q;\n q.push({first,0});\n \n int maxLevel = 0;\n\n while(!q.empty()){\n auto it = q.front();\n q.pop();\n\n TreeNode* temp = it.first;\n int level = it.second;\n \n if(temp->left and s.find(temp->left)==s.end()){\n q.push({temp->left,level+1});\n s.insert(temp->left);\n }\n if(temp->right and s.find(temp->right)==s.end()){\n q.push({temp->right,level+1});\n s.insert(temp->right);\n }\n\n if(mp.find(temp)!=mp.end() and s.find(mp[temp])==s.end()){\n q.push({mp[temp],level+1});\n s.insert(mp[temp]);\n }\n\n maxLevel = max(maxLevel,level);\n } \n return maxLevel;\n }\n};",
"memory": "211500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void build(TreeNode* node, unordered_map<int, vector<int>> &neighbors) {\n if (node == nullptr) {\n return;\n }\n if (node->left) {\n neighbors[node->val].push_back(node->left->val);\n neighbors[node->left->val].push_back(node->val);\n }\n if (node->right) {\n neighbors[node->val].push_back(node->right->val);\n neighbors[node->right->val].push_back(node->val);\n }\n build(node->left, neighbors);\n build(node->right, neighbors);\n }\n\n\n void dfs(int node, set<int>& visited, unordered_map<int, vector<int>> &neighbors, int d, int& maxd) {\n maxd = max(maxd, d);\n if (visited.size() == neighbors.size()) {\n return;\n }\n\n const auto& ns = neighbors[node];\n for (int n : ns) {\n if (visited.find(n) == visited.end()) {\n visited.insert(n);\n dfs(n, visited, neighbors, d + 1, maxd);\n }\n }\n }\n\n int amountOfTime(TreeNode* root, int start) {\n int time = 0;\n unordered_map<int, vector<int>> neighbors;\n set<int> visited = {start};\n build(root, neighbors);\n dfs(start, visited, neighbors, 0, time);\n return time;\n }\n};",
"memory": "214300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int counter = 0;\n void solve(TreeNode* root, int start, unordered_map<TreeNode*, TreeNode*>& child_to_parent) {\n if (root == NULL) {\n return;\n }\n\n \n TreeNode* leftChild = root->left;\n TreeNode* rightChild = root->right;\n\n if (leftChild != NULL){\n child_to_parent[leftChild] = root;\n solve(root->left, start, child_to_parent);\n }\n \n\n if (rightChild != NULL){\n child_to_parent[rightChild] = root;\n solve(root->right, start, child_to_parent);\n }\n }\n\n TreeNode* findNode(TreeNode* root, int start) {\n if(root == NULL){\n return NULL;\n }\n if(root->val == start){\n return root;\n }\n\n TreeNode* leftAns = findNode(root->left, start);\n\n if(leftAns != NULL){\n return leftAns;\n }\n\n return findNode(root->right, start);\n }\n void levelOrderTraversal(TreeNode* root, int start, unordered_map<TreeNode*, TreeNode*>& child_to_parent){\n if(root == NULL){\n return;\n }\n\n unordered_map<TreeNode*, bool> visited;\n\n queue<TreeNode*> q;\n TreeNode* node = findNode(root, start);\n q.push(node);\n visited[node] = true;\n q.push(NULL);\n\n while(!q.empty()){\n TreeNode* getFront = q.front();\n q.pop();\n\n if(getFront == NULL){\n cout << endl; \n counter++;\n\n if(!q.empty()){\n q.push(NULL);\n }\n }else{\n cout << getFront->val << \" \";\n\n if(getFront->left != NULL && !visited[getFront->left]){\n q.push(getFront->left);\n visited[getFront->left] = true;\n }\n\n if(getFront->right != NULL && !visited[getFront->right]){\n q.push(getFront->right);\n visited[getFront->right] = true;\n }\n \n if(child_to_parent.find(getFront) != child_to_parent.end()){\n if(!visited[child_to_parent[getFront]]){\n q.push(child_to_parent[getFront]);\n visited[child_to_parent[getFront]] = true;\n } \n }\n }\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n // infection starts from 3 at 0 min\n // at 1 minutes adjacents will be infected 1,10,6\n // at 2 minutes 5 will be infected\n // at 3 minutes 4 will be infected\n // at 4 minutes 9,2 will be infected\n // output will be 4 complete tree infected at in 4 minutes\n\n // How to traverse back? \n // Infection spreading level by level\n\n unordered_map<TreeNode*, TreeNode*> child_to_parent;\n solve(root, start, child_to_parent);\n\n levelOrderTraversal(root, start, child_to_parent);\n return counter - 1; // why -1 because starting of 0 minute first endl will not include after 3\n }\n};",
"memory": "217100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n queue<TreeNode*> nodes;\n nodes.push(root);\n map<TreeNode*,int> dist;\n // map<TreeNode*,TreeNode*> parent;\n map<TreeNode*,vector<TreeNode*>> adj;\n TreeNode* infection;\n\n while(nodes.size()!=0){\n // num_nodes++;\n TreeNode* node = nodes.front();\n nodes.pop();\n if(node->val==start){\n infection=node;\n }\n if(node->left!=NULL){\n nodes.push(node->left);\n adj[node->left].push_back(node);\n adj[node].push_back(node->left);\n // parent[node->left] = node;\n }\n if(node->right!=NULL){\n nodes.push(node->right);\n adj[node->right].push_back(node);\n adj[node].push_back(node->right);\n // parent[node->right] = node;\n }\n }\n nodes.push(infection);\n map<TreeNode*,int> mark;\n int ans=0;\n while(nodes.size()!=0){\n // num_nodes++;\n TreeNode* node = nodes.front();\n nodes.pop();\n mark[node]=1;\n for(int i=0;i<adj[node].size();i++){\n if(mark[adj[node][i]]) continue;\n nodes.push(adj[node][i]);\n dist[adj[node][i]]=dist[node]+1;\n ans=max(ans,dist[node]+1);\n }\n }\n return ans;\n }\n};",
"memory": "217100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n getHeight(root);\n return getInfectedTime(root, start, 0);\n }\n\nprivate:\n std::unordered_map<int, int> leftHeight;\n std::unordered_map<int, int> rightHeight;\n\n void getHeight(TreeNode* root) {\n if (!root) return;\n getHeight(root->left);\n getHeight(root->right);\n if (root->left) leftHeight[root->val] = max(leftHeight[root->left->val], rightHeight[root->left->val]) + 1;\n if (root->right) rightHeight[root->val] = max(leftHeight[root->right->val], rightHeight[root->right->val]) + 1;\n }\n\n int getInfectedTime(TreeNode* root, int start, int acc) {\n if (!root) {\n return -1;\n }\n if (root->val == start) {\n return max(acc, max(leftHeight[start], rightHeight[start]));\n }\n return max(getInfectedTime(root->left, start, max(rightHeight[root->val], acc) + 1),\n getInfectedTime(root->right, start, max(leftHeight[root->val], acc) + 1));\n }\n};\n",
"memory": "219900"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.