id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n const string letters = \"AEIOUaeiou\";\n unordered_set<char> vowels(letters.begin(), letters.end());\n unordered_map<char, int> vowels_cnt;\n\n for (int i = 0; i < s.size(); i++) {\n if (vowels.count(s[i])) {\n vowels_cnt[s[i]]++;\n }\n }\n\n int idx = 0;\n for (int i = 0; i < 10; i++) {\n if (vowels_cnt.count(letters[i])) {idx = i; break;}\n }\n for (int i = 0; i < s.size(); i++) {\n if (vowels_cnt.count(s[i])) {\n s[i] = letters[idx]; \n vowels_cnt[letters[idx]]--;\n if (vowels_cnt[letters[idx]] == 0) { \n idx++;\n while (idx < 10 && !vowels_cnt.count(letters[idx])) idx++;\n }\n }\n }\n return s;\n }\n};",
"memory": "17000"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n string ans = \"\";\n string vowels = \"\";\n string vowelSet = \"AEIOUaeiou\";\n\n for (char c : s) {\n if (vowelSet.find(c) != string::npos) {\n vowels += c;\n ans += '*';\n } else {\n ans += c;\n }\n }\n \n sort(vowels.begin(), vowels.end());\n \n int j = 0;\n for (int i = 0; i < ans.size(); ++i) {\n if (ans[i] == '*') {\n ans[i] = vowels[j++];\n }\n }\n \n return ans;\n }\n};\n",
"memory": "17100"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // Time/Space: O(n)/O(1)\n string sortVowels(string s) {\n // Use a hash table to check whether a character is a vowel or not\n unordered_set<char> vowels = {\n 'a', 'e', 'i', 'o', 'u',\n 'A', 'E', 'I', 'O', 'U'\n };\n\n // Count the number of each vowel\n map<char, int> vowel_count;\n for (char c : s) {\n if (vowels.count(c)) vowel_count[c]++;\n }\n\n // Replace the vowels in non-decreasing order\n for (char& c : s) {\n if (vowels.count(c)) {\n auto it = vowel_count.begin();\n c = it->first;\n it->second--; // Decrement the count\n if (it->second == 0) // Remove the vowel if the count is zero\n vowel_count.erase(it);\n }\n }\n return s;\n }\n};\n",
"memory": "17200"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n unordered_set<char> s1 = {'a', 'e', 'i', 'o', 'u'};\n\n priority_queue<char, vector<char>, greater<char>> pq;\n \n for (char c : s){\n if (s1.count(tolower(c))){\n pq.push(c);\n }\n }\n string result = \"\";\n for (char& c : s){\n if (s1.count(tolower(c))){\n c = pq.top();\n pq.pop();\n }\n }\n return s;\n \n }\n};",
"memory": "17300"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n bool isVowel(char ch){\n return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||\n ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';\n }\npublic:\n string sortVowels(string s) {\n int n = s.size();\n string v = \"\";\n\n vector<char> arr(n,'*');\n\n for(int i=0; i<n; i++){\n if(isVowel(s[i])) v += s[i];\n else arr[i] = s[i];\n }\n\n sort(v.begin(),v.end());\n int ptr = 0;\n\n for(int i=0; i<n; i++){\n if(arr[i] == '*'){\n arr[i] = v[ptr];\n ptr++;\n }\n }\n\n string ans = \"\";\n for(auto i: arr) ans += i;\n return ans;\n }\n};",
"memory": "17400"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> upper(26,0);\n vector<int> lower(26,0);\n find_upper(upper,s);\n find_lower(lower,s);\n string ans = find_ans(lower,upper);\n placed(ans,s);\n return s;\n }\n void find_upper(vector<int> &upper,string &s){\n for(int i=0;i<s.size();i++){\n if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n upper[s[i]-'A']++;\n s[i]='#';\n }\n }\n }\n void find_lower(vector<int> &lower,string &s){\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'){\n lower[s[i]-'a']++;\n s[i]='#';\n }\n }\n }\n string find_ans(vector<int> &lower,vector<int> &upper){\n string ans=\"\";\n for(int i=0;i<upper.size();i++){\n if(upper[i]) {\n \tans+=i+'A';\n \tupper[i]--;\n \ti--;\n }\n }\n for(int i=0;i<lower.size();i++){\n if(lower[i]) {\n \tans+=i+'a';\n \tlower[i]--;\n \ti--;\n }\n }\n return ans;\n }\n void placed(string ans,string &s){\n \tint j=0;\n \tfor(int i=0;i<s.size();i++){\n \t\tif(s[i]=='#'){\n \t\t\ts[i]=ans[j];\n \t\t\tj++;\n \t\t}\n \t}\n }\n};",
"memory": "17500"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> lower(26, 0), upper(26, 0);\n int n = s.length();\n for (int i = 0; i < n; i++) {\n if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||\n s[i] == 'u')\n lower[s[i] - 'a']++;\n\n if (s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' ||\n s[i] == 'U')\n upper[s[i] - 'A']++;\n }\n\n string Calpha = \"\";\n string Lalpha = \"\";\n for (int i = 0; i < 26; i++) {\n char c = 'A' + i;\n char l = 'a' + i;\n while (upper[i]) {\n Calpha += c;\n upper[i]--;\n }\n while (lower[i]) {\n Lalpha += l;\n lower[i]--;\n }\n }\n \n string alpha = Calpha + Lalpha;\n int k=0;\n for(int i=0;i<n;i++){\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' ||\n s[i] == 'u'||s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' ||\n s[i] == 'U'){\n s[i]=alpha[k++];\n }\n }\n return s;\n }\n};",
"memory": "17600"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n unordered_set<char> s1 = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};\n\n vector<char> v1;\n \n for (char c : s){\n if (s1.count(c)){\n v1.push_back(c);\n }\n }\n sort(v1.begin(), v1.end());\n int i = 0;\n for (char& c : s){\n if (s1.count(c)){\n c = v1[i];\n i++;\n }\n }\n return s;\n \n }\n};",
"memory": "17700"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n unordered_set<char> s1 = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};\n\n priority_queue<char, vector<char>, greater<char>> pq;\n \n for (char c : s){\n if (s1.count(c)){\n pq.push(c);\n }\n }\n for (char& c : s){\n if (s1.count(c)){\n c = pq.top();\n pq.pop();\n }\n }\n return s;\n \n }\n};",
"memory": "17800"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n unordered_set<char> s1 = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};\n\n vector<char> v1;\n \n for (char c : s){\n if (s1.count(c)){\n v1.push_back(c);\n }\n }\n sort(v1.begin(), v1.end());\n int i = 0;\n for (char& c : s){\n if (s1.count(c)){\n c = v1[i];\n i++;\n }\n }\n return s;\n \n }\n};",
"memory": "17800"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n unordered_set<char> st{'A','E','I','O','U','a','e','i','o','u'};\n vector<char> vowels;\n for(auto a: s){\n if(st.count(a)) vowels.push_back(a);\n }\n if(vowels.size() == 0) return s;\n sort(vowels.begin(), vowels.end());\n int j = 0, i = 0;\n while(j < vowels.size() && i < s.size()){\n if(st.count(s[i])){\n s[i] = vowels[j];\n j++;\n }\n i++;\n }\n return s;\n }\n};",
"memory": "17900"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n bool isVowel(char ch) {\n if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||\n ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {\n return true;\n } else {\n return false;\n }\n }\n\n string ans = \"\";\n void solve(string s) {\n string vowels = \"\";\n for (char ch : s) {\n if (isVowel(ch)) {\n vowels += ch;\n }\n }\n sort(vowels.begin(), vowels.end());\n int volIndex = 0;\n for (char ch : s) {\n if (isVowel(ch)) {\n ans += vowels[volIndex++];\n } else {\n ans += ch;\n }\n }\n }\n string sortVowels(string s) {\n solve(s);\n return ans;\n }\n};",
"memory": "18500"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n string vowels = \"AEIOUaeiou\";\n string res = \"\";\n int count = 0;\n unordered_map<char, int> mp;\n for(char &c:vowels)\n mp[c] = 0;\n for(char &c:s)\n mp[c]++;\n\n for(int i = 0; i < s.size(); i++){\n if(vowels.find(s[i]) == std::string::npos){\n res += s[i];\n }\n else{\n while(mp[vowels[count]] == 0){\n count++;\n }\n res += vowels[count];\n mp[vowels[count]]--;\n \n }\n }\n return res;\n }\n};",
"memory": "18600"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool isVowel(char i){\n return i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u' || i == 'A' || i == 'E' || i == 'I' || i == 'O' || i == 'U' ;\n }\n\n string sortVowels(string s) {\n vector<int> vowels; \n \n for(char &c: s){\n if(isVowel(c)){\n vowels.push_back(c);\n }\n } \n\n sort(vowels.begin(),vowels.end());\n\n int vowelIndex=0;\n for(char &c : s){\n if(isVowel(c)){\n c = vowels[vowelIndex++];\n }\n }\n\n return s;\n }\n};",
"memory": "18700"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n unordered_set<char> s1 = {'a', 'e', 'i', 'o', 'u'};\n\n priority_queue<char, vector<char>, greater<char>> pq;\n \n for (char c : s){\n if (s1.count(tolower(c))){\n pq.push(c);\n }\n }\n string result = \"\";\n for (char c : s){\n if (s1.count(tolower(c))){\n result += pq.top();\n pq.pop();\n }\n else{\n result += c;\n }\n }\n return result;\n \n }\n};",
"memory": "18800"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> vovel;\n for(int i = 0; i < s.size() ; i++){\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || \n s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U'){\n vovel.push_back(s[i]);\n }\n }\n sort(vovel.begin() , vovel.end());\n int index = 0;\n for(int i = 0; i < s.size() ; i++){\n if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || \n s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U'){\n s[i] = vovel[index++];\n }\n }\n return s;\n }\n};",
"memory": "18900"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n int n = s.size();\n string vowel=\"aeiouAEIOU\";\n unordered_map<char ,int>mpp;\n for(auto it :vowel){\n mpp[it]++;\n }\n string ti=\"\";\n for(int i =0 ;i<n;i++){\n if(mpp.find(s[i])!=mpp.end()){\n ti+=s[i];\n s[i]='.';\n \n }\n }\n \n sort(begin(ti),end(ti));\n \n string result=\"\";\n int j=0;\n for(int i=0;i<n;i++){\n if(s[i]=='.'){\n result+=ti[j++];\n }\n else{\n result+=s[i];\n }\n // cout<<result<<\" \";\n }\n\n return result;\n }\n};",
"memory": "19000"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n unordered_map<char,int> xmap;\n xmap['a'] = 1;\n xmap['e'] = 2;\n xmap['i'] = 3;\n xmap['o'] = 4;\n xmap['u'] = 5;\n xmap['A'] = 6;\n xmap['E'] = 7;\n xmap['I'] = 8;\n xmap['O'] = 9;\n xmap['U'] = 10;\n string ss=\"\";\n for(char c:s){\n if(xmap.count(c)){\n ss+=c;\n }\n }\n sort(ss.begin(),ss.end());\n string ans=\"\";\n for (char c:s){\n if(xmap.count(c)){\n ans+=ss[0];\n ss.erase(0,1);\n }else{\n ans+=c;\n }\n }\n return ans;\n }\n};",
"memory": "19100"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<char>v;\n map<char,bool>mpp;\n string vow=\"aeiouAEIOU\";\n for(int i=0;i<vow.size();i++){\n mpp[vow[i]]=true;\n }\n int n=s.size();\n for(int i=0;i<n;i++){\n if(mpp[s[i]])v.push_back(s[i]);\n }\n sort(v.begin(),v.end());\n for(int i=0,ind=0;i<n;i++){\n if(mpp[s[i]]){\n s[i]=v[ind];\n ind++;\n }\n }\n return s;\n }\n};",
"memory": "19400"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nunordered_set<char> Vowels = {'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u'};\nstring sortVowels(string s)\n{\n ios_base::sync_with_stdio(false);\n cin.tie(0); \n cout.tie(0);\n\n vector<char> inWord;\n for(int i = 0; i < s.size(); i++)\n {\n if(Vowels.find(s[i]) != Vowels.end()) inWord.push_back(s[i]);\n }\n\n sort(inWord.begin(), inWord.end(), greater());\n string t = \"\";\n for(int i = 0; i < s.size(); i++)\n {\n if(Vowels.find(s[i]) != Vowels.end())\n {\n t += inWord.back();\n inWord.pop_back();\n }\n else t +=s[i];\n }\n return t;\n}\n};",
"memory": "19500"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n string consonant,vowel;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U'){\n vowel+=s[i];\n }\n else{\n consonant+=s[i];\n }\n }\n // sorting vowels based on ASCII values\n vector<int> capitalVector(26,0);\n vector<int> smallVector(26,0);\n for(int i=0;i<vowel.size();i++){\n if(vowel[i]>='A' && vowel[i]<='Z') capitalVector[vowel[i]-'A']++;\n else smallVector[vowel[i]-'a']++;\n }\n string sortedVowel;\n for(int i=0;i<26;i++){\n while(capitalVector[i]){\n sortedVowel+=(char)(i+'A');\n capitalVector[i]--;\n }\n }\n for(int i=0;i<26;i++){\n while(smallVector[i]){\n sortedVowel+=(char)(i+'a');\n smallVector[i]--;\n }\n }\n int k=0,j=0,i=0;\n string ans;\n while(i<s.size()){\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U'){\n ans+=sortedVowel[k];\n k++;\n }\n else{\n ans+=consonant[j];\n j++;\n\n }\n i++;\n } \n return ans;\n }\n};",
"memory": "19700"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n string consonant,vowel;\n for(int i=0;i<s.size();i++){\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U'){\n vowel+=s[i];\n }\n else{\n consonant+=s[i];\n }\n }\n // sorting vowels based on ASCII values\n vector<int> capitalVector(26,0);\n vector<int> smallVector(26,0);\n for(int i=0;i<vowel.size();i++){\n if(vowel[i]>='A' && vowel[i]<='Z') capitalVector[vowel[i]-'A']++;\n else smallVector[vowel[i]-'a']++;\n }\n string sortedVowel;\n for(int i=0;i<26;i++){\n while(capitalVector[i]){\n sortedVowel+=(char)(i+'A');\n capitalVector[i]--;\n }\n }\n for(int i=0;i<26;i++){\n while(smallVector[i]){\n sortedVowel+=(char)(i+'a');\n smallVector[i]--;\n }\n }\n int k=0,j=0,i=0;\n string ans;\n while(i<s.size()){\n if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U'){\n ans+=sortedVowel[k];\n k++;\n }\n else{\n ans+=consonant[j];\n j++;\n\n }\n i++;\n } \n return ans;\n }\n};",
"memory": "19700"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "string removeCons(string s)\n{\n int n = s.size();\n string ans;\n for (char c : s)\n {\n if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')\n {\n ans.push_back(c);\n }\n }\n return ans;\n}\n\nstring getDone(string temp,string s)\n{\n int n = s.size();\n int j=0;\n string ans;\n int i=0;\n for (char c : s)\n {\n if(!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'))\n {\n ans.push_back(c);\n i++;\n }\n else\n {\n ans.push_back(temp[j]);\n j++;\n }\n }\n return ans;\n}\n\nclass Solution {\npublic:\n string sortVowels(string s) {\n string ans = s;\n ans = removeCons(ans);\n sort(ans.begin(),ans.end());\n ans = getDone(ans,s);\n return ans;\n \n }\n};",
"memory": "19800"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "// class Solution {\n// public:\n// string sortVowels(string s) {\n// vector<char> vowels;\n// vector<char> vowels_index;\n// for(int i=0;i<s.size();i++)\n// {\n// if (isVowel(s[i])) \n// {\n// vowels.push_back(s[i]);\n// vowels_index.push_back(i);\n// }\n// } \n// sort(vowels.begin(),vowels.end());\n// for(int i=0;i<vowels.size();i++)\n// {\n// s[vowels_index[i]] = vowels[i];\n// }\n// return s;\n// }\n// private:\n// bool isVowel(char c)\n// {\n// c=tolower(c);\n// return c=='a' || c=='e' || c=='i' || c=='o' || c=='u';\n// }\n\n\n// };\n#include <iostream>\n#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n string sortVowels(string s) {\n vector<char> vowels;\n vector<int> vowels_index;\n\n // Collect vowels and their indices\n for (int i = 0; i < s.size(); i++) {\n if (isVowel(s[i])) {\n vowels.push_back(s[i]);\n vowels_index.push_back(i);\n }\n }\n\n // Sort the vowels\n sort(vowels.begin(), vowels.end());\n\n // Place sorted vowels back in the string\n for (int i = 0; i < vowels.size(); i++) {\n s[vowels_index[i]] = vowels[i];\n }\n\n return s;\n }\n\nprivate:\n // Function to check if a character is a vowel\n bool isVowel(char c) {\n c = tolower(c);\n return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';\n }\n};\n",
"memory": "19900"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<char> vowels ={'a','e','i','o','u','A','E','I','O','U'};\n vector<char> hashOfVowels;\n vector<int> hashOfIndex;\n\n for(int i=0; i<s.size(); i++){\n for(char v: vowels){\n if(s[i] == v){\n hashOfVowels.push_back(s[i]);\n hashOfIndex.push_back(i);\n }\n }\n }\n sort(hashOfVowels.begin(), hashOfVowels.end());\n int index=0;\n for(auto it: hashOfIndex){\n s[it] = hashOfVowels[index];\n index++;\n }\n return s;\n }\n};",
"memory": "20000"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>v;\n int n=s.size();\n string ss=\"\";\n for(int i=0;i<n;i++){\n if(s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U' || s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){\n v.push_back(i);\n ss.push_back(s[i]);\n }\n }\n sort(ss.begin(),ss.end());\n int j=0;\n for(int i=0;i<v.size();i++){\n s[v[i]]=ss[j];\n j++;\n }\n return s;\n }\n};",
"memory": "20100"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>v;\n int n=s.size();\n string ss=\"\";\n for(int i=0;i<n;i++){\n if(s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U' || s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){\n v.push_back(i);\n ss.push_back(s[i]);\n }\n }\n sort(ss.begin(),ss.end());\n int j=0;\n for(int i=0;i<v.size();i++){\n s[v[i]]=ss[j];\n j++;\n }\n return s;\n }\n};",
"memory": "20100"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int> indices;\n vector<char> vogais;\n\n for (int i=0; i<s.size(); ++i){\n if (ehVogal(s[i])){\n indices.push_back(i);\n vogais.push_back(s[i]);\n }\n }\n\n sort(vogais.begin(), vogais.end());\n\n for (int i=0; i<indices.size(); ++i){\n int j = indices[i];\n s[j] = vogais[i];\n }\n\n return s;\n }\n\n bool ehVogal(char c){\n c = tolower(c);\n return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');\n }\n};",
"memory": "20200"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n //solely did it\n vector<char> vowels ={'a','e','i','o','u','A','E','I','O','U'};\n vector<char> hashOfVowels;\n vector<int> hashOfIndex;\n\n for(int i=0; i<s.size(); i++){\n for(char v: vowels){\n if(s[i] == v){\n hashOfVowels.push_back(s[i]);\n hashOfIndex.push_back(i);\n }\n }\n }\n sort(hashOfVowels.begin(), hashOfVowels.end());\n int index=0;\n for(auto it: hashOfIndex){\n s[it] = hashOfVowels[index];\n index++;\n }\n return s;\n }\n};",
"memory": "20300"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n priority_queue<int, vector<int>, greater<int>> vowels;\n string res = \"\";\n for(char c : s) {\n if(tolower(c) == 'a' || tolower(c) == 'e' || tolower(c) =='i' || tolower(c) =='o' || tolower(c) =='u' ) {\n vowels.push(int(c));\n }\n }\n for(char c : s) {\n if(tolower(c) == 'a' || tolower(c) == 'e' || tolower(c) =='i' || tolower(c) =='o' || tolower(c) =='u' ) {\n res += (vowels.top());\n vowels.pop();\n }\n else {\n res += c;\n }\n }\n return res;\n }\n};",
"memory": "20400"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>index;\n string temp=\"\";\n for(int i=0;i<s.size();i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n index.push_back(i);\n temp.push_back(s[i]);\n }\n \n } \n \n int arr[52]={0};\n for(int i=0;i<temp.size();i++){\n if(temp[i]>='A'&&temp[i]<='Z'){\n arr[temp[i]-'A']++;\n }\n else{\n arr[temp[i]-'a'+26]++;\n }\n \n }\n \n temp=\"\";\n for(int i=0;i<52;i++){\n if(arr[i]>0){\n if(i<26){\n while(arr[i]>0){\n temp.push_back(i+'A');\n arr[i]--;\n }\n }\n if(i>=26){\n while(arr[i]>0){\n temp.push_back('a'+(i-26));\n arr[i]--;\n }\n }\n }\n }\n \n for(int i=0;i<index.size();i++){\n s[index[i]]=temp[i];\n }\n return s; \n }\n};",
"memory": "20500"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "// Time: O(NlogN)\n// Space: O(N)\nclass Solution {\n bool isVowel(char c) {\n c = tolower(c);\n return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';\n }\npublic:\n string sortVowels(string s) {\n vector<int> id;\n for (int i = 0; i < s.size(); ++i) {\n if (isVowel(s[i])) id.push_back(i);\n }\n sort(begin(id), end(id), [&](int a, int b) { return s[a] < s[b]; });\n string ans;\n for (int i = 0, j = 0; i < s.size(); ++i) {\n if (isVowel(s[i])) ans += s[id[j++]];\n else ans += s[i];\n }\n return ans;\n }\n};",
"memory": "20600"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n vector<int>v;\n string x;\n string ans=s;\n int i,n=s.length();\n for(i=0;i<n;i++){\n if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'|s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U'){\n x.push_back(s[i]);\n v.push_back(i);\n }\n }\n sort(x.begin(),x.end());\n for(i=0;i<v.size();i++){\n ans[v[i]]=x[i];\n }\n return ans;\n }\n};",
"memory": "20700"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n \nstring t=s;\nvector<char>ans;\nvector<int>pos;\nint index=0;\nwhile(index!=s.size()){\n if(s[index]=='a' || s[index]=='e' || s[index]=='i' || s[index]=='o' || s[index]=='u' || s[index]=='A' || s[index]=='E' ||s[index]=='I' ||s[index]=='O' ||s[index]=='U'){\n pos.push_back(index);\n ans.push_back(s[index]);\n index++;\n }\n else{\n index++;\n }\n}\n\nsort(ans.begin(),ans.end());\nfor(int i=0;i<pos.size();i++){\n t[pos[i]]=ans[i];\n}\nreturn t;\n\n\n }\n};",
"memory": "20800"
} |
2,887 | <p>Given a <strong>0-indexed</strong> string <code>s</code>, <strong>permute</strong> <code>s</code> to get a new string <code>t</code> such that:</p>
<ul>
<li>All consonants remain in their original places. More formally, if there is an index <code>i</code> with <code>0 <= i < s.length</code> such that <code>s[i]</code> is a consonant, then <code>t[i] = s[i]</code>.</li>
<li>The vowels must be sorted in the <strong>nondecreasing</strong> order of their <strong>ASCII</strong> values. More formally, for pairs of indices <code>i</code>, <code>j</code> with <code>0 <= i < j < s.length</code> such that <code>s[i]</code> and <code>s[j]</code> are vowels, then <code>t[i]</code> must not have a higher ASCII value than <code>t[j]</code>.</li>
</ul>
<p>Return <em>the resulting string</em>.</p>
<p>The vowels are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>, and they can appear in lowercase or uppercase. Consonants comprise all letters that are not vowels.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "lEetcOde"
<strong>Output:</strong> "lEOtcede"
<strong>Explanation:</strong> 'E', 'O', and 'e' are the vowels in s; 'l', 't', 'c', and 'd' are all consonants. The vowels are sorted according to their ASCII values, and the consonants remain in the same places.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "lYmpH"
<strong>Output:</strong> "lYmpH"
<strong>Explanation:</strong> There are no vowels in s (all characters in s are consonants), so we return "lYmpH".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists only of letters of the English alphabet in <strong>uppercase and lowercase</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string sortVowels(string s) {\n \nstring t=s;\nvector<char>ans;\nvector<int>pos;\nint index=0;\nwhile(index!=s.size()){\n if(s[index]=='a' || s[index]=='e' || s[index]=='i' || s[index]=='o' || s[index]=='u' || s[index]=='A' || s[index]=='E' ||s[index]=='I' ||s[index]=='O' ||s[index]=='U'){\n pos.push_back(index);\n ans.push_back(s[index]); \n \n \n //in this i create a vector that \n // store the vowels and the position of vowel so that \n // i can insert directly to the position\n index++;\n }\n else{\n index++;\n }\n}\n\nsort(ans.begin(),ans.end());\nfor(int i=0;i<pos.size();i++){\n t[pos[i]]=ans[i];\n}\nreturn t;\n\n\n }\n};",
"memory": "20800"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 */\nvector<int> subtree_sum;\nint sum;\nconst int MAX = 1e5+1;\n\nclass Solution {\npublic:\n void dfs(TreeNode* root){\n if(!root) return;\n\n sum += root->val;\n // cout << root->val << \" \";\n\n dfs(root->left);\n dfs(root->right);\n }\n\n void helper(TreeNode* root){\n if(!root) return;\n sum = 0;\n // cout << \"\\ndfs for root node: \" << root->val << endl;\n // cout << \"nodes are: \" << endl;\n dfs(root);\n subtree_sum.push_back(sum);\n\n helper(root->left);\n helper(root->right);\n }\n\n vector<int> findFrequentTreeSum(TreeNode* root) {\n // store the subtree sum of all subtrees including the root\n subtree_sum.clear();\n helper(root);\n // cout << endl;\n\n // vector<int> freq;\n sort(subtree_sum.begin(), subtree_sum.end());\n\n // for(int i=0; i<subtree_sum.size(); i++) cout << subtree_sum[i] << \" \";\n // cout << endl;\n\n if(subtree_sum.size() == 1){\n return subtree_sum;\n }\n\n map<int, vector<int>> mp;\n\n int count = 1;\n for(int i=0; i<subtree_sum.size()-1; i++){\n if(subtree_sum[i] == subtree_sum[i+1]){\n // subtree_sum[i] = MAX;\n count++;\n }\n else{\n // freq.push_back(count);\n mp[count].push_back(subtree_sum[i]);\n count = 1;\n }\n if(i+1 == subtree_sum.size()-1){\n mp[count].push_back(subtree_sum[i+1]);\n // freq.push_back(count);\n }\n }\n\n // for(const auto& pair : mp){\n // cout << \"Key: \" << pair.first << \" -> Values: \";\n // for(const auto& value : pair.second){\n // cout << value << \" \";\n // }\n // cout << endl;\n // }\n \n // for(int i=0; i<freq.size(); i++) cout << freq[i] << \" \";\n // cout << endl;\n\n // for(int i=0; i<subtree_sum.size(); i++) cout << subtree_sum[i] << \" \";\n // cout << endl;\n\n // for(int i=0; i<subtree_sum.size(); i++){\n // if(subtree_sum[i] == MAX) subtree_sum.erase(subtree_sum.begin()+i);\n // }\n \n // for(int i=0; i<subtree_sum.size(); i++) cout << subtree_sum[i] << \" \";\n // cout << endl;\n\n // vector<int> ans;\n // int max_freq = *max_element(freq.begin(), freq.end());\n // for(int i=0; i<freq.size(); i++){\n // if(freq[i] == max_freq){\n // ans.push_back(subtree_sum[i]);\n // }\n // }\n\n int maxKey = mp.begin()->first;\n for(const auto& pair : mp){\n if(pair.first > maxKey){\n maxKey = pair.first; \n }\n }\n\n return mp[maxKey];\n }\n};",
"memory": "20700"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 */\nvector<int> subtree_sum;\nint sum;\nconst int MAX = 1e5+1;\n\nclass Solution {\npublic:\n void dfs(TreeNode* root){\n if(!root) return;\n\n sum += root->val;\n // cout << root->val << \" \";\n\n dfs(root->left);\n dfs(root->right);\n }\n\n void helper(TreeNode* root){\n if(!root) return;\n sum = 0;\n // cout << \"\\ndfs for root node: \" << root->val << endl;\n // cout << \"nodes are: \" << endl;\n dfs(root);\n subtree_sum.push_back(sum);\n\n helper(root->left);\n helper(root->right);\n }\n\n vector<int> findFrequentTreeSum(TreeNode* root) {\n // store the subtree sum of all subtrees including the root\n subtree_sum.clear();\n helper(root);\n // cout << endl;\n\n // vector<int> freq;\n sort(subtree_sum.begin(), subtree_sum.end());\n\n for(int i=0; i<subtree_sum.size(); i++) cout << subtree_sum[i] << \" \";\n cout << endl;\n\n if(subtree_sum.size() == 1){\n return subtree_sum;\n }\n\n map<int, vector<int>> mp;\n\n int count = 1;\n for(int i=0; i<subtree_sum.size()-1; i++){\n if(subtree_sum[i] == subtree_sum[i+1]){\n // subtree_sum[i] = MAX;\n count++;\n }\n else{\n // freq.push_back(count);\n mp[count].push_back(subtree_sum[i]);\n count = 1;\n }\n if(i+1 == subtree_sum.size()-1){\n mp[count].push_back(subtree_sum[i+1]);\n // freq.push_back(count);\n }\n }\n\n for(const auto& pair : mp){\n cout << \"Key: \" << pair.first << \" -> Values: \";\n for(const auto& value : pair.second){\n cout << value << \" \";\n }\n cout << endl;\n }\n \n // for(int i=0; i<freq.size(); i++) cout << freq[i] << \" \";\n // cout << endl;\n\n // for(int i=0; i<subtree_sum.size(); i++) cout << subtree_sum[i] << \" \";\n // cout << endl;\n\n // for(int i=0; i<subtree_sum.size(); i++){\n // if(subtree_sum[i] == MAX) subtree_sum.erase(subtree_sum.begin()+i);\n // }\n \n // for(int i=0; i<subtree_sum.size(); i++) cout << subtree_sum[i] << \" \";\n // cout << endl;\n\n // vector<int> ans;\n // int max_freq = *max_element(freq.begin(), freq.end());\n // for(int i=0; i<freq.size(); i++){\n // if(freq[i] == max_freq){\n // ans.push_back(subtree_sum[i]);\n // }\n // }\n\n int maxKey = mp.begin()->first;\n for(const auto& pair : mp){\n if(pair.first > maxKey){\n maxKey = pair.first; \n }\n }\n\n return mp[maxKey];\n }\n};",
"memory": "20800"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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:\nvector<int>v;\n\nint f(TreeNode* root ){\nif(!root){return 0;}\nint k=root->val+f(root->left)+f(root->right);\n\nv.push_back(k);\nroot=NULL;\nreturn k;\n}\n vector<int> findFrequentTreeSum(TreeNode* root) {\n \nint k=f(root);\nint m=0;\nsort(v.begin(),v.end());\nvector<int>ans;\nint i=0;\nwhile(i<v.size()){\nint j=i+1;int c=0;\nwhile(j<v.size()&&v[j]==v[i]){\nj++;\n}\nif(j-i==m){\n ans.push_back(v[i]);\n}\nif(j-i>m){\n m=j-i;\n ans.clear();\n ans.push_back(v[i]);\n}\n\n\n\n i=j;\n}\n\nreturn ans;\n\n }\n};",
"memory": "21500"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 {\nprivate:\n int calculate(TreeNode* root, vector<int>& treeSum) {\n if(root == NULL) return 0;\n int left = calculate(root->left, treeSum);\n int right = calculate(root->right, treeSum);\n int result = left + right + root->val;\n treeSum.push_back(result);\n return result;\n }\npublic:\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<int> treeSum;\n calculate(root, treeSum);\n sort(treeSum.begin(), treeSum.end());\n int ct = 1;\n int maxCt = ct;\n int size = treeSum.size();\n for(int i=1;i<size;i++) {\n if(treeSum[i] == treeSum[i-1]) {\n ct++;\n } else {\n maxCt = max(maxCt, ct);\n ct = 1;\n }\n }\n maxCt = max(maxCt, ct);\n ct = 1;\n vector<int> result;\n if(ct == maxCt) result.push_back(treeSum[0]);\n for(int i=1;i<size;i++) {\n if(treeSum[i] == treeSum[i-1]) {\n ct++;\n } else ct = 1;\n if(ct == maxCt) {\n if(result.size() > 0 && result[result.size()-1] == treeSum[i]) continue;\n result.push_back(treeSum[i]);\n }\n }\n return result;\n }\n};",
"memory": "21600"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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> findFrequentTreeSum(TreeNode* root) {\n vector<int> counts;\n vector<int> sums;\n vector<int> res;\n rec(counts, sums, root);\n auto max_el = *max_element(counts.begin(), counts.end());\n cout << max_el<<endl;\n for (int i=0; i<counts.size(); i++) {\n cout << counts[i] << \" \" << sums[i]<<endl;\n if (counts[i] == max_el) {\n res.push_back(sums[i]);\n }\n }\n return res;\n }\n int rec(vector<int>& counts, vector<int>& sums, TreeNode* node) {\n if (node==nullptr) {\n return 0;\n }\n int sub_sum = rec(counts, sums, node->left)+rec(counts, sums, node->right)+node->val;\n auto find_element = find(sums.begin(), sums.end(), sub_sum);\n if (find_element!=sums.end()) {\n int index=find_element-sums.begin();\n counts[index]++;\n }\n else {\n sums.push_back(sub_sum);\n counts.push_back(1);\n }\n return sub_sum;\n }\n};",
"memory": "21700"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 findAns(TreeNode* root) {\n if (root == NULL) {\n return 0;\n }\n if (root->left == NULL && root->right == NULL) {\n return root->val;\n }\n int left = findAns(root->left);\n int right = findAns(root->right);\n root->val += left + right;\n return root->val;\n }\n void inOrder(TreeNode* root,unordered_map<int,int>&um) {\n if (root == NULL) {\n return;\n }\n inOrder(root->left,um);\n um[root->val]++;\n inOrder(root->right,um);\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n findAns(root);\n vector<int> ans;\n unordered_map<int,int> um;\n inOrder(root,um);\n int maxFreq=0;\n for(auto it:um){\n maxFreq=max(maxFreq,it.second);\n }\n for(auto it:um){\n if(it.second==maxFreq){\n ans.push_back(it.first);\n }\n }\n return ans;\n }\n};",
"memory": "21800"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 {\nprivate:\n int collect(TreeNode* root, std::vector<int>* sums) {\n if (root == nullptr) {\n return 0;\n }\n int sum = collect(root->left, sums) + collect(root->right, sums) + root->val;\n sums->push_back(sum);\n return sum;\n }\npublic:\n vector<int> findFrequentTreeSum(TreeNode* root) {\n if (root == nullptr) {\n return std::vector<int>();\n }\n std::vector<int> sums;\n collect(root, &sums);\n std::sort(sums.begin(), sums.end());\n int current = sums[0];\n int count = 1;\n std::vector<std::pair<int, int>> counts;\n for (int i=1; i<sums.size(); ++i) {\n if (sums[i] != current) {\n counts.push_back({count, current});\n count = 1;\n current = sums[i];\n } else {\n ++ count;\n }\n }\n counts.push_back({count, current});\n std::sort(counts.begin(), counts.end(), std::greater<std::pair<int, int>>());\n std::vector<int> result;\n result.push_back(counts[0].second);\n count = counts[0].first;\n for (int i=1; i<counts.size() && counts[i].first == count; ++i) {\n result.push_back(counts[i].second);\n }\n return result;\n }\n};",
"memory": "21900"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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> findFrequentTreeSum(TreeNode* root) {\n vector<int> v;\n int temp = s(root,v);\n std::unordered_map<int,int> fm;\n for(auto x:v){\n fm[x]++;\n }\n int f=0;\n for(auto x:fm){\n if(f<x.second){\n f=x.second;\n }\n }\n vector<int> out;\n for(auto x:fm){\n if(x.second==f){\n out.push_back(x.first);\n }\n }\n return out;\n }\n\n long s(TreeNode* root,vector<int> & v){\n if(root->right==NULL && root->left==NULL){\n v.push_back(root->val);\n return root->val;\n }\n if(root->right && root->left){\n long temp = s(root->left,v)+s(root->right,v)+root->val;\n v.push_back(temp);\n return temp;\n }\n if(root->left==NULL){\n long temp = s(root->right,v)+root->val;\n v.push_back(temp);\n return temp;\n }else{\n long temp = s(root->left,v)+root->val;\n v.push_back(temp);\n return temp;\n }\n }\n};",
"memory": "22300"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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:\nvector<int>ans;\n int solve(TreeNode*root)\n {\n if(!root)\n return 0;\n int l=solve(root->left);\n int r=solve(root->right);\n root->val+=l+r;\n ans.push_back(root->val);\n return root->val;\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n sort(ans.rbegin(),ans.rend());\n solve(root);\n vector<int>k;\n map<int,int>m;\n for(auto l : ans)\n m[l]++;\n int mx=0;\n for(auto l : m)\n mx=max(mx,l.second);\n ans.clear();\n for(auto l : m)\n {\n if(l.second==mx)\n ans.push_back(l.first);\n }\n return ans;\n \n }\n};",
"memory": "22300"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 vector<int> subtreeSums;\n int dfs(TreeNode* root) {\n if(!root) return 0;\n int leftSum = dfs(root->left);\n int rightSum = dfs(root->right);\n int totalSum = leftSum + rightSum + root->val;\n subtreeSums.push_back(totalSum);\n return totalSum;\n }\npublic:\n vector<int> findFrequentTreeSum(TreeNode* root) {\n dfs(root);\n\n unordered_map<int, int> freq;\n for(int sum : subtreeSums) freq[sum]++;\n\n int maxFreq = 0;\n for(auto& it : freq) {\n maxFreq = max(maxFreq, it.second);\n }\n\n vector<int> ans;\n for(auto& it : freq) {\n if(it.second == maxFreq) {\n ans.push_back(it.first);\n }\n }\n\n return ans;\n }\n};",
"memory": "22400"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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> findMaxFrequency(const vector<int>& nums) {\n unordered_map<int, int> freqMap;\n int maxFreq = 0;\n vector<int> mostFrequentNums;\n\n for (int num : nums) {\n freqMap[num]++;\n if (freqMap[num] > maxFreq) {\n maxFreq = freqMap[num];\n }\n }\n\n for (const auto& entry : freqMap) {\n if (entry.second == maxFreq) {\n mostFrequentNums.push_back(entry.first);\n }\n }\n\n return mostFrequentNums;\n}\n int sum(TreeNode *root){\n if(root==nullptr)\n return 0;\n return root->val+sum(root->left)+sum(root->right);\n }\n void help(TreeNode *root,vector<int>&a){\n if(root==nullptr)\n return;\n a.push_back(sum(root));\n help(root->left,a);\n help(root->right,a);\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<int>ans;\n help(root,ans);\n return findMaxFrequency(ans);\n }\n};",
"memory": "22500"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 map <int, int> freqmap;\n int maxfreq = 0;\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector <int> subsum;\n sumofnodes(root, subsum);\n\n for(int sub: subsum){\n freqmap[sub]++;\n }\n\n for (auto ptr: freqmap){\n maxfreq = maxfreq < ptr.second? ptr.second:maxfreq;\n }\n\n vector<int> res;\n for (auto ptr: freqmap){\n if (ptr.second == maxfreq){\n res.push_back(ptr.first);\n }\n }\n return res;\n }\n\n int sumofnodes(TreeNode* curr, vector<int>& subsum){\n if (curr == nullptr)\n return 0;\n\n int sum = sumofnodes(curr->left, subsum) + sumofnodes(curr->right, subsum) + curr->val;\n subsum.push_back(sum);\n return sum;\n }\n};",
"memory": "22600"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 solve(TreeNode* root, vector<int>&v){\n if(!root){\n return 0;\n }\n \n int l = root->val+solve(root->left, v);\n int r = root->val+solve(root->right, v);\n if(root){\n v.push_back(l+r);\n }\n return l+r;\n\n \n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<int>freq;\n if(root->left==NULL && root->right==NULL){\n freq.push_back(root->val);\n return freq;\n }\n int res= solve(root, freq);\n \n vector<int>ans;\n map<int,int>mp;\n\n for(int i=0;i<freq.size();i++){\n mp[freq[i]/2]++;\n }\n int m=0;\n for(auto &x: mp){\n m=max(m, x.second);\n }\n\n for(auto &x: mp){\n if(m==x.second){\n ans.push_back(x.first);\n }\n }\n \n return ans;\n }\n};",
"memory": "22700"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 sum(TreeNode* root){\n //ajamos yvelaperi\n if(!root) return 0;\n int ls=sum(root->left);\n int rs=sum(root->right);\n return root->val +ls +rs;\n }\n void helper(TreeNode* root, vector<int>& sums){\n if(!root) return;\n sums.push_back(sum(root));\n helper(root->left,sums);\n helper(root->right,sums);\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n if(!root) return {};\n vector<int> sums;\n helper(root,sums);\n //sums has all the sums of the tree\n //make a map and find the most frequent member\n map<int,int> mp;\n for(int i=0;i<sums.size();i++){\n mp[sums[i]]++;\n }\n vector<int> ans;\n //add most frequent elements\n int mf=0;\n for(auto it:mp){\n mf=max(mf,it.second);\n }\n //mf is the frequency which is the highest\n \n for(auto i:mp){\n if(i.second==mf){\n ans.push_back(i.first);\n }\n }\n\n\n return ans;\n }\n};",
"memory": "22700"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 subTreeSum(TreeNode *root) {\n if (!root) return 0;\n if (!root->left && !root->right) {\n return root->val;\n }\n return root->val + subTreeSum(root->left) + subTreeSum(root->right);\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<int> sums;\n queue<TreeNode*> tree;\n tree.push(root);\n while (!tree.empty()) {\n int treeSize = tree.size();\n for (int i = 0; i < treeSize; i++) {\n TreeNode *cur = tree.front();\n tree.pop();\n int sum = subTreeSum(cur);\n sums.push_back(sum);\n if (cur->left) tree.push(cur->left);\n if (cur->right) tree.push(cur->right);\n }\n }\n unordered_map<int,int> freqMap;\n for (int i = 0; i < sums.size(); i++) {\n freqMap[sums[i]]++;\n }\n vector<pair<int,int>> freqMapVec;\n for (auto it: freqMap) freqMapVec.push_back(it);\n sort(freqMapVec.begin(), freqMapVec.end(), [](pair<int,int> a, pair<int,int> b){\n return b.second < a.second;\n });\n vector<int> ans;\n int maxFreq = freqMapVec[0].second;\n for (auto it : freqMapVec) {\n if (it.second == maxFreq) {\n ans.push_back(it.first);\n }\n }\n /*for (auto it : freqMapVec) {\n cout << it.first << \", \" << it.second << endl;\n }*/\n \n return ans;\n }\n};",
"memory": "22800"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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:\nint findsum(TreeNode* root,vector<pair<TreeNode*,int>> &v){\n if(root==NULL){\n return 0;\n }\n int leftsum=findsum(root->left,v);\n int rightsum=findsum(root->right,v);\n int rootsum=root->val+leftsum+rightsum;\n pair<TreeNode*,int> p={root,rootsum};\n v.push_back(p);\n return rootsum;\n}\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<pair<TreeNode*,int>> v;\n findsum(root,v);\n unordered_map<int,int> map;\n for(auto p:v){\n map[p.second]++;\n }\n int maxfreq=0;\n for(auto it:map){\n maxfreq=max(maxfreq,it.second);\n }\n // ab mil gayi maxm frequency\n vector<int> ans;\n for(auto it:map){\n if(it.second==maxfreq){\n ans.push_back(it.first);\n }\n }\n return ans;\n }\n};",
"memory": "22900"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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:\nint findsum(TreeNode* root,vector<pair<TreeNode*,int>> &v){\n if(root==NULL){\n return 0;\n }\n int leftsum=findsum(root->left,v);\n int rightsum=findsum(root->right,v);\n int rootsum=root->val+leftsum+rightsum;\n pair<TreeNode*,int> p={root,rootsum};\n v.push_back(p);\n return rootsum;\n}\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<pair<TreeNode*,int>> v;\n findsum(root,v);\n unordered_map<int,int> map;\n for(auto p:v){\n map[p.second]++;\n }\n int maxfreq=0;\n for(auto it:map){\n maxfreq=max(maxfreq,it.second);\n }\n // ab mil gayi maxm frequency\n vector<int> ans;\n for(auto it:map){\n if(it.second==maxfreq){\n ans.push_back(it.first);\n }\n }\n return ans;\n }\n};",
"memory": "22900"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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\nint solve(TreeNode* root, vector<int> &ans) {\n if (root == NULL) {\n return 0;\n }\n int leftSum = solve(root->left, ans);\n int rightSum = solve(root->right, ans);\n int sum = leftSum + rightSum + root->val;\n ans.push_back(sum); // Push the sum of this subtree into the vector\n return sum;\n}\n\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<int>ans;\n solve(root,ans);\n map<int,int>mp;\n\n for(auto i:ans){\n mp[i]++;\n }\n vector<pair<int,int>>pairr;\n\n for(auto i:mp){\n pairr.push_back({i.second,i.first});\n }\n int maxi = INT_MIN;\n for(auto i:pairr){\n maxi = max(maxi,i.first);\n }\n vector<int>finalAns;\n for(auto i:pairr){\n if(i.first==maxi){\n finalAns.push_back(i.second);\n }\n }\n return finalAns;\n }\n};",
"memory": "23000"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 f(TreeNode* root, vector<int>& v){\n if(root == NULL){\n return 0;\n }\n int l = f(root->left, v);\n int r = f(root->right, v);\n v.push_back(l+r+root->val);\n return (l+r+root->val);\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<int> v;\n f(root, v);\n // return v;\n sort(v.begin(), v.end());\n vector<int> ans;\n map<int, int> mp;\n for(auto it:v){\n mp[it]++;\n cout << it <<\" \";\n }\n vector<pair<int, int>> temp;\n for(auto it:mp){\n temp.push_back({it.second, it.first});\n }\n sort(temp.begin(), temp.end());\n int last = temp[temp.size()-1].second;\n int lastfreq = temp[temp.size()-1].first;\n ans.push_back(last);\n for(int i=temp.size()-2; i>=0; i--){\n if(temp[i].first < lastfreq){\n break;\n }\n ans.push_back(temp[i].second);\n }\n return ans;\n // vector<int> ans;\n\n // int last = v[v.size()-1];\n // ans.push_back(last);\n // int lastfreq = mp[last];\n // for(int i=v.size()-1; i>=0; i--){\n // if(v[i] == last){\n // continue;\n // }\n // if(mp[v[i]] < lastfreq){\n // break;\n // }\n // lastfreq = mp[v[i]];\n // last = v[i];\n // ans.push_back(last);\n // }\n return ans;\n\n }\n};",
"memory": "23100"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 findFrequentTreeSumHelper(TreeNode* root, unordered_map<int, int> &sumCount){\n if(root == NULL) return;\n findFrequentTreeSumHelper(root -> left, sumCount);\n findFrequentTreeSumHelper(root -> right, sumCount);\n if(root -> left != NULL) root -> val += root -> left -> val;\n if(root -> right != NULL) root -> val += root -> right -> val;\n sumCount[root -> val]++;\n\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n unordered_map<int, int> sumCount;\n\n findFrequentTreeSumHelper(root, sumCount);\n\n int maxFrequency = -1;\n for(auto i: sumCount)\n if(i.second > maxFrequency) maxFrequency = i.second;\n\n vector<int> ans;\n for(auto i: sumCount)\n if(i.second == maxFrequency) ans.push_back(i.first);\n\n return ans;\n }\n};",
"memory": "23700"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 po(TreeNode* node, unordered_map<int,int>& mp){\n if(!node) return;\n po(node->left,mp);\n po(node->right,mp);\n if(node->left && node->right) node->val=node->left->val+node->right->val+node->val;\n else if (node->left && !node->right) node->val=node->val+node->left->val;\n else if (node->right && !node->left) node->val=node->val+node->right->val;\n mp[node->val]++;\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n unordered_map<int,int> mp;\n po(root,mp);\n vector<int> ans;\n int max=INT_MIN;\n for(auto it:mp){\n if(it.second>=max){\n max=it.second;\n }\n }\n for(auto it:mp){\n if(it.second==max){\n ans.push_back(it.first);\n }\n }\n return ans;\n\n }\n};",
"memory": "23800"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 // sum to value map\n // iterate through the map and return the list with maxSize\n int dfs(TreeNode* root, unordered_map<int, int>& freq){\n if(!root) return 0;\n int leftSubtreeSum = dfs(root->left, freq);\n int rightSubtreeSum = dfs(root->right, freq);\n int currSum = leftSubtreeSum + rightSubtreeSum + root->val;\n freq[currSum]++;\n return currSum;\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n unordered_map<int, int> freq;\n dfs(root, freq);\n int maxFreq = 0;\n for(auto it: freq){\n maxFreq = max(maxFreq, it.second);\n }\n vector<int> res;\n for(auto it: freq){\n if(it.second == maxFreq){\n res.push_back(it.first);\n }\n }\n return res;\n }\n};",
"memory": "23900"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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> ans;\n map<int,int> mp;\n int maxLen = 0;\n int dfs(TreeNode* root){\n if(!root)\n return 0;\n int l = dfs(root->left);\n int r = dfs(root->right);\n root->val+= l+r;\n mp[root->val]++;\n if(maxLen < mp[root->val])\n maxLen = mp[root->val];\n return root->val;\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n if(!root)\n return {};\n dfs(root);\n for(auto i:mp){\n if(i.second == maxLen){\n ans.push_back(i.first);\n }\n\n }\n \n return ans;\n }\n};",
"memory": "24000"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 // sum to value map\n // iterate through the map and return the list with maxSize\n int dfs(TreeNode* root, unordered_map<int, int>& freqMap, int& maxFreq){\n if(!root) return 0;\n int leftSubtreeSum = dfs(root->left, freqMap, maxFreq);\n int rightSubtreeSum = dfs(root->right, freqMap, maxFreq);\n int currSum = leftSubtreeSum + rightSubtreeSum + root->val;\n maxFreq = max(maxFreq, ++freqMap[currSum]);\n return currSum;\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n unordered_map<int, int> freqMap;\n int maxFreq = 0;\n dfs(root, freqMap, maxFreq);\n vector<int> maxFreqSums;\n for(auto &it: freqMap){\n if(it.second == maxFreq){\n maxFreqSums.push_back(it.first);\n }\n }\n return maxFreqSums;\n }\n};",
"memory": "24100"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<int> findFrequentTreeSum(TreeNode* root) {\n vector<int> ans;\n unordered_map<int, int> count;\n int maxCount = 0;\n\n sumDownFrom(root, count);\n\n for (const auto& [_, freq] : count)\n maxCount = max(maxCount, freq);\n\n for (const auto& [sum, freq] : count)\n if (freq == maxCount)\n ans.push_back(sum);\n\n return ans;\n }\n\n private:\n int sumDownFrom(TreeNode* root, unordered_map<int, int>& count) {\n if (root == nullptr)\n return 0;\n\n const int sum = root->val + sumDownFrom(root->left, count) +\n sumDownFrom(root->right, count);\n ++count[sum];\n return sum;\n }\n};",
"memory": "24200"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\nint find(TreeNode* root , unordered_map< int , int >&cnt){\n if(root == NULL){\n return 0;\n }\n int left = find(root->left , cnt);\n int right = find(root->right , cnt);\n int sum = root->val + left + right;\n cnt[sum]++;\n return sum; \n}\npublic:\n vector<int> findFrequentTreeSum(TreeNode* root) {\n unordered_map< int , int >cnt;\n find(root , cnt);\n int value = INT_MIN;\n for( auto i : cnt){\n if(i.second > value){\n value = i.second;\n }\n }\n vector<int>ans;\n for(auto i : cnt){\n if(i.second == value){\n ans.push_back(i.first);\n }\n }\n return ans;\n }\n};",
"memory": "24200"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"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 vector<int> findFrequentTreeSum(TreeNode* root) \n {\n map<int,int>maps;\n\n vector<int>ans;\n int maxi=0;\n solve(root,maps,maxi);\n\n for(auto i:maps)\n {\n if(i.second==maxi)ans.push_back(i.first);\n }\n return ans;\n }\n void solve(TreeNode*root,map<int,int>&maps,int &maxi)\n {\n if(!root)\n {\n return;\n }\n\n maps[sum(root)]++;\n maxi=max(maxi,maps[sum(root)]);\n\n solve(root->left,maps,maxi);\n solve(root->right,maps,maxi);\n\n }\n int sum(TreeNode*root)\n {\n if(!root)return 0;\n return root->val+sum(root->left)+sum(root->right);\n }\n};",
"memory": "24300"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"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,int> mp;\n int max1=INT_MIN;\n int fun(TreeNode* root){\n if(root==NULL){\n return 0;\n }\n int l=fun(root->left);\n int r=fun(root->right);\n mp[root->val+l+r]++;\n max1=max(max1,mp[root->val+l+r]);\n return root->val+l+r;\n }\n vector<int> findFrequentTreeSum(TreeNode* root) {\n fun(root);\n vector<int> v1;\n for(auto &i:mp){\n if(i.second==max1){\n v1.push_back(i.first);\n }\n \n\n \n }\n sort(v1.begin(),v1.end());\n return v1;\n }\n};",
"memory": "24300"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 vector<int> findFrequentTreeSum(TreeNode* root) \n {\n map<int,int>maps;\n\n vector<int>ans;\n int maxi=0;\n solve(root,maps,maxi);\n\n for(auto i:maps)\n {\n if(i.second==maxi)ans.push_back(i.first);\n }\n return ans;\n }\n int solve(TreeNode*root,map<int,int>&maps,int &maxi)\n {\n if(!root)\n {\n return 0;\n }\n \n int sum=root->val+solve(root->left,maps,maxi)+solve(root->right,maps,maxi);\n maxi=max(maxi,++maps[sum]);\n\n return sum;\n\n }\n};",
"memory": "24400"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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<long int,int> m;\n\n long int findFrequentTreeSumHelper(TreeNode* root){\n //BASE CASE\n if(root == NULL){\n return 0;\n }\n\n long int leftsum = findFrequentTreeSumHelper(root->left);\n long int rightsum = findFrequentTreeSumHelper(root->right);\n\n long int currsum = (leftsum+rightsum+root->val);\n m[currsum]++;\n return currsum;\n }\n\n vector<int> findFrequentTreeSum(TreeNode* root) {\n long int treesum = findFrequentTreeSumHelper(root);\n vector<int> ans;\n\n int max_freq = 0;\n for(auto pair:m){\n int freq = pair.second;\n max_freq = max(max_freq,freq);\n }\n\n for(auto pair:m){\n long int currsum = pair.first;\n int freq = pair.second;\n\n if(freq == max_freq){\n ans.push_back(currsum);\n }\n }\n\n return ans;\n }\n};",
"memory": "24500"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 {\nunordered_map<int,int>mp;\n\npublic:\n\n int findSum(TreeNode* root) {\n if(!root) return 0;\n\n int left = findSum(root->left);\n int right = findSum(root->right);\n\n int sum = left+right+root->val;\n mp[sum]++;\n\n return sum;\n }\n\n\n vector<int> findFrequentTreeSum(TreeNode* root) {\n \n vector<int>ans;\n if(!root) return ans;\n priority_queue<pair<int,int>>pq;\n findSum(root);\n for(auto it=mp.begin(); it!=mp.end();it++)\n pq.push({it->second,it->first});\n\n int maxx=pq.top().first;\n\n while(!pq.empty() && pq.top().first==maxx) {\n ans.push_back(pq.top().second);\n pq.pop();\n }\n\n return ans;\n\n \n }\n};",
"memory": "24600"
} |
508 | <p>Given the <code>root</code> of a binary tree, return the most frequent <strong>subtree sum</strong>. If there is a tie, return all the values with the highest frequency in any order.</p>
<p>The <strong>subtree sum</strong> of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</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>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></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 vector<int> findFrequentTreeSum(TreeNode* root) {\n\n vector<int> ans;\n map<int,int> m;\n ok(root,m);\n map<int,vector<int>> m1;\n for(auto i:m)\n {\n m1[i.second].push_back(i.first);\n }\n return m1.rbegin()->second; \n\n }\n int ok(TreeNode* root,map<int,int> &m)\n {\n if(root==NULL)\n return 0;\n int a=ok(root->left,m);\n int b=ok(root->right,m);\n m[a+b+root->val]++;\n return a+b+root->val;\n }\n};",
"memory": "24700"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\nint solveUsingRecursion(int n){\n // base case\n if(n == 0){\n return 0;\n }\n if(n == 1){\n return 1;\n }\n int ans = solveUsingRecursion(n-1) + solveUsingRecursion(n-2);\n return ans;\n }\n int fib(int n) {\n int ans = solveUsingRecursion(n);\n return ans;\n }\n};\n\n// class Solution {\n// public:\n\n// int solveUsingMem(int n,vector<int>& dp){\n// // base case\n// if(n == 0){\n// return 0;\n// }\n// if(n == 1){\n// return 1;\n// }\n \n// // step 3:\n// // agr base case k baddd check if ans is alredy exist or not\n// if(dp[n] != -1){\n// return dp[n];\n// }\n\n// //step 2:\n\n// dp[n]= solveUsingMem(n-1,dp) + solveUsingMem(n-2,dp);\n// return dp[n];\n// }\n\n// int fib(int n){\n// // step1:\n// vector<int>dp(n+1,-1);\n// return solveUsingMem(n,dp);\n// }\n// };",
"memory": "7000"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n == 0) return 0;\n if( n == 1) return 1;\n return fib(n-1) + fib(n-2);\n }\n};",
"memory": "7100"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n==0){return 0;}\n if(n==1){return 1;}\n return fib(n-1)+fib(n-2);\n // if(n<=1){\n // return n;\n // }\n // else{\n // int fib[n+1];\n // fib[0]=0;\n // fib[1]=1;\n // for(int i=2;i<=n;i++){\n // fib[i]=fib[i-1]+fib[i-2];\n // }\n // return fib[n];\n // }\n }\n};",
"memory": "7100"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n<=1) return n;\n int ans=fib(n-1)+fib(n-2);\n return ans;\n \n }\n};",
"memory": "7200"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n<=1){\n return n;\n }\n return fib(n-1)+fib(n-2);\n }\n};",
"memory": "7200"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n==0||n==1){\n return n;\n }\n return fib(n-1)+fib(n-2);\n }\n};",
"memory": "7300"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n==0 || n==1)\n return n;\n return fib(n-1)+fib(n-2);\n }\n};",
"memory": "7300"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n==0){\n return 0;\n }\n else if(n==1){\n return 1;\n }\n else{\n return fib(n-1)+fib(n-2);\n }\n }\n};",
"memory": "7400"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n==1)\n return 1;\n if(n==0)\n return 0; \n return fib(n-1) + fib(n-2);\n }\n};",
"memory": "7400"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int fib(int n) {\n if(n==0 || n==1){\n return n;\n }\n int ans = fib(n-1) + fib(n-2);\n return ans;\n }\n};",
"memory": "7500"
} |
1,013 | <p>The <b>Fibonacci numbers</b>, commonly denoted <code>F(n)</code> form a sequence, called the <b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>
<pre>
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.
</pre>
<p>Given <code>n</code>, calculate <code>F(n)</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 1
<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 3
<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 30</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nint solveUsingRec(int n){\n if(n==0 || n==1){\n return n;\n }\n return solveUsingRec(n-1) + solveUsingRec(n-2);\n}\n\n int solveUsingDp(int n,vector<int>&dp){\n if(n==0 || n==1){\n return n;\n }\n dp[n] = solveUsingRec(n-1) + solveUsingRec(n-2);\n return dp[n];\n }\n\n int fib(int n) {\n vector<int>dp(n+1,-1);\n int ans = solveUsingDp(n,dp);\n return ans;\n }\n};",
"memory": "7500"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 {\nprivate:\n int max_level = -1;\n void solve(TreeNode* &root, int level, int &ans){\n if(root == NULL){ return ;}\n\n if(level > max_level){\n max_level = level;\n ans = root->val;\n }\n\n solve(root->left, level+1, ans);\n solve(root->right, level+1, ans);\n }\n\npublic:\n int findBottomLeftValue(TreeNode* root) {\n int ans = INT_MIN;\n solve(root, 0, ans);\n return ans;\n }\n};",
"memory": "23000"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"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 findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*>q;\n q.push(root);\n int nodeData = 0;\n\n while(!q.empty())\n {\n int size = q.size();\n for(int i=0; i<size; i++)\n {\n TreeNode* node = q.front();\n q.pop();\n\n if(i==0) nodeData = node->val;\n\n if(node->left) q.push(node->left);\n if(node->right) q.push(node->right);\n }\n }\n return nodeData;\n }\n};",
"memory": "23500"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"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 ans=0;\n void solve(TreeNode*root,int h,bool found)\n {\n if(root==NULL)return ;\n \n solve(root->right,h-1,found);\n solve(root->left,h-1,found);\n \n\n if(h==1&&!found)\n {\n found=true;\n cout<<root->val<<endl;\n ans=root->val;\n return;\n }\n }\n int findBottomLeftValue(TreeNode* root) \n {\n int height=h(root);\n solve(root,height,false);\n return ans; \n }\n int h(TreeNode*root)\n {\n if(root==NULL)return 0;\n return 1+max(h(root->left),h(root->right));\n }\n};",
"memory": "23500"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"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 findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*> q;\n q.push(root);\n int leftNodeVal = root->val;\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 (i==0) \n leftNodeVal=node->val;\n if (node->left)\n q.push(node->left);\n if (node->right)\n q.push(node->right);\n }\n } \n return leftNodeVal;\n }\n};",
"memory": "23600"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 1 | {
"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 ans=0;\n bool found=false;\n void solve(TreeNode*root,int h)\n {\n if(root==NULL)return ;\n \n solve(root->left,h-1);\n solve(root->right,h-1);\n\n if(h==1&&!found)\n {\n found=true;\n cout<<root->val<<endl;\n ans=root->val;\n return;\n }\n }\n int findBottomLeftValue(TreeNode* root) \n {\n int height=h(root);\n solve(root,height);\n return ans; \n }\n int h(TreeNode*root)\n {\n if(root==NULL)return 0;\n return 1+max(h(root->left),h(root->right));\n }\n};",
"memory": "23600"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*> q{{root}};\n TreeNode* node = nullptr;\n\n while (!q.empty()) {\n node = q.front();\n q.pop();\n if (node->right)\n q.push(node->right);\n if (node->left)\n q.push(node->left);\n }\n\n return node->val;\n }\n};",
"memory": "23700"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n if (!root) return 0;\n\n // Initialize a queue for level-order traversal\n queue<TreeNode*> q;\n q.push(root);\n int bottomLeftValue;\n\n while (!q.empty()) {\n \n TreeNode* node = q.front();\n \n q.pop();\n \n // On the first node of the level, update the bottomLeftValue\n bottomLeftValue = node->val;\n\n // Push left and right children into the queue if they exist\n \n if (node->right) q.push(node->right);\n if (node->left) q.push(node->left);\n }\n \n\n return bottomLeftValue;\n \n }\n};",
"memory": "23700"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n queue<pair<TreeNode*, int>> q;\n q.push({root, 0});\n TreeNode* ans = root;\n int level = 0;\n while(!q.empty()){\n auto curr = q.front();\n q.pop();\n if(curr.second>level){\n level = curr.second;\n ans = curr.first;\n }\n if(curr.first->left){\n q.push({curr.first->left, curr.second+1});\n }\n if(curr.first->right){\n q.push({curr.first->right, curr.second+1});\n }\n }\n\n return ans->val;\n }\n};",
"memory": "23800"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n int findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*> q{{root}};\n TreeNode* node = nullptr;\n\n while (!q.empty()) {\n node = q.front();\n q.pop();\n if (node->right)\n q.push(node->right);\n if (node->left)\n q.push(node->left);\n }\n\n return node->val;\n }\n};",
"memory": "23800"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n int val;\n int level;\n queue<pair<TreeNode*,int>>node;\n node.push({root,0});\n while(node.empty()==false){\n pair<TreeNode*,int>x=node.front();\n node.pop();\n if(x.second!=level){\n level=x.second;\n val=x.first->val;\n }\n if(x.first->left!=nullptr){\n node.push({x.first->left,x.second+1});\n }\n if(x.first->right!=nullptr){\n node.push({x.first->right,x.second+1});\n }\n }\n return val;\n }\n};",
"memory": "23900"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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:\nvoid lot(TreeNode* root,queue<TreeNode*>q,int &leftmost){\n q.push(root);\n q.push(NULL);\n while(!q.empty()){\n TreeNode* front=q.front();\n q.pop();\n if(front!=NULL){\n if(front->left!=NULL){\n q.push(front->left);\n }\n if(front->right!=NULL){\n q.push(front->right);\n }\n }\n else if(front==NULL){\n if(q.empty()){\n break;\n }\n else{\n leftmost=q.front()->val;\n q.push(NULL);\n }\n }\n }\n}\n int findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*> q;\n int leftmost=root->val;\n lot(root,q,leftmost);\n return leftmost;\n }\n};",
"memory": "24000"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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:\nvoid lot(TreeNode* root,queue<TreeNode*>q,int &leftmost){\n q.push(root);\n q.push(NULL);\n while(!q.empty()){\n TreeNode* front=q.front();\n q.pop();\n if(front!=NULL){\n if(front->left!=NULL){\n q.push(front->left);\n }\n if(front->right!=NULL){\n q.push(front->right);\n }\n }\n else if(front==NULL){\n if(q.empty()){\n break;\n }\n else{\n leftmost=q.front()->val;\n q.push(NULL);\n }\n }\n }\n}\n int findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*> q;\n int leftmost=root->val;\n lot(root,q,leftmost);\n return leftmost;\n }\n};",
"memory": "24000"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*> q;\n q.push(root);\nint answer;\n while (!q.empty()) {\n int n = q.size();\n vector<int> ans;\n for (int i = 0; i < n; i++) {\n TreeNode* temp = q.front();\n ans.push_back(temp->val);\n q.pop();\n\n if(temp->left)q.push(temp->left);\n if(temp->right)q.push(temp->right);\n\n\n\n }\n answer = ans[0];\n }\n\n return answer;\n }\n};",
"memory": "24100"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*> q;\n q.push(root);\n int ans;\n while(!q.empty()){\n int n=q.size();\n vector<int> level;\n while(n--){\n TreeNode* node=q.front();\n q.pop();\n level.push_back(node->val);\n if(node->left) q.push(node->left);\n if(node->right) q.push(node->right);\n }\n ans=level[0];\n }\n return ans;\n }\n};",
"memory": "24200"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) \n {\n queue<TreeNode*>q;\n q.push(root);\n int x=root->val;\n\n while(!q.empty())\n {\n int size=q.size();\n vector<int>v;\n while(size--)\n {\n TreeNode*temp=q.front();\n q.pop();\n v.push_back(temp->val);\n if(temp->left)\n q.push(temp->left);\n if(temp->right)\n q.push(temp->right);\n }\n x=v[0];\n }\n \n return x;\n }\n};",
"memory": "24300"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n vector<int> ans;\n queue<TreeNode*> q;\n q.push(root);\n // if(root == NULL)\n // return ans;\n while(!q.empty()){\n vector<int> level;\n int size = q.size();\n \n for(int i=0; i<size; i++){\n TreeNode *node = q.front();\n q.pop();\n level.push_back(node -> val);\n if(node -> left != NULL){\n q.push(node -> left);\n }\n if(node -> right != NULL){\n q.push(node -> right);\n }\n }\n ans.push_back(level[0]);\n \n }\n return ans[ans.size()-1];\n }\n};",
"memory": "24400"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n vector<int> ans;\n queue<TreeNode*> q;\n q.push(root);\n // if(root == NULL)\n // return ans;\n while(!q.empty()){\n vector<int> level;\n int size = q.size();\n \n for(int i=0; i<size; i++){\n TreeNode *node = q.front();\n q.pop();\n level.push_back(node -> val);\n if(node -> left != NULL){\n q.push(node -> left);\n }\n if(node -> right != NULL){\n q.push(node -> right);\n }\n }\n ans.push_back(level[0]);\n \n }\n return ans[ans.size()-1];\n }\n};",
"memory": "24400"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n vector<int> ans;\n queue<TreeNode*> q;\n q.push(root);\n // if(root == NULL)\n // return ans;\n while(!q.empty()){\n vector<int> level;\n int size = q.size();\n \n for(int i=0; i<size; i++){\n TreeNode *node = q.front();\n q.pop();\n level.push_back(node -> val);\n if(node -> left != NULL){\n q.push(node -> left);\n }\n if(node -> right != NULL){\n q.push(node -> right);\n }\n }\n ans.push_back(level[0]);\n \n }\n return ans[ans.size()-1];\n }\n};",
"memory": "24500"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n vector<int> ans;\n queue<TreeNode*> q;\n q.push(root);\n // if(root == NULL)\n // return ans;\n while(!q.empty()){\n vector<int> level;\n int size = q.size();\n \n for(int i=0; i<size; i++){\n TreeNode *node = q.front();\n q.pop();\n level.push_back(node -> val);\n if(node -> left != NULL){\n q.push(node -> left);\n }\n if(node -> right != NULL){\n q.push(node -> right);\n }\n }\n ans.push_back(level[0]);\n \n }\n return ans[ans.size()-1];\n }\n};",
"memory": "24500"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n vector<int>ans;\n if(!root)return 0;\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()){\n vector<int>temp;\n int levelsize=q.size();\n for(int i=0;i<levelsize;i++){\n TreeNode* newn=q.front();\n q.pop();\n if(newn->left) q.push(newn->left);\n if(newn->right) q.push(newn->right);\n temp.push_back(newn->val);\n }\n ans.push_back(temp[0]);\n }\n int n=ans.size();\n return ans[n-1];\n }\n};",
"memory": "24600"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 vector <pair<int,int>> v;\n\n void solver(TreeNode * root)\n {\n queue <pair<TreeNode *,int>> q;\n q.push({root,0});\n\n while(!q.empty())\n {\n auto p = q.front();\n TreeNode * par = p.first;\n int level = p.second;\n q.pop();\n\n v.push_back({par->val,level});\n\n if(par->left)\n {\n q.push({par->left,level+1});\n }\n\n if(par->right)\n {\n q.push({par->right,level+1});\n }\n }\n }\n\n int maxH(TreeNode * root)\n {\n if(root == NULL) return 0;\n\n int l = maxH(root->left);\n int r = maxH(root->right);\n\n return max(l,r) + 1;\n }\n\n int findBottomLeftValue(TreeNode* root) \n {\n solver(root);\n\n int sz = maxH(root)-1;\n cout << sz;\n\n for(int i=0;i<v.size();i++)\n {\n if(v[i].second == sz) return v[i].first;\n }\n\n return 0;\n }\n};",
"memory": "24700"
} |
513 | <p>Given the <code>root</code> of a binary tree, return the leftmost value in the last row of the tree.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" style="width: 302px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [2,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" style="width: 432px; height: 421px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,5,6,null,null,7]
<strong>Output:</strong> 7
</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>4</sup>]</code>.</li>
<li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></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 findBottomLeftValue(TreeNode* root) {\n queue<TreeNode*>q;\n q.push(root);\n q.push(NULL);\n vector<int>v;\n while(!q.empty())\n {\n vector<int>vtemp;\n while(q.front()!=NULL)\n {\n vtemp.push_back(q.front()->val);\n TreeNode*temp=q.front();q.pop();\n if(temp->left)q.push(temp->left);\n if(temp->right)q.push(temp->right);\n }\n q.pop();\n if(!q.empty())\n q.push(NULL);\n v=vtemp;\n }\n return v[0];\n }\n};",
"memory": "24800"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.