id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n map<char, int> mp;\n for(char c : s){\n mp[c]++;\n }\n vector<int> ans;\n while(mp['z']){\n ans.push_back(0);\n mp['z']--;\n mp['e']--;\n mp['r']--;\n mp['o']--;\n }\n while(mp['w']){\n ans.push_back(2);\n mp['t']--;\n mp['w']--;\n mp['o']--;\n }\n while(mp['u']){\n ans.push_back(4);\n mp['f']--;\n mp['o']--;\n mp['u']--;\n mp['r']--;\n }\n while(mp['x']){\n ans.push_back(6);\n mp['s']--;\n mp['i']--;\n mp['x']--;\n }\n while(mp['f']){\n ans.push_back(5);\n mp['f']--;\n mp['i']--;\n mp['v']--;\n mp['e']--;\n }\n while(mp['v']){\n ans.push_back(7);\n mp['s']--;\n mp['e']--;\n mp['v']--;\n mp['e']--;\n mp['n']--;\n }\n while(mp['g']){\n ans.push_back(8);\n mp['e']--;\n mp['i']--;\n mp['g']--;\n mp['h']--;\n mp['t']--;\n }\n while(mp['t']){\n ans.push_back(3);\n mp['t']--;\n mp['h']--;\n mp['r']--;\n mp['e']--;\n mp['e']--;\n }\n while(mp['o']){\n ans.push_back(1);\n mp['o']--;\n mp['n']--;\n mp['e']--;\n }\n while(mp['i']){\n ans.push_back(9);\n mp['n']--;\n mp['i']--;\n mp['n']--;\n mp['e']--;\n }\n sort(ans.begin(), ans.end());\n s = \"\";\n for(int i = 0; i < ans.size(); i++){\n s.push_back(ans[i] + '0');\n }\n return s;\n }\n};", "memory": "13200" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n vector<vector<int>> countV = formCounts();\n vector<int> v = formCountOfAString(s);\n vector<int> vFreq(10, 0);\n customOriginalDigits(v, countV, vFreq);\n return formStringFromFreq(vFreq);\n }\n\n string formStringFromFreq(vector<int> vFreq){\n string vs = \"\";\n for(int lv = 0; lv < (int)vFreq.size(); lv++){\n while(vFreq[lv]-- > 0){\n vs += to_string(lv);\n }\n }\n return vs;\n }\n\n void customOriginalDigits(vector<int>& v, vector<vector<int>>& countV, vector<int>& vFreq){\n for(int lv = 0; lv < 9; lv += 2){\n while(isDigitPresent(v, countV[lv])){\n removeDigit(v, countV[lv]);\n vFreq[lv] += 1;\n }\n }\n for(int lv = 1; lv < 10; lv += 2){\n while(isDigitPresent(v, countV[lv])){\n removeDigit(v, countV[lv]);\n vFreq[lv] += 1;\n }\n }\n \n }\n\n void removeDigit(vector<int>& v, vector<int>& cV){\n for(int lv = 0; lv < 26; lv++) v[lv] -= cV[lv];\n }\n\n bool isDigitPresent(vector<int>& v, vector<int>& cV){\n for(int lv = 0; lv < 26; lv++){\n if(cV[lv] > v[lv]) return false;\n }\n return true;\n }\n\n vector<vector<int>> formCounts(){\n vector<vector<int>> countV;\n vector<string> vS = {\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\"};\n for(int lv = 0; lv < vS.size(); lv++){\n countV.push_back(formCountOfAString(vS[lv]));\n }\n return countV;\n }\n\n vector<int> formCountOfAString(string s){\n vector<int> cD(26, 0);\n for(int lv1 = 0; lv1 < s.size(); lv1++){\n cD[s[lv1]-'a'] += 1;\n }\n return cD;\n }\n};", "memory": "13300" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n map<char,int> m;\n for(auto it: s){\n m[it]++;\n }\n vector<pair<char,pair<string,int>>> v={{'z',{\"zero\",0}},{'x',{\"six\",6}},{'s',{\"seven\",7}},{'w',{\"two\",2}},{'u',{\"four\",4}},{'o',{\"one\",1}},{'r',{\"three\",3}},{'f',{\"five\",5}},{'h',{\"eight\",8}},{'n',{\"nine\",9}}};\n string ans=\"\";\n int i=0;\n while(i<10){\n char ch=v[i].first;\n if(m[ch]==0){\n i++;\n continue;\n }\n string str=v[i].second.first;\n int num=v[i].second.second;\n for(int j=0;j<str.length();j++){\n m[str[j]]--;\n }\n ans+=(to_string(num));\n }\n vector<int> vec(10,0);\n for(auto it: ans){\n vec[it-'0']++;\n }\n ans=\"\";\n i=0;\n for(i=0;i<10;i++){\n if(vec[i]==0){\n continue;\n }\n while(vec[i]!=0){\n ans+=(to_string(i));\n vec[i]--;\n }\n }\n return ans;\n\n }\n};", "memory": "13700" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n map<char,int>mp;\n\n for(int i=0;i<s.length();i++)\n {\n mp[s[i]]++;\n\n }\n\n vector<int>num(10,0);\n num[0]=mp['z'];\n mp['e']-=mp['z'];\n mp['r']-mp['z'];\n mp['o']-=mp['z'];\n mp['z']=0;\n\n num[2]=mp['w'];\n mp['t']-=mp['w'];\n mp['o']-=mp['w'];\n mp['w']=0;\n\n num[4]=mp['u'];\n mp['f']-=mp['u'];\n mp['o']-=mp['u'];\n mp['r']-=mp['u'];\n mp['u']=0;\n\n\n num[6]=mp['x'];\n mp['s']-=mp['x'];\n mp['i']-=mp['x'];\n mp['x']=0;\n\n num[8]=mp['g'];\n mp['e']-=mp['g'];\n mp['i']-=mp['g'];\n mp['h']-=mp['g'];\n mp['t']-=mp['g'];\n mp['g']=0;\n\n num[1]=mp['o'];\n mp['n']-=mp['o'];\n mp['e']-=mp['o'];\n mp['o']=0;\n\n \n\n num[5]=mp['f'];\n mp['i']-=mp['f'];\n mp['v']-=mp['f'];\n mp['e']-=mp['f'];\n mp['f']=0;\n\n num[7]=mp['s'];\n mp['e']-=mp['s'];\n mp['e']-=mp['s'];\n mp['v']-=mp['s'];\n mp['n']-=mp['s'];\n\n\n num[3]=mp['h'];\n mp['t']-=mp['h'];\n mp['r']-=mp['h'];\n mp['e']-=mp['h'];\n mp['e']-=mp['h'];\n mp['h']=0;\n\n \n\n\n num[9]=mp['i'];\n \n string str=\"\";\n for(int i=0;i<=9;i++)\n {\n for(int j=0;j<num[i];j++)\n {\n str+=to_string(i);\n }\n\n }\n return str;\n\n \n }\n};\n//one-----o\n//five----f\n//seven---s\n//three---h\n//nine----i\n", "memory": "13800" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n map<char,int>mp;\n\n for(int i=0;i<s.length();i++)\n {\n mp[s[i]]++;\n\n }\n\n vector<int>num(10,0);\n num[0]=mp['z'];\n mp['e']-=mp['z'];\n mp['r']-mp['z'];\n mp['o']-=mp['z'];\n mp['z']=0;\n\n num[2]=mp['w'];\n mp['t']-=mp['w'];\n mp['o']-=mp['w'];\n mp['w']=0;\n\n num[4]=mp['u'];\n mp['f']-=mp['u'];\n mp['o']-=mp['u'];\n mp['r']-=mp['u'];\n mp['u']=0;\n\n\n num[6]=mp['x'];\n mp['s']-=mp['x'];\n mp['i']-=mp['x'];\n mp['x']=0;\n\n num[8]=mp['g'];\n mp['e']-=mp['g'];\n mp['i']-=mp['g'];\n mp['h']-=mp['g'];\n mp['t']-=mp['g'];\n mp['g']=0;\n\n num[1]=mp['o'];\n mp['n']-=mp['o'];\n mp['e']-=mp['o'];\n mp['o']=0;\n\n \n\n num[5]=mp['f'];\n mp['i']-=mp['f'];\n mp['v']-=mp['f'];\n mp['e']-=mp['f'];\n mp['f']=0;\n\n num[7]=mp['s'];\n mp['e']-=mp['s'];\n mp['e']-=mp['s'];\n mp['v']-=mp['s'];\n mp['n']-=mp['s'];\n\n\n num[3]=mp['h'];\n mp['t']-=mp['h'];\n mp['r']-=mp['h'];\n mp['e']-=mp['h'];\n mp['e']-=mp['h'];\n mp['h']=0;\n\n \n\n\n num[9]=mp['i'];\n \n string str=\"\";\n for(int i=0;i<=9;i++)\n {\n for(int j=0;j<num[i];j++)\n {\n str+=to_string(i);\n }\n\n }\n return str;\n\n \n }\n};\n//one-----o\n//five----f\n//seven---s\n//three---h\n//nine----i\n", "memory": "13900" }
423
<p>Given a string <code>s</code> containing an out-of-order English representation of digits <code>0-9</code>, return <em>the digits in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> s = "owoztneoer" <strong>Output:</strong> "012" </pre><p><strong class="example">Example 2:</strong></p> <pre><strong>Input:</strong> s = "fviefuro" <strong>Output:</strong> "45" </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is one of the characters <code>[&quot;e&quot;,&quot;g&quot;,&quot;f&quot;,&quot;i&quot;,&quot;h&quot;,&quot;o&quot;,&quot;n&quot;,&quot;s&quot;,&quot;r&quot;,&quot;u&quot;,&quot;t&quot;,&quot;w&quot;,&quot;v&quot;,&quot;x&quot;,&quot;z&quot;]</code>.</li> <li><code>s</code> is <strong>guaranteed</strong> to be valid.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string originalDigits(string s) {\n\n // map<string, in> digit(\n // {\"z\",0}; //unique-->z\n // {\"one\",1};\n // {\"w\",2}; // unique: w\n // {\"three\",3};\n // {\"u\",4}; // unique:u\n // {\"five\",5};\n // {\"x\",6}; //unique:x\n // {\"seven\",7};\n // {\"g\",8}; //unique g\n // {\"nine\",9};\n //});\n\n // con unique --> conti i pari\n // dei rimanenti: con f--> 4+5; h-->3+8; s-->7+6\n // rimangono 9 --> da i ;e 1---> da n: 7 e 9\n map<char,int> freq;\n\n for (auto c:s){\n ++freq[c];\n }\n\n vector<int> num(10,0);\n num[0]=freq['z'];\n num[2]=freq['w']; \n num[4]=freq['u'];\n num[5]=freq['f']-num[4];\n num[6]=freq['x'];\n num[7]=freq['s']-num[6];\n num[8]=freq['g'];\n num[3]=freq['h']-num[8];\n num[9]=freq['i']-num[5]-num[6]-num[8];\n num[1]=freq['n']-2*num[9]-num[7]; \n\n string v;\n\n for (size_t i=0;i<num.size();i++){\n int k=num[i];\n while (k>0){\n v.append(to_string(i));\n k--;\n }\n }\n return v;\n }\n};\n\n", "memory": "14000" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
0
{ "code": "class Solution {\n int characterReplacement_Brute(string s, int k) {\n int maxLen = 0;\n int n = s.size();\n for(int i = 0; i < n; i++){\n int maxFreq = 0;\n int hash[26]{0};\n for(int j = i; j < n; j++){\n hash[s[j] - 'A']++;\n maxFreq = max(maxFreq, hash[s[j] - 'A']);\n int changes = (j - i + 1) - maxFreq;\n if(changes <= k)\n maxLen = max(maxLen, j - i + 1);\n else\n break;\n }\n }\n return maxLen;\n }\n int characterReplacement_SlidingWindow(string s, int k) {\n int maxFreq = 0;\n int hash[26]{0};\n int maxLen = 0;\n int n = s.size();\n int i = 0, j = 0;\n while(j < n){\n hash[s[j] - 'A']++;\n maxFreq = max(maxFreq, hash[s[j] - 'A']);\n while( (j - i + 1) - maxFreq > k ){\n hash[s[i] - 'A']--;\n maxFreq = 0;\n for(int k = 0; k < 26; k++){\n maxFreq = max(maxFreq, hash[k]);\n }\n i++;\n }\n if( (j - i + 1) - maxFreq <= k ){\n maxLen = max(maxLen, (j - i + 1));\n }\n j++;\n }\n return maxLen;\n }\npublic:\n int characterReplacement(string s, int k) {\n return characterReplacement_SlidingWindow(s, k);\n }\n};", "memory": "8400" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n vector<int>count(26);\n int maxFreq = 0;\n int i = 0;\n int j = 0;\n int result = 0;\n while(j < s.size()){\n count[s[j] - 'A']++;\n maxFreq = max(maxFreq,count[s[j] - 'A']);\n if(j - i + 1 - maxFreq > k){\n count[s[i] - 'A']--;\n i++;\n }\n result = max(result,j - i + 1);\n j++;\n }\n return result;\n }\n};", "memory": "8400" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n = s.size();\n int l = 0, r = 0, maxF = 0;\n vector<int> v(26, 0);\n int maxLen = 0;\n while(r < n) {\n v[s[r]-'A']++;\n maxF = max(maxF, v[s[r]-'A']);\n if((r-l+1)-maxF > k) {\n v[s[l]-'A']--;\n maxF = 0;\n for(int i = 0; i<26;i++) {\n maxF = max(maxF, v[i]);\n }\n l++;\n }\n if((r-l+1)-maxF <= k) {\n maxLen = max(maxLen, r-l+1);\n }\n r++;\n }\n return maxLen;\n }\n};", "memory": "8500" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n=s.size();\n int i=0,j=0;\n int maxfreq=0,maxlen=0;\n int freq[26]={0};\n while(j<n)\n {\n freq[s[j]-'A']++;\n maxfreq=max(maxfreq,freq[s[j]-'A']);\n if((j-i+1)-maxfreq>k)\n {\n freq[s[i]-'A']--;\n\n i++;\n }\n if((j-i+1)-maxfreq<=k)\n {\n maxlen=max(maxlen,j-i+1);\n }\n j++;\n }\n return maxlen;\n }\n};", "memory": "8500" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n=s.size(), ans=0;\n for(char c='A'; c<='Z'; c++){\n int i=0, j=0, count=0;\n while(j<n){\n if(s[j]!=c){\n count++;\n }\n while(count>k){\n ans=max(ans,j-i);\n if(s[i]!=c) count--;\n i++;\n }\n j++;\n }\n ans=max(ans,j-i);\n }\n\n return ans;\n }\n};", "memory": "8600" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n = s.size();\n int j = 0;\n int i = 0;\n int max_len = 0;\n int max_freq = 0;\n vector<int>occurence(26,0);\n while(j<n){\n occurence[s[j]-'A']++;\n max_freq = max(max_freq , occurence[s[j]-'A']);\n while((j-i+1-max_freq)>k){\n occurence[s[i]-'A']--;\n max_freq = 0;\n for(auto &it:occurence){\n if(it>max_freq){\n max_freq = it;\n }\n }\n i++;\n }\n max_len = max(max_len,j-i+1);\n j++;\n }\n return max_len;\n }\n};", "memory": "8600" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n bool check(string& s,int len,int k)\n {\n vector<int> freq(26,0);\n\n for(int i=0;i<len;i++)\n {\n freq[s[i]-'A']++;\n }\n int maxi=*max_element(freq.begin(),freq.end());\n int rem=len-maxi;\n if(rem<=k) return true;\n int n=s.length();\n for(int i=len;i<n;i++)\n {\n char prev=s[i-len];\n freq[prev-'A']--;\n char curr=s[i];\n freq[curr-'A']++;\n maxi=*max_element(freq.begin(),freq.end());\n rem=len-maxi;\n if(rem<=k) return true;\n }\n return false;\n\n }\n int characterReplacement(string s, int k) {\n int n=s.length();\n int start=1,end=n;\n\n int ans=-1;\n\n while(start<=end)\n {\n int mid=start+(end-start)/2;\n if(check(s,mid,k))\n {\n ans=mid;\n start=mid+1;\n }\n else end=mid-1;\n }\n return ans;\n }\n};", "memory": "8700" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int variable_window(string s,int k) {\n int low = 0;\n int result=INT_MIN,longest=0;\n vector<int> chars(26,0);\n for(int high=0;high<s.length();high++) {\n \n chars[s[high] - 65]++;\n\n while(((high-low+1)-*max_element(chars.begin(),chars.end()))>k) {\n chars[s[low] - 65]--;\n low++;\n }\n\n result=max(result,(high - low + 1));\n }\n return result;\n }\n int characterReplacement(string s, int k) {\n if(s.length()==0 || s.length()==1 ) return s.length();\n return variable_window(s,k);\n }\n};", "memory": "8800" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int cc[26] = {0};\n char mc = s[0];\n cc[s[0]-'A']=1;\n int diversity = 0;\n int dInxSet=0, dInxGet=0, len = s.size();\n int dQueue[len];\n int i=0,j=1;\n int ans = 0;\n char prev = mc;\n while(j<len) {\n cc[s[j]-'A']++;\n if(prev!=s[j]) dQueue[dInxSet++] = j;\n if(s[j]!=mc) {\n diversity++;\n while(diversity>k) {\n //cout<<j<<\" \"<<diversity<<\" \"<<ans<<endl;\n int cur = j-i+1+k-diversity;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n i=dQueue[dInxGet++];\n //if(dInxGet>dInxSet) check\n mc = s[i];\n diversity-=cc[mc-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n //if(diversity<0) diversity=0;\n }\n }\n prev = s[j++];\n }\n //cout<<ans<<\"-\"<<diversity<<\"-\"<<i<<endl;\n while(dInxGet<dInxSet) {\n //cout<<j<<\" \"<<i<<\" \"<<diversity<<\" \"<<ans<<endl;\n int cur = j-i+k-diversity;\n if(cur>len) cur = len;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n i=dQueue[dInxGet++];\n //if(dInxGet>dInxSet) check\n mc = s[i];\n diversity-=cc[mc-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n //if(diversity<0) diversity=0;\n }\n int curr = j-i+k-diversity;\n if(curr>len) curr = len;\n if(ans<curr) ans=curr;\n return ans;\n }\n};\n", "memory": "8900" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int cc[26] = {0};\n char mc = s[0];\n cc[s[0]-'A']=1;\n int diversity = 0;\n int dInxSet=0, dInxGet=0, len = s.size();\n int dQueue[len];\n int i=0,j=1;\n int ans = 0;\n char prev = mc;\n while(j<len) {\n cc[s[j]-'A']++;\n if(prev!=s[j]) dQueue[dInxSet++] = j;\n if(s[j]!=mc) {\n diversity++;\n while(diversity>k) {\n //cout<<j<<\" \"<<diversity<<\" \"<<ans<<endl;\n int cur = j-i+1+k-diversity;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n i=dQueue[dInxGet++];\n //if(dInxGet>dInxSet) check\n mc = s[i];\n diversity-=cc[mc-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n //if(diversity<0) diversity=0;\n }\n }\n prev = s[j++];\n }\n //cout<<ans<<\"-\"<<diversity<<\"-\"<<i<<endl;\n while(dInxGet<dInxSet) {\n //cout<<j<<\" \"<<i<<\" \"<<diversity<<\" \"<<ans<<endl;\n int cur = j-i+k-diversity;\n if(cur>len) cur = len;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n i=dQueue[dInxGet++];\n //if(dInxGet>dInxSet) check\n mc = s[i];\n diversity-=cc[mc-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n //if(diversity<0) diversity=0;\n }\n int curr = j-i+k-diversity;\n if(curr>len) curr = len;\n if(ans<curr) ans=curr;\n return ans;\n }\n};\n", "memory": "8900" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution\n{\npublic:\n int characterReplacement(string s, int k)\n {\n // At level h : count of characters that have frequency = h in our sliding window\n // loc maps our char to its level\n // height is maximum height of our tower right now\n vector<int> counts(2);\n vector<int> loc(26);\n counts[0] = 25;\n counts[1] = 1;\n loc[s[0]-'A'] = 1;\n\n int i = 0, j = 0;\n int height = 1;\n int ans = 1;\n for (j = 1; j < s.length(); j++) {\n char c = s[j];\n\n counts[loc[c-'A']]--;\n if (loc[c-'A']+1 > height) {\n height++;\n counts.push_back(0);\n }\n loc[c-'A']++;\n counts[loc[c-'A']]++;\n\n // We get the total number of char in our window, and count the number of char\n // which are other than the most frequent one\n int total = j - i + 1;\n int excess = total - height;\n while (excess > k) {\n char d = s[i];\n counts[loc[d-'A']]--;\n loc[d-'A']--;\n counts[loc[d-'A']]++;\n if (counts[height] == 0)\n height--;\n i++;\n total = j - i + 1;\n excess = total - height;\n }\n ans = max(ans, total);\n }\n return ans;\n }\n};", "memory": "9000" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int cc[26] = {0};\n char mc = s[0];\n cc[s[0]-'A']=1;\n int diversity = 0;\n int dInxSet=0, dInxGet=0, len = s.size();\n int dQueue[len];\n int i=0,j=1;\n int ans = 0;\n char prev = mc;\n while(j<len) {\n cc[s[j]-'A']++;\n if(prev!=s[j]) dQueue[dInxSet++] = j;\n if(s[j]!=mc) {\n diversity++;\n while(diversity>k) {\n //cout<<j<<\" \"<<diversity<<\" \"<<ans<<endl;\n int cur = j-i+1+k-diversity;\n if(cur>len) return len;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n i=dQueue[dInxGet++];\n //if(dInxGet>dInxSet) check\n mc = s[i];\n diversity-=cc[mc-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n //if(diversity<0) diversity=0;\n }\n }\n prev = s[j++];\n }\n //cout<<ans<<\"-\"<<diversity<<\"-\"<<i<<endl;\n while(dInxGet<dInxSet) {\n //cout<<j<<\" \"<<i<<\" \"<<diversity<<\" \"<<ans<<endl;\n int cur = j-i+k-diversity;\n if(cur>len) return len;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n i=dQueue[dInxGet++];\n //if(dInxGet>dInxSet) check\n mc = s[i];\n diversity-=cc[mc-'A'];\n //cout<<diversity<<\" \"<<i<<endl;\n //if(diversity<0) diversity=0;\n }\n int curr = j-i+k-diversity;\n if(curr>len) curr = len;\n if(ans<curr) ans=curr;\n return ans;\n }\n};\n", "memory": "9100" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int cc[26] = {0};\n char mc = s[0];\n cc[s[0]-'A']=1;\n int diversity = 0;\n int dInxSet=0, dInxGet=0, len = s.size();\n int dQueue[len];\n int i=0,j=1;\n int ans = 0;\n char prev = mc;\n while(j<len) {\n cc[s[j]-'A']++;\n if(prev!=s[j]) dQueue[dInxSet++] = j;\n if(s[j]!=mc) {\n diversity++;\n while(diversity>k) {\n int cur = j-i+1+k-diversity;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n i=dQueue[dInxGet++];\n mc = s[i];\n diversity-=cc[mc-'A'];\n }\n }\n prev = s[j++];\n }\n while(dInxGet<dInxSet) {\n int cur = j-i+k-diversity;\n if(cur>len) cur = len;\n if(cur>ans) {\n ans=cur;\n cout<<ans<<\":\"<<s.substr(i,ans)<<endl;\n }\n cc[s[i]-'A'] -= (dQueue[dInxGet]-i);\n diversity+=cc[s[i]-'A'];\n i=dQueue[dInxGet++];\n mc = s[i];\n diversity-=cc[mc-'A'];\n }\n int curr = j-i+k-diversity;\n if(curr>len) return len;\n if(ans<curr) ans=curr;\n return ans;\n }\n};\n", "memory": "9100" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(const string& s, int k) {\n unordered_map<char, int> count; // Hash map to store the count of each character in the current window\n int maxCount = 0; // Variable to store the maximum count of a single character in the current window\n int maxLength = 0; // Variable to store the maximum length of the substring found\n int left = 0; // Left pointer of the sliding window\n\n // Iterate through the string using the right pointer\n for (int right = 0; right < s.length(); ++right) {\n count[s[right]]++; // Increment the count of the current character\n maxCount = max(maxCount, count[s[right]]); // Update maxCount if necessary\n\n // If the current window size minus maxCount is greater than k, shrink the window from the left\n while (right - left + 1 - maxCount > k) {\n count[s[left]]--; // Decrement the count of the character at the left pointer\n left++; // Move the left pointer to the right\n }\n\n // Calculate the length of the current window and update maxLength if necessary\n maxLength = max(maxLength, right - left + 1);\n }\n\n return maxLength; // Return the maximum length of the substring found\n }\n};", "memory": "9300" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(const string& s, int k) {\n unordered_map<char, int> count; \n int maxCount = 0; \n int maxLength = 0; \n int left = 0;\n\n // Iterate through the string using the right pointer\n for (int right = 0; right < s.length(); ++right) {\n count[s[right]]++; // Increment the count of the current character\n maxCount = max(maxCount, count[s[right]]); // Update maxCount if necessary\n\n // If the current window size minus maxCount is greater than k, \n // shrink the window from the left\n while (right - left + 1 - maxCount > k) {\n // Decrement the count of the character at the left pointer\n count[s[left]]--; \n left++; // Move the left pointer to the right\n // Update maxCount to reflect the new window\n maxCount = 0;\n for (const auto& pair : count) {\n maxCount = std::max(maxCount, pair.second);\n }\n }\n\n // Calculate the length of the current window and \n // update maxLength if necessary\n maxLength = max(maxLength, right - left + 1);\n }\n\n return maxLength; // Return the maximum length of the substring found\n }\n};", "memory": "9300" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n unordered_map<char, int> alphabets;\n int ans = 0;\n int left = 0;\n int right = 0;\n int maxf = 0;\n\n for (right = 0; right < s.size(); right++) {\n alphabets[s[right]] = 1 + alphabets[s[right]];\n maxf = max(maxf, alphabets[s[right]]);\n\n if ((right - left + 1) - maxf > k) {\n alphabets[s[left]] -= 1;\n left++;\n } else {\n ans = max(ans, (right - left + 1));\n }\n }\n\n return ans;\n }\n};\n", "memory": "9400" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int maxF = 0;\n int longest = 0;\n unordered_map<char, int> freq;\n\n int left = 0;\n for (int right = 0; right < s.length(); ++right) {\n freq[s[right]]++;\n maxF = max(maxF, freq[s[right]]);\n while (right - left + 1 - maxF > k) {\n freq[s[left]]--;\n left++;\n }\n longest = right - left + 1;\n }\n\n return longest;\n }\n};", "memory": "9400" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n=s.size();\n int maxLen=0;\n int l=0,r=0;\n unordered_map<char,int> freq;\n int maxFreq=0;\n while(r<n){\n freq[s[r]]++;\n maxFreq=max(maxFreq,freq[s[r]]);\n int len=r-l+1;\n if(len-maxFreq<=k){\n maxLen=max(maxLen,len);\n }\n else{\n freq[s[l]]--;\n l++;\n }\n r++;\n }\n return maxLen;\n }\n};", "memory": "9500" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int j=0;\n int n=s.length();\n int ans=0;\n int maxfreq=0;\n unordered_map<char,int>mp;\n for(int i=0;i<n;i++)\n {\n mp[s[i]]++;\n maxfreq=max(maxfreq,mp[s[i]]);\n if(i-j+1-maxfreq > k)\n {\n mp[s[j]]--;\n j++;\n }\n else\n ans=max(ans,i-j+1);\n }\n return ans;\n }\n};", "memory": "9500" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n=s.size();\n int i=0;\n int j=0;\n int maxi=0;\n int left=0;\n int right=0;\n int ans=0;\n unordered_map<char , int>mp;\n for(right=0;right<n;right++){\n mp[s[right]]++;\n maxi=max(maxi , mp[s[right]]);\n if((right-left+1)-maxi>k){\n mp[s[left]]--;\n left++;\n }\n else{\n ans=max(ans , right-left+1);\n }\n }\n return ans;\n \n }\n};", "memory": "9600" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n =s.size();\n int l=0,r=0,maxi=0,maxfreq=0;\n unordered_map<int,int>mp;\n while(r<n){\n mp[s[r]]++;\n maxfreq=max(maxfreq,mp[s[r]]);\n int currlen=r-l+1;\n if(currlen-maxfreq>k){\n mp[s[l]]--;\n l++;\n }\n maxi=max(maxi,r-l+1);\n r++;\n }\n return maxi;\n }\n};", "memory": "9600" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n =s.size();\n int low=0,high=0,maxi=0,maxfreq=0;\n unordered_map<char,int>mp;\n while(high<n){\n mp[s[high]]++;\n maxfreq=max(maxfreq,mp[s[high]]);\n int curlen=high-low+1;\n if(curlen-maxfreq>k){\n mp[s[low]]--;\n low++;\n }\n maxi=max(maxi,high-low+1);\n high++;\n }\n return maxi;\n }\n};", "memory": "9700" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n\n int n=s.size();\n int left=0;\n int right=0;\n int maxfrq=0;\n int maxlen=0;\n unordered_map<char,int>mp;\n while(right<n){\n mp[s[right]]++;\n maxfrq=max(maxfrq,mp[s[right]]);\n\n if((right-left+1)-maxfrq>k){\n mp[s[left]]--;\n left++;\n }\n maxlen=max(maxlen,right-left+1);\n right++;\n }\n return maxlen;\n \n }\n};", "memory": "9700" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int l=0,r=0,maxlen=0,maxf=0;\n unordered_map<char , int> count;\n while(r<s.size()){\n count[s[r]]++;\n maxf = max(maxf,count[s[r]]);\n if((r-l+1)-maxf>k){\n count[s[l]]--;\n l++;\n }\n maxlen=max(maxlen,r-l+1);\n r++;\n }\n return maxlen;\n }\n};", "memory": "9800" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n vector<char>v(256,-1);\n map<char,int>m;\n int l=0,r=0,maxf=0,ans=0;\n\n while(r<s.size()){\n m[s[r]-'A']++;\n maxf=max(maxf,m[s[r]-'A']);\n if(r-l+1-maxf>k){\n m[s[l]-'A']--;\n l++;\n }\n \n ans=max(ans,r-l+1);\n r++;\n \n \n }\n\n\n\n return ans;\n \n }\n};", "memory": "9800" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n unordered_map<char, int> mp;\n int maxfreq= 0;\n int l=0;\n int r=0;\n int maxlen=0;\n\n while(r<s.size())\n {\n mp[s[r]]++;\n maxfreq= max(maxfreq, mp[s[r]]);\n\n if(r-l+1 - maxfreq >k)\n {\n mp[s[l]]--;\n if(mp[s[l]]==0) mp.erase(s[l]);\n l++;\n }\n\n if(r-l+1 - maxfreq <=k)\n {\n maxlen= max(maxlen, r-l+1);\n }\n r++;\n }\n\n return maxlen;\n }\n};", "memory": "9900" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "#include <iostream>\n#include <unordered_map>\n#include <map>\nusing namespace std;\nclass Solution {\npublic:\n int characterReplacement(string s, int k) {\n \n //(window_size)-(max_freq) => no of replacements\n //no of replacements <= k\n\n // int l=0,r=0;\n // int winSize=r-l+1;\n // int n=s.size();\n // int max_count=0;\n // vector<int> count(26,0);\n // while(r<n)\n // {\n // while(winSize-max_count <= k && r<n)\n // {\n // winSize=r-l+1;\n // count[s[r]]++;\n // r++;\n // max_count=*max_element(count.begin(),count.end());\n // }\n // count[s[l]]--;\n // l++;\n // }\n // return winSize;\n\n\n // int l=0,r=0;\n // int n=s.size();\n // int max_count=0;\n // int res=0,maxf=0;\n // while(r<n)\n // {\n // hashmap[s[r]]++;\n // maxf=max(maxf,hashmap[s[r]]);\n // if((r-l+1)-maxf > k)\n // {\n // hashmap[s[l]]--;\n // l++;\n // // max_count=(*mpd::max_element(hashmap.begin(), hashmap.end())).second;\n // }\n // else\n // res=max(res,r-l+1);\n // r++;\n // }\n // return res;\n\n // int n=s.size();\n // int l=0,r=0;\n // unordered_map<char> mp;\n // int count=0;\n // int maxi=0;\n // int t=0;\n // while(r<n)\n // {\n // if(mp.find(s[r]) == mp.end())\n // {\n // mp.insert(s[r]);\n // }\n // count=r-l+1;\n // maxi=max(maxi,count);\n // while()\n // r++;\n // }\n // return maxi;\n\n unordered_map<char,int> mp;\n int l=0,r=0;\n int n=s.size();\n int max_freq=0;\n int window_size=0;\n int maxLen=0;\n while(r<n)\n {\n mp[s[r]]++;\n max_freq=max(max_freq,mp[s[r]]);\n window_size=r-l+1;\n while(window_size - max_freq > k && l<=r)\n {\n mp[s[l]]--;\n l++;\n window_size=r-l+1;\n max_freq=0;\n for(int i=0;i<mp.size();i++)\n max_freq=max(max_freq,mp[i]);\n }\n\n if(window_size - max_freq <= k)\n {\n maxLen=max(maxLen,window_size);\n }\n r++;\n }\n return maxLen;\n\n // unordered_map<char, int> mp;\n // int l = 0, r = 0;\n // int n = s.size();\n // int max_freq = 0; // Track the highest frequency of any character in the current window\n // int maxLen = 0;\n\n // while (r < n) {\n // // Increase the count for the current character\n // mp[s[r]]++;\n // // Update max_freq with the highest frequency in the current window\n // max_freq = max(max_freq, mp[s[r]]);\n\n // // Calculate the size of the current window\n // int window_size = r - l + 1;\n\n // // If the window has more than k characters that need to be replaced, shrink it\n // if (window_size - max_freq > k) {\n // mp[s[l]]--; // Remove the leftmost character from the window\n // l++; // Move the left boundary to the right\n // }\n\n // // Update maxLen with the size of the current valid window\n // maxLen = max(maxLen, r - l + 1);\n // r++;\n // }\n\n // return maxLen;\n }\n};", "memory": "9900" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n unordered_map<char, int> mp;\n int l=0, r=0;\n int res=0;\n int t=0;\n for(int i=0;i<s.size();i++){\n mp[s[i]]++;\n t=max(t, mp[s[i]]);\n if((i-l+1)-t > k) \n {\n mp[s[l]]--;\n if(mp[s[l]] == 0) mp.erase(s[l]);\n l++;\n }\n \n res = max(res, i-l+1);\n }\n return res;\n }\n};", "memory": "10000" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int i=0,j=0;\n unordered_map<char,int>mp;\n int cnt = 0;\n //int newChar =0;\n int maxFreq =INT_MIN;\n while(j<s.size())\n {\n mp[s[j]]++;\n maxFreq = max(maxFreq , mp[s[j]]);\n if((j-i+1)-maxFreq<=k)\n {\n cnt = max(j-i+1,cnt);\n j++;\n }\n else \n {\n while((j-i+1)-maxFreq>k)\n {\n mp[s[i]]--;\n //maxFreq = max(maxFreq,mp[s[i]]);\n if(mp[s[i]]==0)\n {\n mp.erase(s[i]);\n }\n i++;\n }\n j++;\n }\n }\n return cnt;\n }\n};", "memory": "10000" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n unordered_map<char,int>m;\n int i=0,j=0,ans=0,maxi=0;\n while(i<s.size())\n {\n m[s[i]]++;\n maxi=max(maxi,m[s[i]]);\n while(i-j+1-maxi>k)\n {\n m[s[j]]--;\n if(m[s[j]]==0)\n m.erase(s[j]);\n j++;\n }\n ans=max(ans,i-j+1);\n i++;\n }\n return ans;\n }\n};", "memory": "10100" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int i=0;\n int j=0;\n int ans=0;\n int maxfreq=INT_MIN;\n unordered_map<int,int> mp;\n int n=s.size();\n while(j<n)\n {\n mp[s[j]]++;\n maxfreq=max(maxfreq,mp[s[j]]);\n if(j-i+1-maxfreq<=k)\n {\n ans=max(ans,j-i+1);\n }\n else\n {\n mp[s[i]]--;\n if(mp[s[i]]==0)\n {\n mp.erase(s[i]);\n }\n i++;\n }\n j++;\n }\n return ans;\n }\n};", "memory": "10100" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int start = 0;\n int end = 0;\n int res = 0;\n map<char,int> m;\n int max_chars_till_now = 0;\n\n while (end < s.length()) {\n m[s[end]]++;\n max_chars_till_now = max(max_chars_till_now, m[s[end]]);\n\n // increase start\n while ((end-start+1) - max_chars_till_now > k) {\n m[s[start]]--;\n if (m[s[start]] == 0)\n m.erase(s[start]);\n start++;\n }\n\n res = max(res, end-start+1);\n end++;\n }\n\n return res;\n }\n};\n/*\nBasic idea:\nKeep max characters till now\nHow?\n1. keep a variable max_chars_till_now = 0\n2. add to map<char,freq>\n3. if (end-start+1) - max_chars_till_now > k\nthis means that number of chars to swap is greater than k\nSo, increment start.\n\n\n*/", "memory": "10500" }
424
<p>You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.</p> <p>Return <em>the length of the longest substring containing the same letter you can get after performing the above operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;ABAB&quot;, k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the two &#39;A&#39;s with two &#39;B&#39;s or vice versa. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;AABABBA&quot;, k = 1 <strong>Output:</strong> 4 <strong>Explanation:</strong> Replace the one &#39;A&#39; in the middle with &#39;B&#39; and form &quot;AABBBBA&quot;. The substring &quot;BBBB&quot; has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only uppercase English letters.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int characterReplacement(string s, int k) {\n int n = s.size();\n int maxi = 1;\n \n map<char,int> mp;\n int l = 0 , r = 0;\n while(r < n){\n mp[s[r]]++;\n int len = r - l + 1;\n int maxfreq = 1;\n for(auto i : mp){\n if(i.second > maxfreq){\n maxfreq = i.second;\n }\n }\n if(len - maxfreq <= k) maxi = max(maxi,len);\n else{\n mp[s[l]]--;\n if(mp[s[l]] == 0) mp.erase(s[l]);\n l++;\n }\n r++;\n }\n return maxi;\n }\n};", "memory": "10600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n grid_ = move(grid);\n leafNodes_[0] = new Node(false, true, nullptr, nullptr, nullptr, nullptr);\n leafNodes_[1] = new Node(true, true, nullptr, nullptr, nullptr, nullptr);\n return construct(0, 0, grid_.size());\n }\n \n Node* construct(int r, int c, int s) {\n if (s == 1)\n return leafNodes_[grid_[r][c]];\n s /= 2;\n Node* tl = construct(r, c, s);\n Node* tr = construct(r, c+s, s);\n Node* bl = construct(r+s, c, s);\n Node* br = construct(r+s, c+s, s);\n if (tl == tr && tl == bl && tl == br)\n return tl;\n return new Node(false, false, tl, tr, bl, br);\n }\n \nprivate:\n vector<vector<int>> grid_;\n array<Node*, 2> leafNodes_;\n};", "memory": "16200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\n vector<Node*> leafs;\npublic:\n Node* construct(vector<vector<int>>& grid) {\n leafs.push_back(new Node(false, true));\n leafs.push_back(new Node(true, true));\n return solve(grid, 0, 0, grid.size());\n }\n\n Node* solve(vector<vector<int>>& grid, int ti, int tj, int n) {\n if (n == 1)\n return leafs[grid[ti][tj]];\n auto tl = solve(grid, ti, tj, n/2);\n auto tr = solve(grid, ti, tj + n/2, n/2);\n auto bl = solve(grid, ti + n/2, tj, n/2);\n auto br = solve(grid, ti + n/2, tj + n/2, n/2);\n if (tl == tr && bl == br && tr == bl)\n return tl;\n return new Node(false, false, tl, tr, bl, br);\n }\n};", "memory": "16400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nNode* zero = new Node(false, true);\nNode* one = new Node(true, true);\n\nclass Solution {\npublic:\n Node* create(vector <vector<int>> &grid, int x, int y, int n) {\n if(n==1) {\n return grid[x][y] == 1 ? one : zero;\n }\n int mid = n/2;\n Node* topleft = create(grid, x, y, mid);\n Node* topright = create(grid, x, y+mid, mid);\n Node* botleft = create(grid, x+mid, y, mid);\n Node* botright = create(grid, x+mid, y+mid, mid);\n\n if(topleft == topright and botleft == botright and topleft == botleft) {\n return topleft;\n }\n return new Node(false, false, topleft, topright, botleft, botright);\n }\n Node* construct(vector<vector<int>>& grid) {\n return create(grid, 0, 0, grid.size());\n }\n};", "memory": "16500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n vector<vector<int>> grid_;\n // only two types of leaf node: 0 and 1\n // use cache instead of creating new node for every leaf\n vector<Node*> leafNodes_ = {nullptr, nullptr};\npublic:\n // to avoid having to pass in grid, we can just set\n // class object equal to grid and edit that instead\n Node* construct(vector<vector<int>>& grid) {\n grid_ = move(grid);\n\n leafNodes_[0] = new Node(false, true, nullptr, nullptr, nullptr, nullptr);\n leafNodes_[1] = new Node(true, true, nullptr, nullptr, nullptr, nullptr);\n\n return recurse(0, 0, grid_.size());\n }\n // to split grid, 4 options:\n // r, c\n // r , c + s\n // r+s, c\n // r+s, c+s\n\n // where s = size of length/height of grid\n\n // have to recurse by splitting grid into 4 sections\n // if the size of the grid == 1, then isLeaf = true, bool = grid[i][j],\n // then return true\n // also, if all subtrees are same value, then turn into leaf set val = value;\n \n Node* recurse(int r, int c, int s){\n if (s == 1){\n if (grid_[r][c] == 0) return leafNodes_[0];\n return leafNodes_[1];\n }\n\n s /= 2;\n \n Node* topLeft = recurse(r, c, s);\n Node* topRight = recurse(r, c+s, s);\n\n Node* bottomLeft = recurse(r+s, c, s);\n Node* bottomRight = recurse(r+s, c+s, s);\n\n // all subtrees are equal, so don't connect to subtrees, instead just make\n // this a leaf\n if (topLeft == topRight && \n topRight == bottomLeft && \n bottomLeft == bottomRight){\n // just return corresponding leaf node\n return topLeft;\n }\n // otherwise, connect to subtrees\n\n return new Node(false, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n};", "memory": "16600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "class Solution\n{\npublic:\n Node* construct(vector<vector<int>>& grid)\n {\n int n = grid.size();\n Node* root = new Node();\n stack<tuple<Node*, int, int, int>> tasks{};\n tasks.push({ root, 0, 0, n });\n\n while (!tasks.empty()) {\n auto [r, i, j, len] = tasks.top();\n if (len == 1) {\n r->isLeaf = true;\n r->val = grid[i][j];\n tasks.pop();\n continue;\n }\n\n if (r->bottomLeft != nullptr) {\n tasks.pop();\n continue;\n }\n\n bool color = grid[i][j] == 1, isLeaf = true; \n for (int ip = i; ip < i + len; ip++) {\n for (int jp = j; jp < j + len; jp++) {\n if ((grid[ip][jp] == 1) != color) {\n isLeaf = false; \n break;\n }\n }\n if (!isLeaf)\n break;\n }\n\n if (isLeaf) {\n r->isLeaf = true; \n r->val = color;\n tasks.pop(); \n } else {\n r->topLeft = new Node(); \n r->topRight = new Node(); \n r->bottomLeft = new Node(); \n r->bottomRight = new Node(); \n\n int new_len = len / 2;\n tasks.push({r->topLeft, i, j, new_len}); \n tasks.push({r->topRight, i, j + new_len, new_len}); \n tasks.push({r->bottomLeft, i + new_len, j, new_len}); \n tasks.push({r->bottomRight, i + new_len, j + new_len, new_len});\n }\n\n }\n\n return root;\n }\n};", "memory": "16700" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n void solve(vector<vector<int>>& grid, int r, int c, int n, Node* &root){\n if(root == NULL) root = new Node(true, false);\n bool found = false;\n int v = grid[r][c];\n for(int i=r;i<r+n;i++){\n for(int j=c;j<c+n;j++){\n if(grid[i][j] != v){\n found = true;\n break;\n }\n }\n if(found) break;\n }\n if(!found) {\n root->val = v;\n root->isLeaf = true;\n return;\n }\n n = n/2;\n solve(grid, r, c, n, root->topLeft);\n solve(grid, r, c+n, n, root->topRight);\n solve(grid, r+n, c, n, root->bottomLeft);\n solve(grid, r+n, c+n, n,root->bottomRight);\n }\n\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n Node* root = new Node(true, false);\n solve(grid, 0, 0, n, root);\n return root;\n }\n};", "memory": "16800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int size = grid.size();\n Node* root = new Node();\n checkBlock(root, grid, 0, 0, size);\n return root;\n }\n\nprivate:\n bool blockSameVal(vector<vector<int>>& grid, int x, int y, int size) {\n for (int i = x; i < x+size; i++) {\n for (int j = y; j<y+size; j++) {\n if (grid[x][y] != grid[i][j]) {\n return false;\n }\n }\n }\n return true;\n }\n\n void checkBlock(Node* root, vector<vector<int>>& grid, int x, int y, int size) {\n if (blockSameVal(grid, x, y, size)) {\n root->val = grid[x][y];\n root->isLeaf = true;\n return;\n }\n \n // Check new 4 quadrants\n int halfSize = size/2;\n\n root->topLeft = new Node();\n checkBlock(root->topLeft, grid, x, y, halfSize);\n root->topRight = new Node();\n checkBlock(root->topRight, grid, x, y+halfSize, halfSize);\n root->bottomLeft = new Node();\n checkBlock(root->bottomLeft, grid, x+halfSize, y, halfSize);\n root->bottomRight = new Node();\n checkBlock(root->bottomRight, grid, x+halfSize, y+halfSize, halfSize);\n }\n};", "memory": "17200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool sameVal(vector<vector<int>>& grid, int x, int y, int size)\n {\n for(auto i{x}; i<x+size; ++i)\n {\n for(auto j{y}; j<y+size; ++j)\n {\n if(grid[i][j] != grid[x][y])\n {\n return false;\n }\n }\n }\n return true;\n }\n void rec(Node* root, vector<vector<int>>& grid, int x, int y, int size)\n {\n if(sameVal(grid, x, y, size))\n {\n root->val = grid[x][y];\n root->isLeaf=true;\n return;\n }\n const auto new_size{size/2};\n \n root->topLeft = new Node();\n rec(root->topLeft, grid, x, y, new_size);\n \n root->topRight = new Node();\n rec(root->topRight, grid, x, y+new_size, new_size);\n \n root->bottomLeft = new Node();\n rec(root->bottomLeft, grid, x+new_size, y, new_size);\n\n root->bottomRight = new Node();\n rec(root->bottomRight, grid, x+new_size, y+new_size, new_size);\n }\n Node* construct(vector<vector<int>>& grid) \n {\n const auto n{grid.size()};\n auto root = new Node();\n rec(root, grid, 0, 0, n);\n return root;\n }\n};", "memory": "17300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n void recurse(vector<vector<int>>& grid, int start_i, int start_j, int end_i, int end_j, Node** p){\n bool flag = true;\n int prev = grid[start_i][start_j];\n for(int i = start_i; i < end_i; i++){\n for(int j = start_j; j < end_j; j++){\n if(prev != grid[i][j]){\n flag = false;\n break;\n }\n prev = grid[i][j];\n }\n if(!flag)\n break;\n }\n if(!flag){\n *p = new Node(true, false);\n int mid_i = (start_i + end_i) / 2;\n int mid_j = (start_j + end_j) / 2;\n \n recurse(grid, start_i, start_j, mid_i, mid_j, &(*p)->topLeft);\n recurse(grid, start_i, mid_j, mid_i, end_j, &(*p)->topRight);\n recurse(grid, mid_i, start_j, end_i, mid_j, &(*p)->bottomLeft);\n recurse(grid, mid_i, mid_j, end_i, end_j, &(*p)->bottomRight);\n }\n else{\n *p = new Node(grid[start_i][start_j], true);\n }\n }\n Node* construct(vector<vector<int>>& grid) {\n Node* root = NULL;\n recurse(grid, 0, 0, grid.size(), grid.size(), &root);\n return root;\n }\n};", "memory": "17400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n void recurse(int x1, int y1, int x2, int y2, Node* curNode, vector<vector<int>>& sums){\n int bigSum = sums[x2][y2];\n int smallSum = 0;\n int n = x2-x1+1;\n if(x1-1 >= 0 && y1 -1 >= 0) smallSum -= sums[x1-1][y1-1];\n if(x1-1 >= 0) smallSum += sums[x1-1][y2];\n if(y1-1 >= 0) smallSum += sums[x2][y1-1];\n int curSum = bigSum - smallSum;\n if(curSum == 0 || curSum == (n*n)){\n curNode->val = curSum == 0 ? 0 : 1;\n curNode->isLeaf = true;\n return;\n }\n curNode->isLeaf = false;\n curNode->topLeft = new Node();\n curNode->topRight = new Node();\n curNode->bottomLeft = new Node();\n curNode->bottomRight = new Node();\n recurse(x1,y1, x1+n/2-1, y1+n/2-1, curNode->topLeft, sums);\n recurse(x1+n/2,y1+n/2, x2, y2, curNode->bottomRight, sums);\n recurse(x1+n/2 ,y1, x2, y1+n/2-1, curNode->bottomLeft, sums);\n recurse(x1,y1+n/2, x1+n/2-1, y2, curNode->topRight, sums);\n }\n\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> sums(n, vector<int>(n, 0));\n for(int i = 0;i < n;i++){\n for(int j = 0;j < n;j++){\n int curSum = grid[i][j];\n if(i-1 >= 0) curSum += sums[i-1][j];\n if(j-1 >= 0) curSum += sums[i][j-1];\n if(i-1 >= 0 && j-1 >= 0) curSum -= sums[i-1][j-1];\n sums[i][j] = curSum;\n }\n }\n Node* head = new Node();\n recurse(0,0, n-1,n-1, head, sums);\n return head;\n }\n};", "memory": "17500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n void helperfunction(Node*& root, vector<vector<int>>& board, int index1, int index2, int n)\n {\n if (n == 1)\n {\n root = new Node(board[index1][index2], true);\n return;\n }\n int i, j;\n pair<bool, int>topleft = { true,board[index1][index2] };\n pair<bool, int>topright = { true,board[index1][index2+n / 2] };\n pair<bool, int>bottomleft = { true,board[index1+n / 2][index2] };\n pair<bool, int>bottomright = { true,board[index1+n / 2][index2+n / 2] };\n for (i = index1; i < index1 + n / 2; i++)\n {\n for (j = index2; j < index2 + n / 2; j++)\n {\n if (board[i][j] != topleft.second)\n topleft.first = false;\n }\n }\n\n for (i = index1; i < index1 + n / 2; i++)\n {\n for (j = index2 + n / 2; j < index2 + n; j++)\n {\n if (board[i][j] != topright.second)\n topright.first = false;\n }\n }\n for (i = index1 + n / 2; i < index1 + n; i++)\n {\n for (j = index2; j < index2 + n / 2; j++)\n {\n if (board[i][j] != bottomleft.second)\n bottomleft.first = false;\n }\n }\n for (i = index1 + n / 2; i < index1 + n; i++)\n {\n for (j = index2 + n / 2; j < index2 + n; j++)\n {\n if (board[i][j] != bottomright.second)\n bottomright.first = false;\n }\n }\n if (topleft.second==topright.second&&topright.second==bottomleft.second&&bottomleft.second==bottomright.second&&topleft.first && topright.first && bottomleft.first && bottomright.first)\n {\n root = new Node(board[0][0], true);\n }\n else\n {\n root = new Node(1, false);\n if(!topleft.first)\n helperfunction(root->topLeft, board, index1, index2, n / 2);\n else\n {\n root->topLeft = new Node(topleft.second, true);\n }\n if (!topright.first)\n helperfunction(root->topRight, board, index1, index2 + n / 2, n / 2);\n else\n {\n root->topRight = new Node(topright.second, true);\n }\n if (!bottomleft.first)\n helperfunction(root->bottomLeft, board, index1 + n / 2, index2, n / 2);\n else\n {\n root->bottomLeft = new Node(bottomleft.second, true);\n }\n if (!bottomright.first)\n helperfunction(root->bottomRight, board, index1 + n / 2, index2 + n / 2, n / 2);\n else\n {\n root->bottomRight = new Node(bottomright.second, true);\n }\n }\n }\n Node* construct(vector<vector<int>>& grid) {\n Node* root = NULL;\n helperfunction(root, grid, 0, 0, grid.size());\n return root;\n }\n};", "memory": "17600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n void util(vector<vector<int>>& grid, int istart, int iend, int jstart, int jend, Node* root){\n int imid = (istart + iend)/2;\n int jmid = (jstart + jend)/2;\n\n int ini = grid[istart][jstart];\n int isAllSame = true;\n for(int i = istart;i<=imid;i++){\n for(int j =jstart;j<=jmid;j++){\n if(grid[i][j] !=ini){\n isAllSame = false;\n break;\n }\n }\n }\n if(isAllSame){\n root->topLeft = new Node(ini, true);\n }else{\n root->topLeft = new Node();\n util(grid, istart, imid, jstart, jmid, root->topLeft);\n }\n\n ini = grid[istart][jmid+1];\n isAllSame = true;\n for(int i = istart;i<=imid;i++){\n for(int j =jmid+1;j<=jend;j++){\n if(grid[i][j] !=ini){\n isAllSame = false;\n break;\n }\n }\n }\n\n if(isAllSame){\n root->topRight = new Node(ini, true);\n }else{\n root->topRight = new Node();\n util(grid, istart, imid,jmid+1, jend, root->topRight);\n }\n\n ini = grid[imid+1][jstart];\n isAllSame = true;\n for(int i= imid+1;i<=iend;i++){\n for(int j =jstart;j<=jmid;j++){\n if(grid[i][j] !=ini){\n isAllSame = false;\n break;\n }\n }\n }\n\n if(isAllSame){\n root->bottomLeft = new Node(ini, true);\n }else{\n root->bottomLeft = new Node();\n util(grid,imid+1, iend, jstart, jmid, root->bottomLeft);\n }\n\n ini = grid[imid+1][jmid+1];\n isAllSame = true;\n for(int i= imid+1;i<=iend;i++){\n for(int j =jmid+1;j<=jend;j++){\n if(grid[i][j] !=ini){\n isAllSame = false;\n break;\n }\n }\n }\n\n if(isAllSame){\n root->bottomRight = new Node(ini, true);\n }else{\n root->bottomRight = new Node();\n util(grid, imid+1, iend,jmid+1, jend, root->bottomRight);\n }\n }\n\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n Node* root;\n int ini = grid[0][n-1];\n int isAllSame = true;\n for(int i= 0;i<=n-1;i++){\n for(int j =0;j<=n-1;j++){\n if(grid[i][j] !=ini){\n isAllSame = false;\n break;\n }\n }\n }\n if(isAllSame){\n root = new Node(ini, true);\n return root;\n }else{\n root = new Node();\n util(grid, 0, n-1,0, n-1, root);\n }\n return root;\n }\n};", "memory": "17700" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\nbool allSame (vector<vector<int>> & grid , int x , int y , int n ){\n int val = grid[x][y];\n for( int i =x ; i< x+n ;i++){\n for( int j = y ; j < y+n ; j++){\n if(grid[i][j] != val) return false ;\n }\n }\n return true ;\n}\nNode * solve(vector<vector<int>>& grid , int x , int y , int n ){\n if(allSame ( grid ,x, y , n )){\n Node * root = new Node( grid[x][y] , true );\n return root ;\n }\n else {\n Node* root = new Node ( 1 , false);\n root->topLeft = solve(grid , x ,y , n/2);\n root->topRight = solve(grid , x ,y+n/2 , n/2); \n root->bottomLeft = solve(grid , x+n/2 ,y , n/2);\n root->bottomRight = solve(grid , x+n/2 ,y+n/2 , n/2);\n \n return root ;\n }\n\n \n}\n Node* construct(vector<vector<int>>& grid) {\n return solve ( grid , 0 , 0 , grid.size() ) ;\n }\n};", "memory": "18400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/* 9:45\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n int isAllSame(vector<vector<int>>& grid, int x, int y, int n) {\n int val = grid[x][y];\n for(int i = x; i < x + n; i++) {\n for(int j = y; j < y + n; j++) {\n if(grid[i][j] != val)\n return -1;\n }\n }\n return val;\n }\n\n Node* fun(vector<vector<int>>& grid, int x, int y, int n) {\n Node* node = new Node();\n int same = isAllSame(grid, x, y, n);\n if(same != -1) {\n node->isLeaf = true;\n node->val = same;\n return node;\n }\n\n node->topLeft = fun(grid, x, y, n/2);\n node->topRight = fun(grid, x, y + n/2, n/2);\n node->bottomLeft = fun(grid, x + n/2, y, n/2);\n node->bottomRight = fun(grid, x + n/2, y + n/2, n/2);\n return node;\n \n }\n Node* construct(vector<vector<int>>& grid) {\n return fun(grid, 0, 0, grid.size());\n }\n};", "memory": "18500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n // Returns true if all the values in the matrix are the same; otherwise, false.\n bool sameValue(vector<vector<int>>& grid, int x1, int y1, int length) {\n for (int i = x1; i < x1 + length; i++) {\n for (int j = y1; j < y1 + length; j++)\n if (grid[i][j] != grid[x1][y1])\n return false;\n }\n return true;\n }\n \n Node* solve(vector<vector<int>>& grid, int x1, int y1, int length) {\n // Return a leaf node if all values are the same.\n if (sameValue(grid, x1, y1, length)) {\n return new Node(grid[x1][y1], true);\n } else {\n Node* root = new Node(false, false);\n\n // Recursive call for the four sub-matrices.\n root -> topLeft = solve(grid, x1, y1, length / 2);\n root -> topRight = solve(grid, x1, y1 + length / 2, length / 2);\n root -> bottomLeft = solve(grid, x1 + length / 2, y1, length / 2);\n root -> bottomRight = solve(grid, x1 + length / 2, y1 + length / 2, length / 2);\n\n return root;\n }\n }\n \n Node* construct(vector<vector<int>>& grid) {\n return solve(grid, 0, 0, grid.size());\n }\n};", "memory": "18600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int const n = grid.size();\n return build(grid, 0, n, 0,n );\n }\n \n Node* build(vector<vector<int>> const& grid, int i0, int ni, int j0, int nj){\n bool leaf = true;\n int const ref = grid[i0][j0];\n for(int i=i0; i<i0+ni && leaf; ++i){\n for(int j=j0; j<j0+nj && leaf; ++j){\n leaf = leaf && (ref == grid[i][j]);\n }\n }\n if(leaf){\n return new Node(ref, true);\n }\n else{\n return new Node(\n 1, false,\n build(grid, i0, ni/2, j0, nj/2),\n build(grid, i0, ni/2, j0+nj/2, nj/2),\n build(grid, i0+ni/2, ni/2, j0, nj/2),\n build(grid, i0+ni/2, ni/2, j0+nj/2, nj/2));\n }\n }\n};", "memory": "18600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n bool checkSameVal(vector<vector<int>> &grid, int m, int n,int x, int y)\n {\n bool ans = false;\n int val = grid[x][y];\n for(int i=x;i<x+n;i++)\n {\n for(int j=y;j<y+n;j++)\n {\n if(grid[i][j] != val)\n return false;\n } \n }\n\n return true;\n }\n\n Node* recur(vector<vector<int>> &grid, int m, int n, int x, int y)\n {\n if(checkSameVal(grid,m,n,x,y))\n {\n return new Node(grid[x][y],1);\n }\n else\n {\n Node* temp = new Node(true,false);\n temp->topLeft = recur(grid,m/2,n/2,x,y);\n temp->topRight = recur(grid,m/2,n/2,x,y+n/2);\n temp->bottomLeft = recur(grid,m/2,n/2,x+m/2,y);\n temp->bottomRight = recur(grid,m/2,n/2,x+m/2,y+n/2);\n return temp;\n }\n }\n Node* construct(vector<vector<int>>& grid) {\n return recur(grid,grid.size(),grid[0].size(),0,0);\n }\n};", "memory": "18700" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n bool isallsame(vector<vector<int>>&grid,int x,int y,int n){\n int intial=grid[x][y];\n for(int i=x;i<x+n;i++){\n for(int j=y;j<y+n;j++){\n if(intial!=grid[i][j]){\n return false;\n }\n } } \n return true;}\n Node*constructquadtree(vector<vector<int>>&grid,int x,int y,int n){\n if(isallsame(grid,x,y,n)){\n return new Node(grid[x][y],true);\n } \n Node*node=new Node(grid[x][y],false);\n \n node->topLeft=constructquadtree(grid,x,y,n/2);\n node->topRight=constructquadtree(grid,x,y+n/2,n/2);\n node->bottomLeft=constructquadtree(grid,x+n/2,y,n/2);\n node->bottomRight=constructquadtree(grid,x+n/2,y+n/2,n/2);\n return node; \n }\npublic:\n Node* construct(vector<vector<int>>& grid) {\n return constructquadtree(grid,0,0,grid.size());\n }\n};", "memory": "18700" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node *solve(vector<vector<int>> &grid,int sr,int sc,int er,int ec){\n if(sr>er||sc>ec){\n NULL;\n }\n bool isLeaf=true;\n int value=grid[sr][sc];\n for(int i=sr;i<=er;i++){\n for(int j=sc;j<=ec;j++){\n if(value!=grid[i][j]){\n isLeaf=false;\n break;\n }\n }\n }\n Node *ans=new Node();\n if(isLeaf){\n ans->isLeaf=true;\n ans->val=grid[sr][sc];\n return ans;\n }\n int mr=(sr+er)/2;\n int mc=(sc+ec)/2;\n ans->topLeft=solve(grid,sr,sc,mr,mc);\n ans->topRight=solve(grid,sr,mc+1,mr,ec);\n ans->bottomLeft=solve(grid,mr+1,sc,er,mc);\n ans->bottomRight=solve(grid,mr+1,mc+1,er,ec);\n return ans;\n\n \n }\n Node* construct(vector<vector<int>>& grid) {\n int n=grid.size();\n return solve(grid,0,0,n-1,n-1);\n }\n};", "memory": "18800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
0
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n bool isAllSame(vector<vector<int>> & grid,int x, int y, int n)\n {\n int val = grid[x][y];\n\n for(int i = x; i<x+n; i++)\n {\n for(int j = y; j<y+n; j++)\n {\n if(grid[i][j]!=val)\n return false;\n }\n }\n return true;\n }\nNode* solve(vector<vector<int>> &grid, int x, int y, int n)\n{\n if(isAllSame(grid,x,y,n))\n return new Node(grid[x][y], true);\n\n else\n {\n Node *root = new Node(1,false);\n root->topLeft = solve(grid,x,y,n/2);\n root->topRight = solve(grid,x,y+n/2,n/2);\n root->bottomLeft = solve(grid,x+n/2,y,n/2);\n root->bottomRight = solve(grid,x+n/2,y+n/2,n/2);\n return root;\n }\n \n}\n\n Node* construct(vector<vector<int>>& grid) {\n \n return solve(grid,0,0,grid.size());\n }\n};", "memory": "18800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
1
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n\n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node*\n_bottomLeft, Node* _bottomRight) { val = _val; isLeaf = _isLeaf; topLeft =\n_topLeft; topRight = _topRight; bottomLeft = _bottomLeft; bottomRight =\n_bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n bool checkAllValue(vector<vector<int>>& grid, int r_st, int r_ed, int c_st,\n int c_ed) {\n int val = grid[r_st][c_st];\n for (int i = r_st; i <= r_ed; i++) {\n for (int j = c_st; j <= c_ed; j++) {\n if (grid[i][j] != val) {\n return false;\n }\n }\n }\n return true;\n }\n Node* treeMake(vector<vector<int>>& grid, int r_st, int r_ed, int c_st,\n int c_ed) {\n if (r_st > r_ed || c_st > c_ed) {\n return NULL;\n }\n bool flag = checkAllValue(grid, r_st, r_ed, c_st, c_ed);\n if (flag) {\n return new Node(grid[r_st][c_st] == 1, true);\n }\n int r_mid = (r_st + r_ed) / 2;\n int c_mid = (c_st + c_ed) / 2;\n Node* topLeft = treeMake(grid, r_st, r_mid, c_st, c_mid);\n Node* topRight = treeMake(grid, r_st, r_mid, c_mid + 1, c_ed);\n Node* bottomLeft = treeMake(grid, r_mid + 1, r_ed, c_st, c_mid);\n Node* bottomRight = treeMake(grid, r_mid + 1, r_ed, c_mid + 1, c_ed);\n return new Node(1, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n return treeMake(grid, 0, n - 1, 0, n - 1);\n }\n};", "memory": "18900" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
1
{ "code": " class Solution {\npublic:\n // Helper function to check if all elements in the grid are the same\n bool isUniform(vector<vector<int>>& grid, int x, int y, int length) {\n int val = grid[x][y];\n for (int i = x; i < x + length; ++i) {\n for (int j = y; j < y + length; ++j) {\n if (grid[i][j] != val) {\n return false;\n }\n }\n }\n return true;\n }\n \n // Main function to construct the Quad-Tree\n Node* construct(vector<vector<int>>& grid, int x = 0, int y = 0, int length = -1) {\n // Initialize length if it's the first call\n if (length == -1) length = grid.size();\n \n // Base case: If the grid is uniform (all 0's or all 1's)\n if (isUniform(grid, x, y, length)) {\n return new Node(grid[x][y] == 1, true);\n }\n \n // Divide the grid into four quadrants\n int halfLength = length / 2;\n \n Node* topLeft = construct(grid, x, y, halfLength);\n Node* topRight = construct(grid, x, y + halfLength, halfLength);\n Node* bottomLeft = construct(grid, x + halfLength, y, halfLength);\n Node* bottomRight = construct(grid, x + halfLength, y + halfLength, halfLength);\n \n // Create a non-leaf node\n return new Node(true, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n};\n", "memory": "19000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
1
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n#include<iostream>\n#include<bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n bool isLeaf(vector<vector<int>>& grid, int row, int col, int n) {\n int val = grid[row][col];\n for(int i=row; i<row+n; i++) {\n for(int j=col; j<col+n; j++) {\n if (grid[i][j] != val) {\n return false;\n }\n }\n }\n return true;\n }\n Node* getSubTree(vector<vector<int>>& grid, int col, int row, int n) {\n // if ((n-col) == 1) {\n\n // }\n if(isLeaf(grid, row, col, n)) {\n return new Node(grid[row][col], true);\n }\n cout<<\"in getSubTree col = \"<<col<<\" row = \"<<row<<\" n = \"<<n<<endl;\n int row_start = row, row_end = row+n, col_start = col, col_end = col+n, row_mid = row_start + (row_end-row_start) / 2, col_mid = col_start + (col_end - col_start)/2;\n cout<<\"row_start = \"<<row_start<<\" col_start = \"<<col_start<<\" row_mid = \"<<row_mid<<\" col_mid = \"<<col_mid<<endl;\n Node* topLeft = isLeaf(grid, row_start, col_start, row_mid - row_start) ? new Node( grid[row_start][col_start], true) : getSubTree(grid, col_start, row_start, row_mid - row_start);\n Node* topRight = isLeaf(grid, row_start, col_mid, row_mid - row_start) ? new Node(grid[row_start][col_mid], true) : getSubTree(grid, col_mid, row_start, row_mid - row_start);\n Node* bottomLeft = isLeaf(grid, row_mid, col_start, row_mid - row_start) ? new Node( grid[row_mid][col_start], true) : getSubTree(grid, col_start, row_mid, row_mid - row_start);\n Node* bottomRight = isLeaf(grid, row_mid, col_mid, row_mid - row_start) ? new Node( grid[row_mid][col_mid], true) : getSubTree(grid, col_mid, row_mid, row_mid - row_start);\n\n // Node* node = new Node(false, 0, topLeft, topRight, bottomLeft, bottomRight);\n // return node;\n return new Node(0, false, topLeft, topRight, bottomLeft, bottomRight);\n } \n Node* construct(vector<vector<int>>& grid) {\n Node* node;\n if(grid.size() == 1) {\n return new Node(grid[0][0], true);\n }\n if(grid.size() == 2) {\n if(isLeaf(grid, 0, 0, 2) == true) {\n cout<<\"returning......\"<<endl;\n return new Node(grid[0][0], true);\n }\n Node* topLeft = new Node(grid[0][0], true);\n Node* topRight = new Node(grid[0][1], true);\n Node* bottomLeft = new Node(grid[1][0], true);\n Node* bottomRight = new Node(grid[1][1], true);\n return new Node(0, false, topLeft, topRight, bottomLeft, bottomRight);\n } else {\n if(isLeaf(grid, 0, 0, grid.size()) == true) {\n cout<<\"whole leaf\"<<endl;\n return new Node(grid[0][0], true);\n }\n \n int start = 0, end = grid.size(), mid = (end-start) / 2;\n cout<<\"start = \"<<start<<\" end = \"<<end<<\" mid = \"<<mid<<endl;\n\n Node* topLeft = isLeaf(grid, start, start, mid-start) ? new Node(grid[start][start], true) : getSubTree(grid, start, start, mid-start);\n Node* topRight = isLeaf(grid, start, mid, mid-start) ? new Node(grid[start][mid], true) : getSubTree(grid, mid, start, mid-start);\n Node* bottomLeft = isLeaf(grid, mid, start, mid-start) ? new Node(grid[mid][start], true) : getSubTree(grid, start, mid, mid-start);\n Node* bottomRight = isLeaf(grid, mid, mid, end-mid) ? new Node(grid[mid][mid], true) : getSubTree(grid, mid, mid, mid-start);\n node = new Node(0, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n cout<<\"cam ehere/////\"<<endl;\n return node;\n }\n};", "memory": "19000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
1
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\n // returns 1 if all elements have 1 as value\n // 0 if all elements have 0 as value\n // -1 if elements have different values\n int isSameValue(vector<vector<int>>& grid, int rs, int re, int cs, int ce) {\n int val = grid[rs][cs];\n for (int r = rs; r < re; r++) {\n for (int c = cs; c < ce; c++) {\n if (val != grid[r][c]) {\n return -1;\n }\n }\n }\n return val;\n }\n\n Node* getNodes(vector<vector<int>>& grid, int rs, int re, int cs, int ce) {\n int val = isSameValue(grid, rs, re, cs, ce);\n\n if (val != -1) {\n return new Node(val == 1, true, nullptr, nullptr, nullptr, nullptr);\n }\n else {\n Node* topLeft, * topRight, * bottomLeft, * bottomRight;\n\n topLeft = getNodes(grid, rs, (rs + re) / 2, cs, (cs + ce) / 2);\n\n topRight = getNodes(grid, rs, (rs + re) / 2, (cs + ce) / 2, ce);\n\n bottomLeft = getNodes(grid, (rs + re) / 2, re, cs, (cs + ce) / 2);\n \n bottomRight = getNodes(grid, (rs + re) / 2, re, (cs + ce) / 2, ce);\n\n return new Node(true, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n }\n\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n return getNodes(grid, 0, n, 0, n);\n }\n};", "memory": "19100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n return make(grid, 0, n - 1, 0, n - 1);\n }\n\n Node* make(const vector<vector<int>>& grid, int row_start, int row_end, int col_start, int col_end) {\n int n = row_end - row_start + 1;\n\n bool is_leaf = true;\n bool val = grid[row_start][col_start];\n for (int row = row_start; row <= row_end; row++) {\n for (int col = col_start; col <= col_end; col++) {\n if (grid[row][col] != val) {\n is_leaf = false;\n break;\n }\n }\n if (!is_leaf) break;\n }\n\n if (is_leaf) {\n return new Node(val, true);\n }\n\n Node* top_left = make(grid, row_start, row_start + n / 2 - 1, col_start, col_start + n / 2 - 1);\n Node* bottom_left = make(grid, row_start + n / 2, row_end, col_start, col_start + n / 2 - 1);\n Node* bottom_right = make(grid, row_start + n / 2, row_end, col_start + n / 2, col_end);\n Node* top_right = make(grid, row_start, row_start + n / 2 - 1, col_start + n / 2, col_end);\n\n Node* new_node = new Node();\n bool all_children_leaf = top_left->isLeaf && bottom_left->isLeaf && bottom_right->isLeaf && top_right->isLeaf;\n if (all_children_leaf) {\n bool all_children_same = top_left->val == bottom_left->val && bottom_left->val == bottom_right->val && bottom_right->val == top_right->val;\n bool val = top_left->val;\n if (all_children_same) {\n delete top_left;\n delete bottom_left;\n delete bottom_right;\n delete top_right;\n return new Node(val, true);\n }\n }\n\n return new Node(false, false, top_left, top_right, bottom_left, bottom_right);\n }\n};", "memory": "19200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<Node*>> nodes(n, vector<Node*>(n, NULL));\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n nodes[i][j] = new Node(grid[i][j] == 1, true, NULL, NULL, NULL, NULL);\n }\n }\n while (n) {\n n /= 2;\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n int r = i * 2, c = j * 2;\n Node* tl = nodes[r][c];\n Node* tr = nodes[r][c+1];\n Node* bl = nodes[r+1][c];\n Node* br = nodes[r+1][c+1];\n if (tl->val==tr->val && tl->val==bl->val && tl->val==br->val && tl->isLeaf && tr->isLeaf && bl->isLeaf && br->isLeaf) {\n nodes[i][j] = new Node(tl->val, true, NULL, NULL, NULL, NULL);\n } else {\n nodes[i][j] = new Node(false, false, tl, tr, bl, br);\n }\n }\n }\n }\n return nodes[0][0];\n }\n};", "memory": "19300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n Node* construct(vector<vector<int>>& grid, int x_start, int x_end, int y_start, int y_end) {\n int x_range = x_end - x_start;\n int y_range = y_end - y_start;\n if(x_range == 1) {\n return new Node(grid[x_start][y_start], true);\n }\n\n int val = grid[x_start][y_start];\n for(int row = x_start; row < x_end; row++) {\n for(int col = y_start; col < y_end; col++) {\n if(grid[row][col] != val) {\n Node* parent = new Node();\n parent->topLeft = construct(grid, x_start, x_start + x_range / 2, y_start, y_start + y_range / 2);\n parent->topRight = construct(grid, x_start, x_start + x_range / 2, y_start + y_range / 2, y_end);\n parent->bottomLeft = construct(grid, x_start + x_range / 2, x_end, y_start, y_start + y_range / 2);\n parent->bottomRight = construct(grid, x_start + x_range / 2, x_end, y_start + y_range / 2, y_end);\n return parent;\n }\n }\n }\n\n return new Node(val, true);\n }\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n return construct(grid, 0, n, 0, n);\n }\n};", "memory": "19400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* solve(int sr,int sc,int er,int ec,vector<vector<int>>&grid) {\n\n int zeros=0,ones=0;\n for(int i=sr;i<=er;i++) {\n for(int j=sc;j<=ec;j++) {\n if(grid[i][j]==0) zeros++;\n else ones++;\n }\n }\n\n if(zeros == 0) {\n Node* newNode=new Node(1,true);\n return newNode;\n }else if(ones == 0) {\n Node* newNode=new Node(0,true);\n return newNode;\n }\n\n Node* newNode=new Node(0,false);\n int midR=(sr+er)/2;\n int midC=(sc+ec)/2;\n newNode->topLeft=solve(sr,sc,midR,midC,grid);\n newNode->topRight=solve(sr,midC+1,midR,ec,grid);\n newNode->bottomLeft=solve(midR+1,sc,er,midC,grid);\n newNode->bottomRight=solve(midR+1,midC+1,er,ec,grid);\n\n return newNode;\n }\n\n Node* construct(vector<vector<int>>& grid) {\n int n=grid.size();\n return solve(0,0,n-1,n-1,grid);\n }\n};", "memory": "19500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n Node* helper(vector<vector<int>> &grid,int row1,int col1,int row2,int col2){\n int sum=0;\n int flag=0;\n int n=row2-row1+1;\n int m=col2-col1+1;\n // cout<<row1<<\" \"<<col1<<endl;\n // cout<<row2<<\" \"<<col2<<endl;\n for(int i=row1;i<=row2;i++){\n for(int j=col1;j<=col2;j++){\n sum=sum+grid[i][j];\n }\n }\n // cout<<sum<<endl;\n if(sum==n*m){\n return new Node(1,1);\n }\n if(sum==0){\n return new Node(0,1);\n }\n \n Node* tL=helper(grid,row1,col1,row1+(n/2)-1,col1+(m/2)-1);\n Node* tR=helper(grid,row1,col1+(m/2),row1+(n/2)-1,col2);\n Node* bL=helper(grid,row1+(n/2),col1,row2,col1+(m/2)-1);\n Node* bR=helper(grid,row1+(n/2),col1+(m/2),row2,col2);\n return new Node(0,0,tL,tR,bL,bR); \n\n }\n Node* construct(vector<vector<int>>& grid) {\n int n=grid.size();\n int m=grid[0].size();\n return helper(grid,0,0,n-1,m-1); \n }\n};", "memory": "19600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* func(vector<vector<int>>& grid, vector<vector<int>>& vec, int x1, int y1, int x2, int y2){\n int n = x2-x1+1;\n if(x1==0 && y1==0){\n if(grid[x1][y1] && vec[x2][y2]==n*n)\n return new Node(true, true);\n else if(!grid[x1][y1] && vec[x2][y2]==0)\n return new Node(false, true);\n else{\n Node* root = new Node();\n root->topLeft = func(grid, vec, x1, y1, x1 + (n-1)/2, y1 + (n-1)/2);\n root->topRight = func(grid, vec, x1, y1 + n/2, x1 + (n-1)/2, y2);\n root->bottomLeft = func(grid, vec, x1 + n/2, y1, x2, y1 + (n-1)/2);\n root->bottomRight = func(grid, vec, x1 + n/2, y1 + n/2, x2, y2);\n return root;\n }\n }else if(x1==0){\n if(grid[x1][y1] && vec[x2][y2]-vec[x2][y1-1]==n*n)\n return new Node(true, true);\n else if(!grid[x1][y1] && vec[x2][y2]-vec[x2][y1-1]==0)\n return new Node(false, true);\n else{\n Node* root = new Node();\n root->topLeft = func(grid, vec, x1, y1, x1 + (n-1)/2, y1 + (n-1)/2);\n root->topRight = func(grid, vec, x1, y1 + n/2, x1 + (n-1)/2, y2);\n root->bottomLeft = func(grid, vec, x1 + n/2, y1, x2, y1 + (n-1)/2);\n root->bottomRight = func(grid, vec, x1 + n/2, y1 + n/2, x2, y2);\n return root;\n }\n }else if(y1==0){\n if(grid[x1][y1] && vec[x2][y2]-vec[x1-1][y2]==n*n)\n return new Node(true, true);\n else if(!grid[x1][y1] && vec[x2][y2]-vec[x1-1][y2]==0)\n return new Node(false, true);\n else{\n Node* root = new Node();\n root->topLeft = func(grid, vec, x1, y1, x1 + (n-1)/2, y1 + (n-1)/2);\n root->topRight = func(grid, vec, x1, y1 + n/2, x1 + (n-1)/2, y2);\n root->bottomLeft = func(grid, vec, x1 + n/2, y1, x2, y1 + (n-1)/2);\n root->bottomRight = func(grid, vec, x1 + n/2, y1 + n/2, x2, y2);\n return root;\n }\n }else{\n if(grid[x1][y1] && vec[x2][y2]-vec[x1-1][y2]-vec[x2][y1-1]+vec[x1-1][y1-1]==n*n)\n return new Node(true, true);\n else if(!grid[x1][y1] && vec[x2][y2]-vec[x1-1][y2]-vec[x2][y1-1]+vec[x1-1][y1-1]==0)\n return new Node(false, true);\n else{\n Node* root = new Node();\n root->topLeft = func(grid, vec, x1, y1, x1 + (n-1)/2, y1 + (n-1)/2);\n root->topRight = func(grid, vec, x1, y1 + n/2, x1 + (n-1)/2, y2);\n root->bottomLeft = func(grid, vec, x1 + n/2, y1, x2, y1 + (n-1)/2);\n root->bottomRight = func(grid, vec, x1 + n/2, y1 + n/2, x2, y2);\n return root;\n }\n }\n }\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> vec(n, vector<int>(n, 0));\n vec[0][0] = grid[0][0];\n for(int i = 1; i < n; i++)\n vec[i][0] = grid[i][0] + vec[i-1][0], vec[0][i] = grid[0][i] + vec[0][i-1];\n for(int i = 1; i < n; i++){\n for(int j = 1; j < n; j++){\n vec[i][j] = grid[i][j] + vec[i-1][j] + vec[i][j-1] - vec[i-1][j-1];\n\n }\n }\n Node* root;\n root = func(grid, vec, 0, 0, n-1, n-1);\n return root;\n }\n};", "memory": "19700" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\n Node* construct(const pair<int, int>& top_left, int length, const vector<vector<int>>& grid) {\n if (length == 0) {\n return nullptr;\n }\n\n auto [row, col] = top_left;\n\n bool all_same = true;\n for (int i = row; i < row + length; i++) {\n for (int j = col; j < col + length; j++) {\n if (grid[i][j] != grid[row][col]) {\n all_same = false;\n break;\n }\n }\n }\n\n Node* node = new Node(grid[row][col], all_same);\n\n if (!all_same) {\n int next_length = length / 2;\n\n node->topLeft = construct(top_left, next_length, grid);\n node->topRight = construct({ row, col + next_length }, next_length, grid);\n node->bottomLeft = construct({ row + next_length, col }, next_length, grid);\n node->bottomRight = construct({ row + next_length, col + next_length }, next_length, grid);\n }\n\n return node;\n }\npublic:\n Node* construct(vector<vector<int>>& grid) {\n pair<int, int> top_left = { 0, 0 };\n int length = grid.size();\n\n return construct(top_left, length, grid);\n }\n};", "memory": "19800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n Node* construct(vector<vector<int>>& grid, vector<vector<int>>& prefix_sum, int x_start, int x_end, int y_start, int y_end) {\n int x_range = x_end - x_start;\n int y_range = y_end - y_start;\n if(x_range == 1) {\n return new Node(grid[x_start][y_start], true);\n }\n\n int total = prefix_sum[x_end - 1][y_end - 1];\n int first = x_start > 0 ? prefix_sum[x_start - 1][y_end - 1] : 0;\n int second = y_start > 0 ? prefix_sum[x_end - 1][y_start - 1] : 0;\n int common = x_start > 0 && y_start > 0 ? prefix_sum[x_start - 1][y_start - 1] : 0;\n int sum = total - first - second + common;\n \n if(sum == 0) \n return new Node(0, true);\n\n if(sum == x_range * y_range)\n return new Node(1, true);\n\n Node* parent = new Node();\n parent->topLeft = construct(grid, prefix_sum, x_start, x_start + x_range / 2, y_start, y_start + y_range / 2);\n parent->topRight = construct(grid, prefix_sum, x_start, x_start + x_range / 2, y_start + y_range / 2, y_end);\n parent->bottomLeft = construct(grid, prefix_sum, x_start + x_range / 2, x_end, y_start, y_start + y_range / 2);\n parent->bottomRight = construct(grid, prefix_sum, x_start + x_range / 2, x_end, y_start + y_range / 2, y_end);\n return parent;\n }\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> prefix_sum(n , vector<int>(n, 0));\n for(int row = 0; row < n; row++) {\n for(int col = 0; col < n; col++) {\n int first = row > 0 ? prefix_sum[row-1][col] : 0;\n int second = col > 0 ? prefix_sum[row][col-1] : 0;\n int common = row > 0 && col > 0 ? prefix_sum[row-1][col-1] : 0;\n prefix_sum[row][col] = first + second - common + grid[row][col];\n }\n }\n return construct(grid, prefix_sum, 0, n, 0, n);\n }\n};", "memory": "19900" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\n vector<vector<int>> g;\n int n;\npublic:\n bool check_same(int rl, int rr, int cl, int cr) {\n for (int i = rl; i < rr ; i ++) {\n for (int j = cl; j < cr; j ++) {\n if (g[i][j] != g[rl][cl]) return false;\n }\n }\n return true;\n }\n Node * dfs(int cur, int rl, int cl) {\n // return new Node(g[rl][cl], true);\n if (cur == 1) {\n return new Node(g[rl][cl], true);\n }\n bool is_same = check_same(rl, rl + cur, cl, cl + cur);\n if (is_same) {\n return new Node(g[rl][cl], true);\n } \n cur = cur/2;\n Node * topleft = dfs(cur, rl, cl);\n Node * topright = dfs(cur, rl, cl + cur);\n Node * bottomleft = dfs(cur, rl + cur, cl);\n Node * bottomright = dfs(cur, rl + cur, cl + cur);\n Node * node = new Node(0, false, topleft, topright, bottomleft, bottomright);\n return node;\n } \n\n Node* construct(vector<vector<int>>& grid) {\n n = grid.size();\n g.resize(n, vector<int>(n));\n for(int i = 0;i < n; i ++) {\n for (int j = 0; j < n; j ++) {\n g[i][j] = grid[i][j];\n cout << g[i][j];\n }\n }\n Node * ans = new Node();\n ans = dfs(n, 0, 0);\n return ans;\n // return dfs(n, 0, 0);\n }\n};", "memory": "20000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n int getValue(int x1, int y1, int x2, int y2, vector<vector<int>>& precompute) {\n int total = precompute[x2][y2];\n if((x1 > 0) && (y1 > 0)) {\n total += precompute[x1 - 1][y1 - 1];\n }\n if(x1 > 0) {\n total -= precompute[x1 - 1][y2];\n }\n if(y1 > 0) {\n total -= precompute[x2][y1 - 1];\n }\n // cout<<\"x1 \"<<x1<<\" y1 \"<<y1<<\" x2 \"<<x2<<\" y2 \"<<y2<<\" total: \"<<total<<endl;\n return total;\n }\n Node* fun(int x1, int y1, int x2, int y2, vector<vector<int>>& precompute) {\n if((x2 < x1) || (y2 < y1)) {\n return NULL;\n }\n if((x2 == x1) && (y2 == y1)) {\n return new Node(getValue(x1,y1,x2,y2,precompute), true, NULL, NULL, NULL, NULL);\n }\n int gridValue = getValue(x1,y1,x2,y2,precompute);\n int gridSize = (x2 - x1 + 1) * (y2 - y1 + 1);\n if(gridValue == 0) {\n return new Node(0, true, NULL, NULL, NULL, NULL);\n }else if(gridValue == gridSize) {\n return new Node(1, true, NULL, NULL, NULL, NULL);\n }else {\n int x3 = (x2 + x1)/2;\n int y3 = (y2 + y1)/2;\n Node* top_left = fun(x1, y1, x3,y3, precompute);\n Node* top_right = fun(x3 + 1, y1, x2,y3, precompute);\n Node* bottom_left = fun(x1, y3 + 1, x3,y2, precompute);\n Node* bottom_right = fun(x3 + 1, y3 + 1, x2,y2, precompute);\n return new Node(1, false, top_left, bottom_left, top_right, bottom_right);\n }\n }\n\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n vector<vector<int>> precompute(n, vector<int>(n, 0));\n for(int i = 0;i < n;i++) {\n for(int j = 0;j < n;j++) {\n int sum = 0;\n if(i > 0) {\n sum += precompute[i - 1][j];\n }\n if(j > 0) {\n sum += precompute[i][j - 1];\n }\n if(i > 0 && j > 0) {\n sum -= precompute[i - 1][j - 1];\n }\n sum += grid[i][j];\n precompute[i][j] = sum;\n cout<<\"i \"<<i<<\" j \"<<j<<\" val \"<<sum<<endl;\n }\n }\n return fun(0,0,n - 1,n - 1, precompute);\n }\n};", "memory": "20100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n int isLeaf(const vector<vector<int>>& grid, int n, vector<int> start) {\n int res = grid[start[0]][start[1]];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n if (grid[i+start[0]][j+start[1]] != res) {\n return -1;\n }\n }\n }\n return res;\n }\n\n Node* construct(vector<vector<int>>& grid) {\n return createQuadTree(grid, grid.size(), {0, 0});\n }\n\n Node* createQuadTree(const vector<vector<int>>& grid, int n, vector<int> start) {\n int val = isLeaf(grid, n, start);\n Node* node = new Node();\n if (val != -1) {\n node->val = val;\n node->isLeaf = true;\n return node;\n }\n int x = start[0], y = start[1];\n node->topLeft = createQuadTree(grid, n/2, {x, y});\n node->bottomLeft = createQuadTree(grid, n/2, {x+n/2, y});\n node->topRight = createQuadTree(grid, n/2, {x, y+n/2});\n node->bottomRight = createQuadTree(grid, n/2, {x+n/2, y+n/2});\n return node;\n }\n};", "memory": "20800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n return helper(grid, 0, 0, grid.size());\n }\n\n private:\n Node* helper(const vector<vector<int>>& grid, int i, int j, int w) {\n if (allSame(grid, i, j, w))\n return new Node(grid[i][j], true);\n\n Node* node = new Node(true, false);\n node->topLeft = helper(grid, i, j, w / 2);\n node->topRight = helper(grid, i, j + w / 2, w / 2);\n node->bottomLeft = helper(grid, i + w / 2, j, w / 2);\n node->bottomRight = helper(grid, i + w / 2, j + w / 2, w / 2);\n return node;\n }\n\n bool allSame(const vector<vector<int>>& grid, int i, int j, int w) {\n return all_of(begin(grid) + i, begin(grid) + i + w,\n [&](const vector<int>& row) {\n return all_of(begin(row) + j, begin(row) + j + w,\n [&](int num) { return num == grid[i][j]; });\n });\n }\n};", "memory": "20900" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n int isLeaf(const vector<vector<int>>& grid, int row, int col, vector<int> offset) {\n int res = grid[offset[0]][offset[1]];\n for (int i = 0; i < row; i++) {\n for (int j = 0; j < col; j++) {\n if (grid[i+offset[0]][j+offset[1]] != res) {\n return -1;\n }\n }\n }\n return res;\n }\n\n Node* construct(vector<vector<int>>& grid) {\n return createQuadTree(grid, grid.size(), {0, 0});\n }\n\n Node* createQuadTree(const vector<vector<int>>& grid, int n, vector<int> offset) {\n int val = isLeaf(grid, n, n, offset);\n Node* node = new Node();\n if (val != -1) {\n node->val = val;\n node->isLeaf = true;\n return node;\n }\n int x = offset[0], y = offset[1];\n node->topLeft = createQuadTree(grid, n/2, {x, y});\n node->bottomLeft = createQuadTree(grid, n/2, {x+n/2, y});\n node->topRight = createQuadTree(grid, n/2, {x, y+n/2});\n node->bottomRight = createQuadTree(grid, n/2, {x+n/2, y+n/2});\n return node;\n }\n};", "memory": "21000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n\n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node*\n_bottomLeft, Node* _bottomRight) { val = _val; isLeaf = _isLeaf; topLeft =\n_topLeft; topRight = _topRight; bottomLeft = _bottomLeft; bottomRight =\n_bottomRight;\n }\n};\n*/\n\nclass Solution {\n private:\n Node* constQuad(vector<vector<int>>& grid, int i, int j,\n int new_n) {\n if (allSame(grid, i, j, new_n)) {\n return new Node(grid[i][j], true);\n }\n\n Node* root = new Node(true, false);\n root->topLeft = constQuad(grid, i, j, new_n / 2);\n root->topRight = constQuad(grid, i, j + new_n / 2, new_n / 2);\n\n root->bottomLeft = constQuad(grid, i + new_n / 2, j, new_n / 2);\n\n root->bottomRight =\n constQuad(grid, i + new_n / 2, j + new_n / 2, new_n / 2);\n\n return root;\n }\n bool allSame(const vector<vector<int>>& grid, int i, int j, int new_n) {\n return all_of(\n begin(grid)+i, begin(grid) + i + new_n,\n [&](const vector<int>& row) {\n return all_of(begin(row) + j, begin(row) + j + new_n,\n [&](int num) { return num == grid[i][j]; });\n }\n\n );\n }\n\npublic:\n \n Node* construct(vector<vector<int>>& grid) {\n return constQuad(grid, 0, 0, grid.size());\n }\n};", "memory": "21100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "class Solution {\n public:\n Node* construct(vector<vector<int>>& grid) {\n return helper(grid, 0, 0, grid.size());\n }\n\n private:\n Node* helper(const vector<vector<int>>& grid, int i, int j, int w) {\n if (allSame(grid, i, j, w))\n return new Node(grid[i][j] == 1, /*isLeaft=*/true);\n const int half = w / 2;\n return new Node(/*val=*/true, /*isLeaf=*/false,\n /*topLeft=*/helper(grid, i, j, half),\n /*topRight=*/helper(grid, i, j + half, half),\n /*bottomLeft=*/helper(grid, i + half, j, half),\n /*bottomRight=*/helper(grid, i + half, j + half, half));\n }\n\n bool allSame(const vector<vector<int>>& grid, int i, int j, int w) {\n return all_of(grid.begin() + i, grid.begin() + i + w,\n [&](const vector<int>& row) {\n return all_of(row.begin() + j, row.begin() + j + w,\n [&](int num) { return num == grid[i][j]; });\n });\n }\n};", "memory": "21200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "class Solution {\n public:\n Node* construct(vector<vector<int>>& grid) {\n return helper(grid, 0, 0, grid.size());\n }\n\n private:\n Node* helper(const vector<vector<int>>& grid, int i, int j, int w) {\n if (allSame(grid, i, j, w))\n return new Node(grid[i][j] == 1, /*isLeaft=*/true);\n const int half = w / 2;\n return new Node(/*val=*/true, /*isLeaf=*/false,\n /*topLeft=*/helper(grid, i, j, half),\n /*topRight=*/helper(grid, i, j + half, half),\n /*bottomLeft=*/helper(grid, i + half, j, half),\n /*bottomRight=*/helper(grid, i + half, j + half, half));\n }\n\n bool allSame(const vector<vector<int>>& grid, int i, int j, int w) {\n return all_of(grid.begin() + i, grid.begin() + i + w,\n [&](const vector<int>& row) {\n return all_of(row.begin() + j, row.begin() + j + w,\n [&](int num) { return num == grid[i][j]; });\n });\n }\n};", "memory": "21300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n\n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node*\n_bottomLeft, Node* _bottomRight) { val = _val; isLeaf = _isLeaf; topLeft =\n_topLeft; topRight = _topRight; bottomLeft = _bottomLeft; bottomRight =\n_bottomRight;\n }\n};\n*/\n\n// returns {sum,total}\n\n// Can optimize this but let it be like this for now.\nvector<int> getSum(int s1, int e1, int s2, int e2, vector<vector<int>>& grid) {\n int sum = 0, total = (s2 - s1) * (e2 - e1);\n for (int i = s1; i < s2; i++) {\n for (int j = e1; j < e2; j++) {\n sum += grid[i][j];\n }\n }\n return {sum, total};\n}\n\nNode* recurse(int s1, int e1, int s2, int e2, vector<vector<int>>& grid) {\n // This means we have reached a leaf node now.\n if (s1 == s2 && e1 == e2) {\n return new Node(grid[s1][e1] == 1, true);\n }\n vector<int> sum = getSum(s1, e1, s2, e2, grid);\n if (sum[1] == sum[0] || sum[0] == 0) {\n return new Node(sum[0] == sum[1], true);\n }\n\n Node* root = new Node(true, false);\n root->topLeft = recurse(s1, e1, (s1 + s2) / 2, (e1 + e2) / 2, grid);\n root->topRight = recurse(s1, (e1 + e2) / 2, (s1 + s2) / 2, e2, grid);\n root->bottomLeft = recurse((s1 + s2) / 2, e1, s2, (e1 + e2) / 2, grid);\n root->bottomRight = recurse((s1 + s2) / 2, (e1 + e2) / 2, s2, e2, grid);\n return root;\n}\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n return recurse(0, 0, n, n, grid);\n }\n};", "memory": "21500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid[0].size();\n bool val = (bool) grid[0][0];\n bool isLeaf = true;\n \n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if( val != (bool) grid[i][j] )\n {\n isLeaf = false;\n break;\n }\n }\n if(!isLeaf)\n {\n break;\n }\n }\n if(isLeaf)\n {\n Node* res = new Node(val, isLeaf);\n return res;\n }\n else\n {\n vector<vector<int>> sub_grid;\n vector<int> vec(n/2,0);\n for(int i=0; i<n/2; i++)\n {\n sub_grid.push_back(vec);\n }\n int p;\n int q;\n p = 0;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_left = construct(sub_grid);\n p = 0;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_right = construct(sub_grid);\n p = n/2;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_left = construct(sub_grid);\n p = n/2;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_right = construct(sub_grid);\n Node* res = new Node(val, isLeaf, sub_res_top_left, sub_res_top_right, sub_res_bottome_left, sub_res_bottome_right);\n return res;\n }\n }\nprivate:\n void fill_sub_grid(int p, int q, int n, vector<vector<int>>& grid, vector<vector<int>>& sub_grid)\n {\n for(int i=0; i<n; i++)\n {\n std::copy(&grid[p+i][q], &grid[p+i][q+n], &sub_grid[i][0]);\n }\n }\n};", "memory": "21700" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid[0].size();\n bool val = (bool) grid[0][0];\n bool isLeaf = true;\n \n\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if( val != (bool) grid[i][j] )\n {\n isLeaf = false;\n break;\n }\n }\n if(!isLeaf)\n {\n break;\n }\n }\n if(isLeaf)\n {\n Node* res = new Node(val, isLeaf);\n return res;\n }\n else\n {\n vector<vector<int>> sub_grid;\n vector<int> vec(n/2,0);\n for(int i=0; i<n/2; i++)\n {\n sub_grid.push_back(vec);\n }\n int p;\n int q;\n p = 0;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_left = construct(sub_grid);\n p = 0;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_right = construct(sub_grid);\n p = n/2;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_left = construct(sub_grid);\n p = n/2;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_right = construct(sub_grid);\n Node* res = new Node(val, isLeaf, sub_res_top_left, sub_res_top_right, sub_res_bottome_left, sub_res_bottome_right);\n\n return res;\n }\n }\nprivate:\n void fill_sub_grid(int p, int q, int n, vector<vector<int>>& grid, vector<vector<int>>& sub_grid)\n {\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n sub_grid[i][j] = grid[p+i][q+j];\n }\n }\n }\n};", "memory": "21800" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid[0].size();\n bool val = (bool) grid[0][0];\n bool isLeaf = true;\n \n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if( val != (bool) grid[i][j] )\n {\n isLeaf = false;\n break;\n }\n }\n if(!isLeaf)\n {\n break;\n }\n }\n if(isLeaf)\n {\n Node* res = new Node(val, isLeaf);\n return res;\n }\n else\n {\n vector<vector<int>> sub_grid;\n vector<int> vec(n/2,0);\n for(int i=0; i<n/2; i++)\n {\n sub_grid.push_back(vec);\n }\n int p;\n int q;\n p = 0;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_left = construct(sub_grid);\n p = 0;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_right = construct(sub_grid);\n p = n/2;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_left = construct(sub_grid);\n p = n/2;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_right = construct(sub_grid);\n Node* res = new Node(val, isLeaf, sub_res_top_left, sub_res_top_right, sub_res_bottome_left, sub_res_bottome_right);\n return res;\n }\n }\nprivate:\n void fill_sub_grid(int p, int q, int n, vector<vector<int>>& grid, vector<vector<int>>& sub_grid)\n {\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n sub_grid[i][j] = grid[p+i][q+j];\n }\n }\n }\n};", "memory": "21900" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid[0].size();\n bool val = (bool) grid[0][0];\n bool isLeaf = true;\n \n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if( val != (bool) grid[i][j] )\n {\n isLeaf = false;\n break;\n }\n }\n if(!isLeaf)\n {\n break;\n }\n }\n if(isLeaf)\n {\n Node* res = new Node(val, isLeaf);\n return res;\n }\n else\n {\n vector<vector<int>> sub_grid;\n vector<int> vec(n/2,0);\n for(int i=0; i<n/2; i++)\n {\n sub_grid.push_back(vec);\n }\n int p;\n int q;\n p = 0;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_left = construct(sub_grid);\n p = 0;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_right = construct(sub_grid);\n p = n/2;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_left = construct(sub_grid);\n p = n/2;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_right = construct(sub_grid);\n Node* res = new Node(val, isLeaf, sub_res_top_left, sub_res_top_right, sub_res_bottome_left, sub_res_bottome_right);\n return res;\n }\n }\nprivate:\n void fill_sub_grid(int p, int q, int n, vector<vector<int>>& grid, vector<vector<int>>& sub_grid)\n {\n for(int i=0; i<n; i++)\n {\n std::copy(&grid[p+i][q], &grid[p+i][q+n], &sub_grid[i][0]);\n }\n }\n};", "memory": "21900" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid[0].size();\n bool val = (bool) grid[0][0];\n bool isLeaf = true;\n \n\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if( val != (bool) grid[i][j] )\n {\n isLeaf = false;\n break;\n }\n }\n if(!isLeaf)\n {\n break;\n }\n }\n if(isLeaf)\n {\n Node* res = new Node(val, isLeaf);\n return res;\n }\n else\n {\n vector<vector<int>> sub_grid;\n vector<int> vec(n/2,0);\n for(int i=0; i<n/2; i++)\n {\n sub_grid.push_back(vec);\n }\n int p;\n int q;\n p = 0;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_left = construct(sub_grid);\n p = 0;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_right = construct(sub_grid);\n p = n/2;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_left = construct(sub_grid);\n p = n/2;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_right = construct(sub_grid);\n Node* res = new Node(val, isLeaf, sub_res_top_left, sub_res_top_right, sub_res_bottome_left, sub_res_bottome_right);\n\n return res;\n }\n }\nprivate:\n void fill_sub_grid(int p, int q, int n, vector<vector<int>>& grid, vector<vector<int>>& sub_grid)\n {\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n sub_grid[i][j] = grid[p+i][q+j];\n }\n }\n }\n};", "memory": "22000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int n = grid[0].size();\n bool val = (bool) grid[0][0];\n bool isLeaf = true;\n \n\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n if( val != (bool) grid[i][j] )\n {\n isLeaf = false;\n break;\n }\n }\n if(!isLeaf)\n {\n break;\n }\n }\n if(isLeaf)\n {\n Node* res = new Node(val, isLeaf);\n return res;\n }\n else\n {\n vector<vector<int>> sub_grid;\n vector<int> vec(n/2,0);\n for(int i=0; i<n/2; i++)\n {\n sub_grid.push_back(vec);\n }\n int p;\n int q;\n p = 0;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_left = construct(sub_grid);\n p = 0;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_top_right = construct(sub_grid);\n p = n/2;\n q = 0;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_left = construct(sub_grid);\n p = n/2;\n q = n/2;\n fill_sub_grid(p, q, n/2, grid, sub_grid);\n Node* sub_res_bottome_right = construct(sub_grid);\n Node* res = new Node(val, isLeaf, sub_res_top_left, sub_res_top_right, sub_res_bottome_left, sub_res_bottome_right);\n\n return res;\n }\n }\nprivate:\n void fill_sub_grid(int p, int q, int n, vector<vector<int>>& grid, vector<vector<int>>& sub_grid)\n {\n for(int i=0; i<n; i++)\n {\n for(int j=0; j<n; j++)\n {\n sub_grid[i][j] = grid[p+i][q+j];\n }\n }\n }\n};", "memory": "22000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "\n\nclass Solution {\npublic:\n int isvalid( int l , int r , int t , int b , vector<vector<int>> &grid )\n {\n int ele = grid[l][t];\n for( int i = l ; i <=r ; ++i )\n {\n for( int j = t ; j <= b ; ++j )\n {\n if(grid[i][j]!=ele) return -1;\n }\n }\n return ele;\n }\n vector<vector<int>> childs(int l , int r , int t , int b )\n {\n\n int mid1 = (l+r)/2;\n int mid2 = (t+b)/2;\n \n vector<vector<int>> ans = { \n {l,mid1,t,mid2}, // top left \n {l,mid1,mid2+1,b},//top right \n {mid1+1,r,t,mid2},//bottom left \n {mid1+1,r,mid2+1,b} // bottom right \n };\n return ans;\n\n }\n Node* fun(int l , int r , int t , int b , vector<vector<int>> &grid)\n { int ele = isvalid(l,r,t,b,grid);\n if(ele != -1)\n {\n Node *node = new Node(ele,true);\n return node;\n }\n\n Node *node = new Node(1,false);\n \n vector<vector<int>> adj = childs(l,r ,t , b );\n cout<<l<<\" \"<<r<<\" \"<<t<<\" \"<<b<<\" \\n\";\n node->topLeft = fun(adj[0][0],adj[0][1],adj[0][2],adj[0][3],grid);\n node->topRight = fun(adj[1][0],adj[1][1],adj[1][2],adj[1][3],grid);\n node->bottomLeft = fun(adj[2][0],adj[2][1],adj[2][2],adj[2][3],grid);\n node->bottomRight = fun(adj[3][0],adj[3][1],adj[3][2],adj[3][3],grid);\n \n return node;\n }\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size() , m = grid[0].size();\n return fun(0,n-1,0,m-1,grid);\n }\n};", "memory": "22100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\n\n\nclass Solution {\npublic:\n int isvalid( int l , int r , int t , int b , vector<vector<int>> &grid )\n {\n int ele = grid[l][t];\n for( int i = l ; i <=r ; ++i )\n {\n for( int j = t ; j <= b ; ++j )\n {\n if(grid[i][j]!=ele) return -1;\n }\n }\n return ele;\n }\n vector<vector<int>> childs(int l , int r , int t , int b )\n {\n\n int mid1 = (l+r)/2;\n int mid2 = (t+b)/2;\n \n vector<vector<int>> ans = { \n {l,mid1,t,mid2}, // top left \n {l,mid1,mid2+1,b},//top right \n {mid1+1,r,t,mid2},//bottom left \n {mid1+1,r,mid2+1,b} // bottom right \n };\n return ans;\n\n }\n Node* fun(int l , int r , int t , int b , vector<vector<int>> &grid)\n { int ele = isvalid(l,r,t,b,grid);\n if(ele != -1)\n {\n Node *node = new Node(ele,true);\n return node;\n }\n\n Node *node = new Node(1,false);\n \n vector<vector<int>> adj = childs(l,r ,t , b );\n cout<<l<<\" \"<<r<<\" \"<<t<<\" \"<<b<<\" \\n\";\n node->topLeft = fun(adj[0][0],adj[0][1],adj[0][2],adj[0][3],grid);\n node->topRight = fun(adj[1][0],adj[1][1],adj[1][2],adj[1][3],grid);\n node->bottomLeft = fun(adj[2][0],adj[2][1],adj[2][2],adj[2][3],grid);\n node->bottomRight = fun(adj[3][0],adj[3][1],adj[3][2],adj[3][3],grid);\n \n return node;\n }\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size() , m = grid[0].size();\n return fun(0,n-1,0,m-1,grid);\n }\n};", "memory": "22200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n Node* dfs(int i, int j, int size, vector<vector<int>>& grid){\n cout << i << \" \" << j << \" \" << size << endl;\n\n assert(i < grid.size() && j < grid.size());\n\n if(size == 1){\n return new Node(grid[i][j] == 1, true);\n }\n\n //else\n int xx[4] = {0, 0, size/2, size/2};\n int yy[4] = {0, size/2, 0, size/2};\n\n int sum = 0; bool all_leafs = true;\n\n vector<Node*> arr;\n\n for(int k=0;k<4;k++){\n Node* v = dfs(i+xx[k], j+yy[k], size/2, grid);\n\n arr.push_back(v);\n\n if(v->isLeaf){\n sum += (v->val ? 1 : 0);\n }\n else{\n all_leafs = false;\n }\n }\n\n if(all_leafs){\n if(sum == 0 || sum == 4){\n //this is a leaf node\n bool val = (sum == 4) ? true: false;\n\n for(int i=0;i<4;i++){\n delete arr[i];\n }\n\n return new Node(val, true);\n }\n }\n\n return new Node(true, false, arr[0], arr[1], arr[2], arr[3]);\n }\n\n Node* construct(vector<vector<int>>& grid) {\n int len = grid.size();\n \n return dfs(0,0,len, grid);\n }\n};", "memory": "22400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n Node* dfs(int i, int j, int size, vector<vector<int>>& grid){\n cout << i << \" \" << j << \" \" << size << endl;\n\n assert(i < grid.size() && j < grid.size());\n\n if(size == 1){\n return new Node(grid[i][j] == 1, true);\n }\n\n //else\n int xx[4] = {0, 0, size/2, size/2};\n int yy[4] = {0, size/2, 0, size/2};\n\n int sum = 0; bool all_leafs = true;\n\n vector<Node*> arr;\n\n for(int k=0;k<4;k++){\n Node* v = dfs(i+xx[k], j+yy[k], size/2, grid);\n\n arr.push_back(v);\n\n if(v->isLeaf){\n sum += (v->val ? 1 : 0);\n }\n else{\n all_leafs = false;\n }\n }\n\n if(all_leafs){\n if(sum == 0 || sum == 4){\n //this is a leaf node\n bool val = (sum == 4) ? true: false;\n\n for(int i=0;i<4;i++){\n delete arr[i];\n }\n\n return new Node(val, true);\n }\n }\n\n return new Node(true, false, arr[0], arr[1], arr[2], arr[3]);\n }\n\n Node* construct(vector<vector<int>>& grid) {\n int len = grid.size();\n \n return dfs(0,0,len, grid);\n }\n};", "memory": "22400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n return construct(grid, 0, 0, grid.size());\n }\n \n Node* construct(vector<vector<int>>& grid, size_t row, size_t col, size_t n) {\n if (n == 1) {\n return new Node(grid[row][col], true);\n }\n \n n /= 2;\n Node* tl = construct(grid, row, col, n);\n Node* tr = construct(grid, row, col+n, n);\n Node* bl = construct(grid, row+n, col, n);\n Node* br = construct(grid, row+n, col+n, n);\n if (tl->isLeaf && tr->isLeaf && bl->isLeaf && br->isLeaf && tl->val == tr->val && tl->val == bl->val && tl->val == br->val) {\n int val = tl->val;\n return tl;\n }\n \n return new Node(tl->val, false, tl, tr, bl, br);\n }\n};", "memory": "22500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\n bool checkLeaf(Node *topLeft, Node *topRight, Node *bottomLeft, Node *bottomRight) {\n if (topLeft->val == topRight->val\n && topRight->val == bottomLeft->val\n && bottomLeft->val == bottomRight->val\n && topLeft->isLeaf\n && topRight->isLeaf\n && bottomLeft->isLeaf\n && bottomRight->isLeaf)\n return true;\n return false;\n }\n Node* dfs(vector<vector<int>>& grid, int x, int y, int len) {\n if (len == 1) {\n return new Node(grid[x][y] != 0, true);\n }\n int halfLen = len / 2;\n\n Node *topLeft = dfs(grid, x, y, halfLen);\n Node *topRight = dfs(grid, x, y + halfLen, halfLen);\n Node *bottomLeft = dfs(grid, x + halfLen, y, halfLen);\n Node *bottomRight = dfs(grid, x + halfLen, y + halfLen, halfLen);\n\n if (checkLeaf(topLeft, topRight, bottomLeft, bottomRight))\n return new Node(topLeft->val, true);\n return new Node(false, false, topLeft, topRight, bottomLeft, bottomRight);\n }\npublic:\n Node* construct(vector<vector<int>>& grid) {\n return dfs(grid, 0, 0, grid.size());\n }\n};", "memory": "23000" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n Node* buildNode(const std::vector<std::vector<int>>& grid, size_t x0, size_t y0, size_t x, size_t y) {\n if((x0 == x) && (y0 == y)) {\n return new Node(grid[x0][y0], true);\n }\n\n const int diff = (x - x0)/2;\n\n Node* topLeft = buildNode(grid, x0, y0, x0+diff, y0+diff);\n Node* topRight = buildNode(grid, x0, y0+diff+1, x0+diff, y);\n Node* bottomLeft = buildNode(grid, x0+diff+1, y0, x, y0+diff);\n Node* bottomRight = buildNode(grid, x0+diff+1, y0+diff+1, x, y);\n\n const int currentNodeVal = topLeft->val + topRight->val + bottomLeft->val + bottomRight->val;\n const bool isAllLeaves = topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf;\n if(isAllLeaves && ((currentNodeVal == 0) || (currentNodeVal == 4))) {\n delete topLeft;\n delete topRight;\n delete bottomRight;\n\n return bottomLeft;\n }\n\n return new Node(1, false, topLeft, topRight, bottomLeft, bottomRight);\n }\npublic:\n Node* construct(const std::vector<std::vector<int>>& grid) {\n return buildNode(grid, 0, 0, grid.size() - 1, grid.size() - 1);\n }\n};", "memory": "23100" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* constructSub(vector<vector<int>>& grid, int left1, int right1, int left2, int right2)\n {\n \n if(left1 == left2)\n {\n return new Node(grid[left1][right1], true);\n }\n int len = (left2 - left1) / 2;\n Node* node = new Node(0, false);\n node->topLeft = constructSub(grid, left1, right1, left1 + len, right1 + len);\n node->topRight = constructSub(grid, left1, right1 + len+ 1, left1 + len, right2);\n node->bottomLeft = constructSub(grid, left1 + len + 1, right1, left2, right1 + len);\n node->bottomRight = constructSub(grid, left1 + len + 1, right1 + len+ 1, left2, right2);\n\n if(node->topLeft->val == node->topRight->val \n && node->topRight->val == node->bottomLeft->val \n && node->topRight->val == node->bottomRight->val\n && node->topLeft->isLeaf\n && node->topRight->isLeaf\n && node->bottomLeft->isLeaf\n && node->bottomRight->isLeaf)\n {\n node->val = node->topRight->val;\n node->topLeft = nullptr;\n node->topRight = nullptr;\n node->bottomLeft = nullptr;\n node->bottomRight = nullptr;\n node->isLeaf = true;\n }\n\n return node;\n }\n Node* construct(vector<vector<int>>& grid) {\n return constructSub(grid, 0,0, grid.size() -1, grid.size() -1);\n }\n};", "memory": "23200" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* constructSub(vector<vector<int>>& grid, int left1, int right1, int left2, int right2)\n {\n \n if(left1 == left2)\n {\n return new Node(grid[left1][right1], true);\n }\n int len = (left2 - left1) / 2;\n Node* node = new Node(0, false);\n node->topLeft = constructSub(grid, left1, right1, left1 + len, right1 + len);\n node->topRight = constructSub(grid, left1, right1 + len+ 1, left1 + len, right2);\n node->bottomLeft = constructSub(grid, left1 + len + 1, right1, left2, right1 + len);\n node->bottomRight = constructSub(grid, left1 + len + 1, right1 + len+ 1, left2, right2);\n\n if(node->topLeft->val == node->topRight->val \n && node->topRight->val == node->bottomLeft->val \n && node->topRight->val == node->bottomRight->val\n && node->topLeft->isLeaf\n && node->topRight->isLeaf\n && node->bottomLeft->isLeaf\n && node->bottomRight->isLeaf)\n {\n node->val = node->topRight->val;\n node->topLeft = nullptr;\n node->topRight = nullptr;\n node->bottomLeft = nullptr;\n node->bottomRight = nullptr;\n node->isLeaf = true;\n }\n\n cout << left1 << \" \" << right1 << \" \" << left2<< \" \" << right2 << \" \" << node->val << \"l\" << node->isLeaf << endl;\n return node;\n }\n Node* construct(vector<vector<int>>& grid) {\n return constructSub(grid, 0,0, grid.size() -1, grid.size() -1);\n }\n};", "memory": "23300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
2
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n\n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n\n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node*\n_bottomLeft, Node* _bottomRight) { val = _val; isLeaf = _isLeaf; topLeft =\n_topLeft; topRight = _topRight; bottomLeft = _bottomLeft; bottomRight =\n_bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n using Grid = vector<vector<int>>;\n Node* helper(const Grid& grid, int rStart, int rEnd, int cStart, int cEnd) {\n if (rEnd == rStart) {\n return new Node(grid[rStart][cStart], true);\n }\n Node* topLeft = helper(grid, rStart, (rStart + rEnd) / 2, cStart,\n (cStart + cEnd) / 2);\n Node* topRight = helper(grid, rStart, (rStart + rEnd) / 2,\n (cStart + cEnd) / 2 + 1, cEnd);\n Node* bottomLeft = helper(grid, (rStart + rEnd) / 2 + 1, rEnd, cStart,\n (cStart + cEnd) / 2);\n Node* bottomRight = helper(grid, (rStart + rEnd) / 2 + 1, rEnd,\n (cStart + cEnd) / 2 + 1, cEnd);\n if (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf &&\n bottomRight->isLeaf && topLeft->val == topRight->val &&\n topLeft->val == bottomLeft->val &&\n topLeft->val == bottomRight->val) {\n return new Node(topLeft->val, true);\n } else {\n return new Node(topLeft->val, false, topLeft, topRight, bottomLeft,\n bottomRight);\n }\n }\n\npublic:\n Node* construct(vector<vector<int>>& grid) {\n int len = grid.size();\n return helper(grid, 0, len - 1, 0, len - 1);\n }\n};", "memory": "23300" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n\n // it is simple recursion...\n\n // divide grid into 4 parts based on size..\n // get Nodes corresponding to each part\n // if value of all 4 parts are same And are leafs, you have a leaf,\n // else you have a non-leaf\n // base condition for recursion is the grid of size 1 \n Node* traverseAndCreate(vector<vector<int>>& grid, int startRow, int startCol, int size) {\n if (size <= 1) {\n if (grid[startRow][startCol] == 1) {\n Node *ret = new Node(true, true);\n return ret;\n } else {\n return new Node(false, true);\n }\n }\n\n Node* topLeft = traverseAndCreate(grid, startRow, startCol, size/2);\n Node* topRight = traverseAndCreate(grid, startRow, startCol + (size/2), size/2);\n Node* bottomLeft = traverseAndCreate(grid, startRow + (size/2), startCol, size/2);\n Node* bottomRight = traverseAndCreate(grid, startRow + (size/2), startCol + (size/2), size/2);\n\n if (\n topLeft -> isLeaf == true &&\n topRight -> isLeaf == true &&\n bottomLeft -> isLeaf == true &&\n bottomRight -> isLeaf == true &&\n topLeft -> val == topRight -> val && \n topLeft -> val == bottomLeft -> val &&\n topLeft -> val == bottomRight -> val\n ) {\n return new Node(topLeft -> val, true);\n } else {\n return new Node(false, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n\n return new Node();\n\n }\n\n Node* construct(vector<vector<int>>& grid) {\n return traverseAndCreate(grid, 0, 0, grid.size());\n }\n};", "memory": "23400" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\nvector<vector<int>> G;\n\n\nNode* fun(int i,int j,int n){\n if(n==1) return new Node(G[i][j],1);\n Node* topLeft = fun(i,j,n/2);\n Node* topRight = fun(i,j+n/2,n/2);\n Node* bottomLeft = fun(i+n/2,j,n/2);\n Node* bottomRight = fun(i+n/2,j+n/2,n/2);\n bool val = 0;\n bool leaf = 0;\n // cout << i << \" \" << j<<\" \"<<n <<endl;\n if(topLeft->isLeaf&&topRight->isLeaf&&bottomLeft->isLeaf&&bottomRight->isLeaf){\n bool a = topLeft->val, b = topRight->val,c=bottomLeft->val,d=bottomRight->val;\n // cout << a << \" \" <<b <<\" \"<<c << \" \"<<d<<endl;\n if(a==b&&b==c&&c==d){\n leaf=1;\n val = a;\n }\n }\n if(leaf) return new Node(val,leaf);\n return new Node(val,leaf,topLeft,topRight,bottomLeft,bottomRight);\n}\n\nclass Solution {\npublic:\n Node* construct(vector<vector<int>>& grid) {\n G=grid;\n int n = G.size();\n return fun(0,0,n);\n }\n};", "memory": "23500" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\npublic:\n Node* rec(const vector<vector<int>> &grid, int i1, int i2, int j1, int j2) {\n if (i1 + 1 == i2 && j1 + 1 == j2) {\n return new Node(grid[i1][j1], true);\n }\n int imid = (i1 + i2) / 2;\n int jmid = (j1 + j2) / 2;\n Node* a = rec(grid, i1, imid, j1, jmid);\n Node* b = rec(grid, i1, imid, jmid, j2);\n Node* c = rec(grid, imid, i2, j1, jmid);\n Node* d = rec(grid, imid, i2, jmid, j2);\n if (a->isLeaf && b->isLeaf && c->isLeaf && d->isLeaf && (a->val == b->val) && (a->val == c->val) && (a->val == d->val)) {\n int val = a->val;\n delete a, b, c, d;\n return new Node(val, true);\n }\n return new Node(0, false, a, b, c, d);\n }\n Node* construct(vector<vector<int>>& grid) {\n int n = grid.size();\n return rec(grid, 0, n, 0, n);\n }\n};", "memory": "23600" }
772
<p>Given a <code>n * n</code> matrix <code>grid</code> of <code>0&#39;s</code> and <code>1&#39;s</code> only. We want to represent <code>grid</code> with a Quad-Tree.</p> <p>Return <em>the root of the Quad-Tree representing </em><code>grid</code>.</p> <p>A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:</p> <ul> <li><code>val</code>: True if the node represents a grid of 1&#39;s or False if the node represents a grid of 0&#39;s. Notice that you can assign the <code>val</code> to True or False when <code>isLeaf</code> is False, and both are accepted in the answer.</li> <li><code>isLeaf</code>: True if the node is a leaf node on the tree or False if the node has four children.</li> </ul> <pre> class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; }</pre> <p>We can construct a Quad-Tree from a two-dimensional area using the following steps:</p> <ol> <li>If the current grid has the same value (i.e all <code>1&#39;s</code> or all <code>0&#39;s</code>) set <code>isLeaf</code> True and set <code>val</code> to the value of the grid and set the four children to Null and stop.</li> <li>If the current grid has different values, set <code>isLeaf</code> to False and set <code>val</code> to any value and divide the current grid into four sub-grids as shown in the photo.</li> <li>Recurse for each of the children with the proper sub-grid.</li> </ol> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/new_top.png" style="width: 777px; height: 181px;" /> <p>If you want to know more about the Quad-Tree, you can refer to the <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a>.</p> <p><strong>Quad-Tree format:</strong></p> <p>You don&#39;t need to read this section for solving the problem. This is only if you want to understand the output format here. The output represents the serialized format of a Quad-Tree using level order traversal, where <code>null</code> signifies a path terminator where no node exists below.</p> <p>It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list <code>[isLeaf, val]</code>.</p> <p>If the value of <code>isLeaf</code> or <code>val</code> is True we represent it as <strong>1</strong> in the list <code>[isLeaf, val]</code> and if the value of <code>isLeaf</code> or <code>val</code> is False we represent it as <strong>0</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/02/11/grid1.png" style="width: 777px; height: 99px;" /> <pre> <strong>Input:</strong> grid = [[0,1],[1,0]] <strong>Output:</strong> [[0,1],[1,0],[1,1],[1,1],[1,0]] <strong>Explanation:</strong> The explanation of this example is shown below: Notice that 0 represents False and 1 represents True in the photo representing the Quad-Tree. <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e1tree.png" style="width: 777px; height: 186px;" /> </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2mat.png" style="width: 777px; height: 343px;" /></p> <pre> <strong>Input:</strong> grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]] <strong>Output:</strong> [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] <strong>Explanation:</strong> All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft and bottomRight each has the same value. The topRight have different values so we divide it into 4 sub-grids where each has the same value. Explanation is shown in the photo below: <img alt="" src="https://assets.leetcode.com/uploads/2020/02/12/e2tree.png" style="width: 777px; height: 328px;" /> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>n == 2<sup>x</sup></code> where <code>0 &lt;= x &lt;= 6</code></li> </ul>
3
{ "code": "/*\n// Definition for a QuadTree node.\nclass Node {\npublic:\n bool val;\n bool isLeaf;\n Node* topLeft;\n Node* topRight;\n Node* bottomLeft;\n Node* bottomRight;\n \n Node() {\n val = false;\n isLeaf = false;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = NULL;\n topRight = NULL;\n bottomLeft = NULL;\n bottomRight = NULL;\n }\n \n Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {\n val = _val;\n isLeaf = _isLeaf;\n topLeft = _topLeft;\n topRight = _topRight;\n bottomLeft = _bottomLeft;\n bottomRight = _bottomRight;\n }\n};\n*/\n\nclass Solution {\nprivate:\n vector<vector<int>> grid;\n Node* retTree(int startx, int starty, int length){\n if(length == 1) return new Node(grid[startx][starty], true);\n else{\n int nxLength = length/2, endx = startx + nxLength, endy = starty + nxLength;\n Node *topLeft = retTree(startx, starty, nxLength), *topRight = retTree(startx, endy, nxLength), *bottomLeft = retTree(endx, starty, nxLength), *bottomRight = retTree(endx, endy, nxLength);\n int tlv = topLeft->val, trv = topRight->val, blv = bottomLeft->val, brv = bottomRight->val;\n bool allLeaves = (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf);\n if(tlv == trv && blv == brv && tlv == blv && allLeaves) return new Node(tlv, true);\n else return new Node(tlv, false, topLeft, topRight, bottomLeft, bottomRight);\n }\n }\n\npublic:\n Node* construct(vector<vector<int>>& _grid) {\n grid = vector<vector<int>> (_grid);\n return retTree(0, 0, grid.size());\n }\n};", "memory": "23700" }