id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n // long long table[1002][1002];\n /*int calc(string &s, string &t, int i, int j){\n if(j==t.length()){\n return 1;\n }\n if( i>=s.length() ){\n return 0;\n }\n if(table[i][j]!=-1){\n return table[i][j];\n }\n int op1=0,op2=0;\n\n op1 = calc(s,t,i+1,j);\n if(s[i]==t[j]){\n op2 = calc(s,t,i+1,j+1);\n }\n return table[i][j] = op1+op2;\n }*/\n // dp[i][j] = (if eq) (dp[i+1][j+1]) + dp[i+1][j];\npublic:\n int numDistinct(string s, string t) {\n\n // for(int i=0;i<1002;i++){\n // for(int j=0;j<1002;j++){\n // table[i][j]=0;\n // }\n // }\n vector<vector<unsigned int>> table(1002,vector<unsigned int>(1002,0));\n for(int i=0;i<=s.length();i++){\n table[i][t.length()]=1;\n }\n\n\n // for(int i=0;i<t.length();i++){\n // if( s[s.length()-1] == t[i])\n // {\n // cout<<\"a\";\n // table[s.length()-1][i] = 1;\n // }\n // else\n // table[s.length()-1][i] = 0;\n // }\n for(int i=s.length()-1;i>=0;i--){\n for(int j=t.length()-1;j>=0;j--){\n if(s[i] == t[j]){\n table[i][j] = table[i+1][j+1];\n }\n table[i][j] += table[i+1][j];\n \n }\n }\n \n // for(int i=0;i<s.length()+1;i++){\n // for(int j=0;j<t.length()+1;j++){\n // cout<<\" \" <<table[i][j];\n // }\n // cout<<endl;\n // }\n\n return table[0][0];\n \n }\n};",
"memory": "201756"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>>dp;\n int f(string&s,string&t,int i,int j){\n if(i>=s.size()&&j>=t.size())return 1;\n if(j>=t.size()&&i<s.size())return 1;\n if(i>=s.size()&&j<t.size())return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n if(s[i]==t[j]){\n //matched\n //either picking this matched character from s\n //or lets find same character occurrence in further string\n //hence taking both possibilities\n return dp[i][j]= f(s,t,i+1,j+1)+f(s,t,i+1,j);\n }else{\n //not matched look futher for same character in s\n return dp[i][j]=f(s,t,i+1,j);\n }\n }\n int numDistinct(string s, string t) {\n dp.clear();\n dp.resize(1004,vector<int>(1004,-1));\n return f(s,t,0,0);\n }\n};",
"memory": "206933"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int f(int i, int j, string &s, string &t, vector<vector<int>> &dp)\n {\n int n = s.size(), m = t.size();\n if(j == m) return 1;\n if(i == n) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n\n int ans = f(i + 1, j, s, t, dp);\n if(s[i] == t[j]) ans += f(i + 1, j + 1, s ,t, dp);\n\n return dp[i][j] = ans;\n }\n\n int numDistinct(string s, string t) {\n vector<vector<int>> dp(1005, vector<int> (1005, -1));\n return f(0,0,s,t, dp);\n }\n};",
"memory": "212111"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>>dp;//as both index is changing only\n//traditional take and not_take ka hi problem hai\n//isme basically hame na s[i]==t[i] tbhi na isko lekr age bdhnge\nint helper(int s_index,int t_index,string &s,string &t)\n{\n if(t_index>=t.size()) return 1;//ye tbhi hi hga jab pura ek koi mil gya hga\n if(s_index>=s.size()) return 0;\n if(dp[s_index][t_index]!=-1) return dp[s_index][t_index];\n long long take=0;\n if(s[s_index]==t[t_index])\n {\n take=helper(s_index+1,t_index+1,s,t);\n }\n long long not_take=helper(s_index+1,t_index,s,t);//jab s_index wala liye hi ny hai to t_index me kyu bdhaye bhai ham jab tak equal nhi ho jata\n //at last jo take and not_take se ayega vo mera nanswer\n return dp[s_index][t_index]=take+not_take;\n}\n int numDistinct(string s, string t) {\n dp.resize(1005,vector<int>(1005,-1));\n return helper(0,0,s,t);\n }\n};",
"memory": "217288"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<vector<int>>dp;//as both index is changing only\n//traditional take and not_take ka hi problem hai\n//isme basically hame na s[i]==t[i] tbhi na isko lekr age bdhnge\nint helper(int s_index,int t_index,string &s,string &t)\n{\n if(t_index>=t.size()) return 1;//ye tbhi hi hga jab pura ek koi mil gya hga\n if(s_index>=s.size()) return 0;\n if(dp[s_index][t_index]!=-1) return dp[s_index][t_index];\n long long take=0;\n if(s[s_index]==t[t_index])\n {\n take=helper(s_index+1,t_index+1,s,t);\n }\n long long not_take=helper(s_index+1,t_index,s,t);//jab s_index wala liye hi ny hai to t_index me kyu bdhaye bhai ham jab tak equal nhi ho jata\n //at last jo take and not_take se ayega vo mera nanswer\n return dp[s_index][t_index]=take+not_take;\n}\n int numDistinct(string s, string t) {\n dp.resize(1005,vector<int>(1005,-1));\n return helper(0,0,s,t);\n }\n};",
"memory": "222466"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n // long long table[1002][1002];\n /*int calc(string &s, string &t, int i, int j){\n if(j==t.length()){\n return 1;\n }\n if( i>=s.length() ){\n return 0;\n }\n if(table[i][j]!=-1){\n return table[i][j];\n }\n int op1=0,op2=0;\n\n op1 = calc(s,t,i+1,j);\n if(s[i]==t[j]){\n op2 = calc(s,t,i+1,j+1);\n }\n return table[i][j] = op1+op2;\n }*/\n // dp[i][j] = (if eq) (dp[i+1][j+1]) + dp[i+1][j];\npublic:\n int numDistinct(string s, string t) {\n\n // for(int i=0;i<1002;i++){\n // for(int j=0;j<1002;j++){\n // table[i][j]=0;\n // }\n // }\n vector<vector<unsigned long long>> table(1002,vector<unsigned long long>(1002,0));\n for(int i=0;i<=s.length();i++){\n table[i][t.length()]=1;\n }\n\n\n // for(int i=0;i<t.length();i++){\n // if( s[s.length()-1] == t[i])\n // {\n // cout<<\"a\";\n // table[s.length()-1][i] = 1;\n // }\n // else\n // table[s.length()-1][i] = 0;\n // }\n for(int i=s.length()-1;i>=0;i--){\n for(int j=t.length()-1;j>=0;j--){\n if(s[i] == t[j]){\n table[i][j] = table[i+1][j+1];\n }\n table[i][j] += table[i+1][j];\n \n }\n }\n \n // for(int i=0;i<s.length()+1;i++){\n // for(int j=0;j<t.length()+1;j++){\n // cout<<\" \" <<table[i][j];\n // }\n // cout<<endl;\n // }\n\n return table[0][0];\n \n }\n};",
"memory": "227643"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n vector<vector<unsigned long long>> dp(1001, vector<unsigned long long>(1001, 0));\n for(int i = 0; i < s.length(); i++) {\n dp[0][i] = 1;\n }\n\n for(int i = 1; i <= t.length(); i++) {\n for(int j = 1; j <= s.length(); j++) {\n dp[i][j] = dp[i][j - 1];\n if(s[j - 1] == t[i - 1]) {\n dp[i][j] += dp[i - 1][j - 1];\n }\n }\n }\n return dp[t.length()][s.length()];\n }\n};",
"memory": "232821"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n return dp(s, t, 0, 0);\n }\n\n int dp(string s, string t, int i, int j) {\n if(j == t.length()) return 1;\n if(s.length() - i < t.length() - j) return 0;\n if(memo.contains(pair{i, j})) return memo[pair{i, j}];\n // from s\n memo[pair{i, j}] = 0;\n for(int k = i; k < s.length(); k++) {\n if(s[k] == t[j]){\n memo[pair{i,j}] += dp(s, t, k + 1, j + 1);\n }\n }\n return memo[pair{i,j}];\n }\nprivate:\n map<pair<int, int>, int> memo;\n};",
"memory": "237998"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int getDistSubs(int nIndex, int nSize, int nOffset, string& sBase, string& sTarget, string sCurrent, map<string, map<int,int>>&dp) {\n\t\tif (nSize == nIndex) {\n\t\t\tif (0 == sCurrent.compare(sTarget)) {\n\t\t\t\treturn dp[sCurrent][nOffset] = 1;\n\t\t\t}\n\t\t}\n\t\tmap<string, map<int,int>>::iterator _it = dp.find(sCurrent);\n\t\tif (dp.end() != _it) {\n\t\t\tmap<int, int>::iterator _it1 = _it->second.find(nOffset);\n\t\t\tif (_it->second.end() != _it1) {\n\t\t\t\treturn _it1->second;\n\t\t\t}\n\t\t}\n\t\tint nLength = sBase.length();\n\t\tint nCount = 0;\n\t\tfor (int i = nOffset; i < nLength; i++) {\n\t\t\tif (sTarget[nIndex] != sBase[i]) continue;\n if (nLength - i < nSize - nOffset) continue;\n\t\t\tnCount += getDistSubs(nIndex + 1, nSize, i + 1, sBase, sTarget, sCurrent + sBase[i], dp);\n\t\t}\n\t\treturn dp[sCurrent][nOffset] = nCount;\n\t}\n\tint numDistinct(string s, string t) {\n unordered_map<char,int> stat;\n int nSize = t.size();\n for (int i = 0; i < nSize; i++) {\n stat[t[i]]++;\n }\n nSize = s.size();\n for (int i = 0; i < nSize; i++) {\n stat[s[i]]--;\n }\n unordered_map<char,int>::iterator _it = stat.begin();\n while (_it != stat.end()) {\n if (_it->second > 0) return 0;\n _it++;\n }\n\t\tstring sCurrent;\n\t\tmap<string, map<int,int>> dp;\n\t\tgetDistSubs(0, t.length(), 0, s, t, sCurrent, dp);\n\t\treturn dp[sCurrent].begin()->second;\n\t}\n};",
"memory": "243176"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int ans = 0;\n int dp[1009][1009];\n int dfs(int p, int q, string s, string t){\n if(dp[p][q] != -1) return dp[p][q];\n if(s.length()-p < t.length()-q) return 0;\n int sum = 0;\n if(q == t.length()) return 1;\n for(int i = p; i < s.length(); i++){\n if(s[i] == t[q]){\n sum += dfs(i+1, q+1, s, t);\n }\n }\n dp[p][q] = sum;\n return sum;\n }\n int numDistinct(string s, string t) {\n memset(dp, -1, sizeof(dp));\n return dfs(0, 0, s, t);\n\n }\n};",
"memory": "248353"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<vector<int>> dp;\n\npublic:\n int numDistinct(string s, string t) {\n dp.assign(1001, vector<int> (1001, -1));\n return count(s, t);\n }\n int count(string s, string t) {\n int i = s.size(), j = t.size();\n if(j == 0) return 1;\n if(i == 0 || i < j) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n return dp[i][j] = count(s.substr(1), t) + ((s[0] == t[0]) ? count(s.substr(1), t.substr(1)) : 0);\n }\n};",
"memory": "253531"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int dp[1001][1001];\n int solve(string s , string t , int index1 , int index2)\n {\n if(index2 >= t.size())\n {\n return 1;\n }\n else if(index1 >= s.size())\n {\n return 0;\n }\n\n if(dp[index1][index2] != -1)return dp[index1][index2];\n int take=0;\n if(s[index1] == t[index2])\n {\n take = solve(s , t , index1+1 ,index2+1);\n \n }\n int not_take = solve(s , t , index1+1 , index2);\n\n return dp[index1][index2] = take+not_take;\n }\n int numDistinct(string s, string t) {\n memset(dp , -1 , sizeof(dp));\n return solve(s , t , 0 , 0);\n }\n};",
"memory": "258708"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n int util(string s, string t, int si, int ti) {\n if(ti == t.size()) {\n return 1;\n }\n if(si >= s.size()) {\n return 0;\n }\n if(dp[si][ti] != -1) {\n return dp[si][ti];\n }\n int ans = 0;\n if(s[si] == t[ti]) {\n ans += util(s, t, si+1, ti+1);\n }\n ans += util(s, t, si+1, ti);\n return dp[si][ti] = ans;\n }\npublic:\n int dp[1000][1000];\n int numDistinct(string s, string t) {\n // dp = vector<vector<int>>(s.size(), vector<int>(t.size(), -1));\n memset(dp, -1, sizeof(dp));\n return util(s, t, 0, 0);\n }\n};\n",
"memory": "263886"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int func(int i,int j,string s, string t){\n int n=s.length(),m=t.length();\n if(j==m)return 1;\n else if(i==n)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int x=0,y=0;\n if(s[i]==t[j]){\n x=func(i+1,j+1,s,t);\n }\n y=func(i+1,j,s,t);\n return dp[i][j]=x+y;\n }\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof dp);\n return func(0,0,s,t);\n }\n};",
"memory": "269063"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int solve(int i,int j,string s,string t){\n if(j==t.size())return 1;\n if(i>=s.size())return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int take=0,ntake=0;\n if(s[i]==t[j]){\n take = solve(i+1,j+1,s,t);\n }\n ntake = solve(i+1,j,s,t);\n return dp[i][j] = (take+ntake);\n }\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof(dp));\n return solve(0,0,s,t);\n }\n};",
"memory": "274241"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int dp[1005][1005];\n\n int find(int s_index,int t_index,string s, string t){\n\n if(t_index == t.size()) return 1;\n if(s_index == s.size()) return 0;\n\n if(dp[s_index][t_index] != -1) return dp[s_index][t_index];\n int count = 0;\n\n if(s[s_index] == t[t_index])\n count += find(s_index+1 , t_index+1,s,t);\n count += find(s_index+1,t_index,s,t);\n return dp[s_index][t_index] = count;\n }\n\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof(dp));\n return find(0,0,s,t);\n }\n};",
"memory": "279418"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int cnt; \n int dp[1003][1003];\n int get(string s, string t, int i, int j){\n if(j>t.size()){\n return 1;\n }\n\n if(i>s.size()){\n return 0;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n\n int res=0;\n\n if(s[i] == t[j]){\n res+=get(s, t, i+1, j+1);\n }\n\n res+=get(s, t, i+1, j);\n\n return dp[i][j] = res;\n }\n int numDistinct(string s, string t) {\n cnt = 0;\n memset(dp, -1, sizeof(dp));\n return get(s, t, 0, 0);\n\n }\n};",
"memory": "284596"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1002][1002];\n //i => s and j => t\n int f(int i, int j, string s, string t)\n {\n if(j < 0) return 1;\n if(i < 0) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n int take = 0, nottake = 0;\n if(s[i] == t[j]) take = f(i - 1, j - 1, s, t);\n nottake = f(i - 1, j, s, t);\n return dp[i][j] = take + nottake;\n }\n int numDistinct(string s, string t) {\n memset(dp, -1, sizeof(dp));\n int m = s.size(), n = t.size();\n return f(m - 1, n - 1, s, t);\n }\n};",
"memory": "289773"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint dp[1002][1002];\n int fun(int ind1,int ind2,string s,string t){\n if(ind2<0) return 1;\n if(ind1<0) return 0;\n\n\n if(dp[ind1][ind2]!=-1) return dp[ind1][ind2];\n int ans=0;\n if(s[ind1]==t[ind2]){\n ans+=fun(ind1-1,ind2-1,s,t);\n }\n ans+=fun(ind1-1,ind2,s,t);\n // ans+=fun(ind1,ind2-1,s,t);\n\n return dp[ind1][ind2]=ans;\n }\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof(dp));\n return fun(s.size()-1,t.size()-1,s,t);\n }\n};",
"memory": "294951"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int Solve(string &s,string t,int i,int j,vector<vector<int>>&dp){\n if(j==t.size()){\n return 1;\n }\n if(i==s.size()){//already handles j==t.size() from above,so no need to handle seperately\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int ans = 0;\n if(s[i]==t[j]){\n ans += Solve(s,t,i+1,j+1,dp);\n }\n ans += Solve(s,t,i+1,j,dp);\n dp[i][j]=ans;\n return ans;\n }\n int numDistinct(string s, string t) {\n vector<vector<int>>dp(s.size(),vector<int>(t.size(),-1));\n int ans = Solve(s,t,0,0,dp);\n return ans;\n }\n};",
"memory": "300128"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int mod=INT_MAX;\n int solve(string s,string t,int n,int m){\n if(m==0) return 1;\n if(n==0) return 0;\n if(dp[n][m]!=-1) return dp[n][m];\n int sum=0;\n if(s[n-1]==t[m-1]){\n sum=(sum%mod+solve(s,t,n-1,m-1)%mod)%mod;\n }\n sum=(sum%mod+solve(s,t,n-1,m)%mod)%mod;\n return dp[n][m]=sum%mod;\n }\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof(dp)); \n return solve(s,t,s.size(),t.size())%mod;\n }\n};",
"memory": "300128"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int Solve(string &s,string t,int i,int j,vector<vector<int>>&dp){\n if(j==t.size()){\n return 1;\n }\n if(i==s.size()){//already handles j==t.size() from above,so no need to handle seperately\n return 0;\n }\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n int ans = 0;\n if(s[i]==t[j]){\n ans += Solve(s,t,i+1,j+1,dp);\n }\n ans += Solve(s,t,i+1,j,dp);\n dp[i][j]=ans;\n return ans;\n }\n int numDistinct(string s, string t) {\n vector<vector<int>>dp(s.size(),vector<int>(t.size(),-1));\n int ans = Solve(s,t,0,0,dp);\n return ans;\n }\n};",
"memory": "305306"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint dp[1001][1001];\nint func(int i1, int i2, string s1 , string s2){\n if(i2<0)return 1;\n if(i1<0)return 0;\n if(dp[i1][i2]!=-1)return dp[i1][i2];\n int ways=0;\n if(s1[i1]==s2[i2]){ways= func(i1-1, i2-1, s1, s2);}\n ways+= func(i1-1, i2, s1, s2);\n return dp[i1][i2]=ways;\n}\n int numDistinct(string s, string t) {\n memset(dp, -1, sizeof dp);\n int n=s.size(); int m=t.size();\n return func(n,m, s,t);\n }\n};",
"memory": "310483"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int helper(string s, string t, int i, int j, vector<vector<int>>&dp) {\n int N = s.size(), M = t.size();\n if(j >= M) {\n if(j == M) {\n return 1;\n }\n return 0;\n }\n if(i >= N) {\n return 0;\n }\n if(dp[i][j] != -1) {\n return dp[i][j];\n }\n int ans = 0;\n if(s[i] == t[j]) {\n ans += helper(s, t, i+1, j+1, dp);\n }\n ans += helper(s, t, i+1, j, dp);\n dp[i][j] = ans;\n return ans;\n }\n\n int numDistinct(string s, string t) {\n if(t.size() > s.size()) {\n return 0;\n }\n if(t.size() == s.size()) {\n if(t == s) {\n return 1;\n }\n return 0;\n }\n vector<vector<int>>dp(s.size(), vector<int>(t.size(), -1));\n return helper(s, t, 0, 0, dp);\n }\n};",
"memory": "315661"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int solve(int i,int j,string s, string t,vector<vector<int>> &dp){\n if(j<0) return 1;\n if(i<0) return 0;\n\n if(dp[i][j]!=-1) return dp[i][j];\n\n int first=0,second=0;\n\n if(s[i]==t[j]){\n first = solve(i-1,j-1,s,t,dp);\n }\n second = solve(i-1,j,s,t,dp);\n return dp[i][j]=first + second;\n }\npublic:\n int numDistinct(string s, string t) {\n int n = s.length(), m =t.length();\n if(m>n || (m==n && s!=t)) return 0;\n vector<vector<int>> dp(n,vector<int>(m,-1));\n return solve(n-1,m-1,s,t,dp);\n }\n};",
"memory": "320838"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> dp;\n int solve(string& s, string t, int i, int j) {\n if (j >= t.size()) { \n return 1;\n }\n\n if (i >= s.size()) {\n return 0;\n }\n\n if (dp[i][j] != -1) return dp[i][j];\n \n int result = solve(s, t, i + 1, j);\n if (s[i] == t[j]) result += solve(s, t, i + 1, j + 1);\n\n return dp[i][j] = result;\n }\n\n int numDistinct(string s, string t) {\n dp.resize(s.size(), vector<int>(t.size(), -1));\n return solve(s, t, 0, 0);\n }\n};",
"memory": "326016"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<vector<int>> distinctSubsequenceCountCache;\n\npublic:\n int numDistinct(string s, string t) {\n distinctSubsequenceCountCache =\n vector<vector<int>>(s.size(), vector<int>(t.size(), -1));\n return getDistinctSubsequenceCount(s, 0, t, 0);\n }\n\n int getDistinctSubsequenceCount(string s, int i, string t, int j) {\n if (j == t.size()) {\n return 1;\n }\n\n if (s.size() - i < t.size() - j) {\n return 0;\n }\n\n if (distinctSubsequenceCountCache[i][j] != -1) {\n return distinctSubsequenceCountCache[i][j];\n }\n\n int distinctSubsequenceCount = 0;\n for(int k = i; k < s.size(); k++){\n if(s[k] == t[j]){\n distinctSubsequenceCount += \n getDistinctSubsequenceCount(s, k + 1, t, j + 1);\n }\n }\n distinctSubsequenceCountCache[i][j] = distinctSubsequenceCount;\n return distinctSubsequenceCountCache[i][j];\n }\n};",
"memory": "331193"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int result = 0;\n unordered_map<char, vector<int>> c_2_idx;\n vector<vector<int>> memo;\n int numDistinct(string s, string t) {\n for(int i = 0; i < s.length(); i++){\n c_2_idx[s[i]].push_back(i);\n }\n memo = vector<vector<int>>(t.length(), vector<int>(s.length(), -1));\n\n return dp(s, t, -1, 0);\n }\n\n int dp(string s, string t, int s_idx, int t_idx){\n if(t_idx == t.length()){\n return 1;\n }\n if(s.length()-s_idx<t.length()-t_idx){\n return 0;\n }\n if(s_idx>=0){\n if(memo[t_idx][s_idx]!=-1){\n return memo[t_idx][s_idx];\n }\n }\n char t_c = t[t_idx];\n int res = 0;\n for(int s_i: c_2_idx[t_c]){\n if(s_i>s_idx){\n res+=dp(s, t, s_i, t_idx+1);\n }\n }\n if(s_idx!=-1){\n memo[t_idx][s_idx] = res;\n }\n return res;\n }\n};",
"memory": "336371"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n vector<vector<int>>distinctSubsequenceCountCache;\npublic:\n int numDistinct(string s, string t) {\n this->distinctSubsequenceCountCache = vector<vector<int>>(s.size(), vector<int>(t.size(), -1));\n return getDistinctSubsequence(s, 0, t, 0);\n }\n\n int getDistinctSubsequence(string str, int strIndex, string subStr, int subStrIndex){\n if(subStrIndex == subStr.length()){\n return 1;\n }\n\n if(str.size() - strIndex < subStr.size() - subStrIndex){\n return 0;\n }\n\n if(distinctSubsequenceCountCache[strIndex][subStrIndex] != -1){\n return distinctSubsequenceCountCache[strIndex][subStrIndex];\n }\n int distinctSubsequenceCount = 0;\n for(int i=strIndex;i<str.length();i++){\n if(str[i] == subStr[subStrIndex]){\n distinctSubsequenceCount += getDistinctSubsequence(str, i+1, subStr, subStrIndex+1);\n }\n }\n distinctSubsequenceCountCache[strIndex][subStrIndex] = distinctSubsequenceCount;\n return distinctSubsequenceCountCache[strIndex][subStrIndex];\n }\n};",
"memory": "341548"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n if(s.size()<t.size())return 0;\n vector<vector<int>> mem(s.size(),vector<int>(t.size(),-1));\n return solve(s,t,0,0,mem);\n }\n int solve(string s, string t, int s_begin, int t_begin, vector<vector<int>>& mem){\n //if(mem[s_begin][t_begin]!=-1)return mem[s_begin][t_begin];\n if(s.size()-s_begin<t.size()-t_begin){\n mem[s_begin][t_begin]=0;\n return 0;\n }\n int count=0;\n if(t.size()-t_begin==1){\n for(int k=s_begin;k<s.size();k++){\n if(mem[k][t_begin]!=-1){\n count+=mem[k][t_begin];\n break;\n }\n if(s[k]==t[t_begin])count++;\n }\n mem[s_begin][t_begin]=count;\n }\n else{\n for(int i=s_begin;i<s.size()-1;i++){\n if(s[i]==t[t_begin]){\n if(mem[i+1][t_begin+1]==-1)mem[i+1][t_begin+1]=solve(s,t,i+1,t_begin+1,mem);\n count+=mem[i+1][t_begin+1];\n }\n else{\n if(mem[i+1][t_begin]==-1)mem[i+1][t_begin]=solve(s,t,i+1,t_begin,mem);\n }\n }\n mem[s_begin][t_begin]=count;\n }\n return count;\n }\n};",
"memory": "346726"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n if(s.size()<t.size())return 0;\n vector<vector<int>> mem(s.size(),vector<int>(t.size(),-1));\n return solve(s,t,0,0,mem);\n }\n int solve(string s, string t, int s_begin, int t_begin, vector<vector<int>>& mem){\n if(mem[s_begin][t_begin]!=-1)return mem[s_begin][t_begin];\n if(s.size()-s_begin<t.size()-t_begin){\n mem[s_begin][t_begin]=0;\n return 0;\n }\n int count=0;\n if(t.size()-t_begin==1){\n for(int k=s_begin;k<s.size();k++){\n if(mem[k][t_begin]!=-1){\n count+=mem[k][t_begin];\n break;\n }\n if(s[k]==t[t_begin])count++;\n }\n mem[s_begin][t_begin]=count;\n }\n else{\n for(int i=s_begin;i<s.size()-1;i++){\n if(s[i]==t[t_begin]){\n if(mem[i+1][t_begin+1]==-1)mem[i+1][t_begin+1]=solve(s,t,i+1,t_begin+1,mem);\n count+=mem[i+1][t_begin+1];\n }\n else{\n if(mem[i+1][t_begin]==-1)mem[i+1][t_begin]=solve(s,t,i+1,t_begin,mem);\n }\n }\n mem[s_begin][t_begin]=count;\n }\n return count;\n }\n};",
"memory": "351903"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "int dp[1001][1001];\nclass Solution {\npublic:\n int util(string s, string t, int i, int j) {\n if (j == t.size()) return 1;\n if (i == s.size()) return 0;\n if (dp[i][j] != -1) return dp[i][j];\n int o1 = util(s, t, i + 1, j);\n int o2 = s[i] == t[j] ? util(s, t, i + 1, j + 1) : 0;\n return dp[i][j] = o1 + o2;\n }\n int numDistinct(string s, string t) {\n if (s.size() < t.size()) return 0;\n memset(dp, -1, sizeof(dp));\n return util(s, t, 0, 0);\n }\n};",
"memory": "357081"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int numDistinct(string s, string t) {\n if(s.size()<t.size())return 0;\n vector<vector<int>> mem(s.size(),vector<int>(t.size(),-1));\n return solve(s,t,0,0,mem);\n }\n int solve(string s, string t, int s_begin, int t_begin, vector<vector<int>>& mem){\n // if(mem[s_begin][t_begin]!=-1)return mem[s_begin][t_begin];\n if(s.size()-s_begin<t.size()-t_begin){\n mem[s_begin][t_begin]=0;\n return 0;\n }\n int count=0;\n if(t.size()-t_begin==1){\n for(int k=s_begin;k<s.size();k++){\n if(mem[k][t_begin]!=-1){\n count+=mem[k][t_begin];\n break;\n }\n if(s[k]==t[t_begin])count++;\n }\n mem[s_begin][t_begin]=count;\n }\n else{\n for(int i=s_begin;i<s.size()-1;i++){\n if(s[i]==t[t_begin]){\n if(mem[i][t_begin]==-1)mem[i][t_begin]=solve(s,t,i+1,t_begin+1,mem);\n count+=mem[i][t_begin];\n }\n else{\n if(mem[i][t_begin]==-1)mem[i][t_begin]=solve(s,t,i+1,t_begin,mem);\n }\n }\n }\n return count;\n }\n};",
"memory": "362258"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int dp[1001][1001];\n int solve(int i, int j, string s, string t)\n {\n if(j==t.size())\n {\n return 1;\n }\n if(i==s.size()) return 0;\n\n if(dp[i][j]!=-1) return dp[i][j];\n\n int nontake = solve(i+1,j,s,t);\n int take = (s[i]==t[j]) ? solve(i+1,j+1,s,t) : 0;\n return dp[i][j] = take + nontake;\n }\n\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof(dp));\n int ans = solve(0,0,s,t);\n return ans;\n }\n};",
"memory": "367436"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1000][1000];\n int recursion(string s,string t,int i,int j){\n if(j>=t.length()) return 1;\n if(i>=s.length()) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n int way1=recursion(s,t,i+1,j);\n int way2=0;\n if(s[i]==t[j]) way2=recursion(s,t,i+1,j+1);\n return dp[i][j]=way1+way2; \n }\n int numDistinct(string s, string t) {\n memset(dp,-1,sizeof(dp));\n return recursion(s,t,0,0);\n }\n};",
"memory": "372613"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint find(string s, string t,int i, int j, int dp[1003][1003]) {\n if(j==t.length()) {\n return 1;\n }\n if(i==s.length()){\n return 0;\n }\n if(dp[i][j]!=-1) {\n return dp[i][j];\n }\n int notchoose=find(s,t,i+1,j,dp);\n int choose=0;\n if(s[i]==t[j]) {\nchoose = find(s,t,i+1,j+1,dp);\n }\n dp[i][j]=choose+notchoose;\n return dp[i][j];\n}\n int numDistinct(string s, string t) {\n int dp[1003][1003] ={0};\n for(int i=0;i<1003;i++) {\n for(int j=0;j<1003;j++) {\n dp[i][j]=-1;\n }\n }\n int ans=find(s,t,0,0,dp);\n return ans;\n \n }\n};",
"memory": "377791"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001];\n int ans(int i,int j, string s, string t){\n int n=s.length(), m=t.length();\n if(j>=m) return 1;\n if(i>=n) return 0;\n if(dp[i][j]!=-1) return dp[i][j];\n int res = 0;\n res = ans(i+1,j,s,t);\n if(s[i]==t[j]) res +=ans(i+1,j+1,s,t);\n return dp[i][j]= res ;\n\n }\n\n int numDistinct(string s, string t) {\n memset(dp, -1, sizeof(dp));\n return ans(0,0,s,t);\n }\n};",
"memory": "382968"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n int dp[1001][1001];\n\n int solve(string s, string t, int i, int j) {\n if (j >= t.size()) return 1;\n if (i >= s.size()) return 0;\n if (dp[i][j] != -1) return dp[i][j];\n\n int ans = solve(s, t, i+1, j);\n if (s[i] == t[j]) ans += solve(s, t, i+1, j+1);\n\n return dp[i][j] = ans;\n }\n\npublic:\n int numDistinct(string s, string t) {\n memset(dp, -1, sizeof(dp));\n return solve(s, t, 0, 0);\n }\n};",
"memory": "388146"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1001][1001] ;\n\n int numDistinctHelper(int i, int j, string s, string t){\n if(j < 0) return 1 ;\n if(i < 0) return 0 ;\n if(dp[i][j] != -1) return dp[i][j] ;\n\n int ans = numDistinctHelper(i-1, j, s, t) ;\n\n if(s[i] == t[j]){\n return dp[i][j] = numDistinctHelper(i-1, j-1, s, t) + ans ;\n }\n\n return dp[i][j] = ans ;\n }\n\n int numDistinct(string s, string t) {\n memset(dp, -1, sizeof dp) ;\n return numDistinctHelper(s.size() - 1, t.size() - 1, s, t) ;\n }\n};",
"memory": "393323"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n int dp[1001][1001];\n int solve(string s,string t,int i,int j){\n if(j==0) return 1;\n if(i==0 )return 0;\n if(dp[i][j]!=-1){\n return dp[i][j];\n }\n\n if(s[i-1]==t[j-1]){ \n int ans1,ans2;\n if (dp[i-1][j-1]!=-1){\n ans1 = dp[i-1][j-1];\n }else{\n ans1 = solve(s,t,i-1,j-1);\n dp[i-1][j-1]= ans1;\n }\n if (dp[i-1][j]!=-1){\n ans2 = dp[i-1][j];\n }else{\n ans2 = solve(s,t,i-1,j);\n dp[i-1][j]= ans2;\n }\n \n return dp[i][j]=ans1+ans2;\n }else{\n return dp[i][j]=solve(s,t,i-1,j);\n }\n\n }\npublic:\n int numDistinct(string s, string t) { \n memset(dp,-1,sizeof(dp));\n return solve(s,t,s.length(),t.length());\n \n }\n};",
"memory": "398501"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int memo[1001][1001];\n int subsequence(string s, string t, int s_index, int t_index){\n if(t_index < 0) return 1;\n if(s_index < 0){\n return 0;\n }\n if(memo[s_index][t_index] != -1) return memo[s_index][t_index];\n int skip = subsequence(s, t, s_index - 1, t_index);\n int take = 0;\n if(s[s_index] == t[t_index]) take = subsequence(s, t, s_index - 1, t_index - 1);\n return memo[s_index][t_index] = skip + take;\n }\n int numDistinct(string s, string t) {\n memset(memo, -1, sizeof(memo));\n return subsequence(s, t, s.size(), t.size());\n }\n};",
"memory": "403678"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int n,m;\n int dp[1001][1001];\n int solve(string s,string t,int i,int j){\n \n if(dp[i][j] != -1) return dp[i][j];\n\n if(j >= m) { \n return 1;\n }\n\n if(i >= n) return 0;\n int result = 0;\n \n if(s[i] == t[j]){ \n result += solve(s,t,i + 1,j + 1); \n result += solve(s,t,i + 1,j);\n }else { \n result += solve(s,t,i + 1,j);\n }\n\n return dp[i][j] = result;\n }\n int numDistinct(string s, string t) {\n n = s.size();\n m = t.size();\n memset(dp,-1,sizeof(dp));\n return solve(s,t,0,0);\n }\n};",
"memory": "408856"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int slen, tlen;\n int dp[1002][1002];\n int solve(int i , int j, string s, string t){\n if(j>=tlen){\n return 1;\n }\n if(i>=slen){\n return 0;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n int take = 0;\n int not_take = 0;\n\n if(s[i]==t[j]){\n take += solve(i+1,j+1,s,t);\n not_take += solve(i+1,j,s,t);\n }\n else{\n not_take += solve(i+1,j,s,t); \n }\n return dp[i][j] = take + not_take;\n\n }\n int numDistinct(string s, string t) {\n slen = s.length();\n tlen = t.length();\n memset(dp, -1, sizeof(dp));\n return solve(0,0,s,t);\n }\n};",
"memory": "408856"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int dp[1005][1005];\n int f(int i,int j,string s,string t){\n //base case\n if(j<0)return 1;\n if(i<0)return 0;\n if(dp[i][j]!=-1)return dp[i][j];\n int ans=0;\n if(s[i]==t[j]){\n ans+=f(i-1,j-1,s,t);\n ans+=f(i-1,j,s,t);\n }\n else{\n ans+=f(i-1,j,s,t);\n }\n return dp[i][j]=ans;\n }\n int numDistinct(string s, string t) {\n int n=s.length();\n int m=t.length();\n memset(dp,-1,sizeof(dp));\n return f(n-1,m-1,s,t); \n }\n};",
"memory": "414033"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int s_size , t_size ;\n int solve(int i , int j , string s , string t , vector<vector<int>>&dp){\n if(i == t_size){\n return 1 ;\n }\n if(j == s_size){\n return 0 ;\n }\n if(dp[i][j] != -1) return dp[i][j] ;\n \n int take = 0 ;\n if(t[i] == s[j]) take = solve(i+1 , j+1 , s , t , dp) ;\n int not_take = solve(i , j+1 , s , t , dp) ;\n return dp[i][j] = take + not_take ;\n \n }\n int numDistinct(string s, string t) {\n s_size = s.size() ;\n t_size = t.size() ;\n vector<vector<int>>dp(t_size + 1 , vector<int>(s_size + 1 , -1)) ;\n return solve(0 , 0 , s , t , dp) ;\n }\n};",
"memory": "414033"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n int fun(int ind1,int ind2,string s,string t,vector<vector<int>>&dp){\n if(ind2<0){\n return 1;\n }\n if(ind1==0){\n if(ind2==0){\n return s[ind1]==t[ind2];\n }\n return 0;\n }\n if(dp[ind1][ind2]!=-1)return dp[ind1][ind2];\n int nttake=fun(ind1-1,ind2,s,t,dp);\n int take=0;\n if(s[ind1]==t[ind2]){\n take+=fun(ind1-1,ind2-1,s,t,dp);\n }\n return dp[ind1][ind2]=take+nttake;\n }\npublic:\n int numDistinct(string s, string t) {\n int n=s.size(),m=t.size();\n vector<vector<int>>dp(n,vector<int>(m,-1));\n return fun(n-1,m-1,s,t,dp);\n }\n};",
"memory": "419211"
} |
115 | <p>Given two strings s and t, return <i>the number of distinct</i> <b><i>subsequences</i></b><i> of </i>s<i> which equals </i>t.</p>
<p>The test cases are generated so that the answer fits on a 32-bit signed integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "rabbbit", t = "rabbit"
<strong>Output:</strong> 3
<strong>Explanation:</strong>
As shown below, there are 3 ways you can generate "rabbit" from s.
<code><strong><u>rabb</u></strong>b<strong><u>it</u></strong></code>
<code><strong><u>ra</u></strong>b<strong><u>bbit</u></strong></code>
<code><strong><u>rab</u></strong>b<strong><u>bit</u></strong></code>
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "babgbag", t = "bag"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
As shown below, there are 5 ways you can generate "bag" from s.
<code><strong><u>ba</u></strong>b<u><strong>g</strong></u>bag</code>
<code><strong><u>ba</u></strong>bgba<strong><u>g</u></strong></code>
<code><u><strong>b</strong></u>abgb<strong><u>ag</u></strong></code>
<code>ba<u><strong>b</strong></u>gb<u><strong>ag</strong></u></code>
<code>babg<strong><u>bag</u></strong></code></pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 1000</code></li>
<li><code>s</code> and <code>t</code> consist of English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solve(string s, string t, int i, int j, vector<vector<int>> &dp){\n if(j < 0) return 1;\n if(i < 0) return 0;\n if(dp[i][j] != -1) return dp[i][j];\n\n int nottake = solve(s, t, i-1, j, dp);\n int take = 0;\n if(s[i] == t[j]) take = solve(s, t, i-1, j-1, dp);\n return dp[i][j] = take + nottake;\n }\n\n int numDistinct(string s, string t) {\n if(t.length() > s.length()) return 0;\n vector<vector<int>> dp(s.length(), vector<int>(t.length(), -1));\n return solve(s, t, s.length()-1, t.length()-1, dp);\n }\n};",
"memory": "419211"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n\n// we can use bfs but we should not use it for optimal approach\n\n Node* connect(Node* root){\n if(!root)return NULL;\n Node* Ref=root;\n root->next=NULL;\n while(root){\n \n Node* temp=root;\n while(temp){\n if(temp->left)temp->left->next=temp->right;\n if(temp->right){\n if(temp->next)temp->right->next=temp->next->left;\n else temp->right=NULL;\n }\n temp=temp->next;\n }\n root=root->left;\n }\n\n return Ref;\n }\n};",
"memory": "18000"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == NULL)\n return root;\n queue<Node*> q;\n q.push(root);\n while (!q.empty()) {\n int n = q.size();\n Node* prev =NULL;\n for (int i = 0; i < n; i++) {\n Node* curr = q.front();\n q.pop();\n if(i==0) {\n prev = curr;\n }\n else\n {\n prev->next = curr;\n prev = curr;\n }\n if (curr->left != NULL)\n q.push(curr->left);\n if (curr->right != NULL)\n q.push(curr->right);\n }\n prev->right = NULL;\n }\n\n return root;\n }\n};",
"memory": "18300"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n // Node* root;\n queue<Node*>q;\n if(!root)\n return root;\n q.push(root);\n while(!q.empty()){\n int n=q.size();\n Node* it1=q.front();\n q.pop();\n for(int i=0;i<n-1;i++){\n if(it1->left)\n q.push(it1->left);\n if(it1->right)\n q.push(it1->right);\n auto it=q.front();\n it1->next=it;\n it1=it;\n q.pop();\n }\n if(it1->left)\n q.push(it1->left);\n if(it1->right)\n q.push(it1->right);\n it1->right=NULL;\n }\n return root;\n }\n};",
"memory": "18400"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n Node* connect(Node* root) {\n if (root == nullptr)\n return nullptr;\n connectTwoNodes(root->left, root->right);\n return root;\n }\n\n private:\n void connectTwoNodes(Node* p, Node* q) {\n if (p == nullptr)\n return;\n p->next = q;\n connectTwoNodes(p->left, p->right);\n connectTwoNodes(q->left, q->right);\n connectTwoNodes(p->right, q->left);\n }\n};",
"memory": "18500"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root) {\n return nullptr;\n }\n\n Node* prev = root;\n Node* curr = nullptr;\n\n while(prev->left) {\n curr = prev;\n\n while(curr) {\n curr->left->next = curr->right;\n\n if(curr->next) {\n curr->right->next = curr->next->left;\n }\n curr = curr->next;\n }\n prev = prev->left;\n }\n return root;\n }\n};",
"memory": "18600"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n Node* connect(Node* root) {\n if (root == nullptr)\n return nullptr;\n connectTwoNodes(root->left, root->right);\n return root;\n }\n\n private:\n void connectTwoNodes(Node* p, Node* q) {\n if (p == nullptr)\n return;\n p->next = q;\n connectTwoNodes(p->left, p->right);\n connectTwoNodes(q->left, q->right);\n connectTwoNodes(p->right, q->left);\n }\n};",
"memory": "18600"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n void solve(Node* root)\n {\n if(root==NULL)\n {\n return ;\n }\n Node* l = root->left;\n Node* r = root->right;\n if(l!=NULL)\n {\n l->next = r;\n }\n if(r!=NULL and root->next!=NULL)\n {\n r->next = root->next->left;\n }\n solve(root->left);\n solve(root->right);\n return ;\n }\n\n Node* connect(Node* root) {\n if(root==NULL)\n {\n return root;\n }\n solve(root);\n return root;\n }\n};",
"memory": "18700"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n void helper(Node* root, Node* parent) {\n if (root == NULL)\n return;\n if (parent != NULL)\n parent->left->next = parent->right;\n if (parent != NULL and parent->next != NULL)\n parent->right->next = parent->next->left;\n if (root->left == NULL and root->right == NULL)\n return;\n helper(root->left, root);\n helper(root->right, root);\n }\n Node* connect(Node* root) {\n helper(root, NULL);\n return root;\n }\n};",
"memory": "18700"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root)\n return NULL;\n if(root->left)\n root->left->next=root->right;\n if(root->right && root->next)\n root->right->next=root->next->left;\n connect(root->left);\n connect(root->right);\n return root;\n \n }\n};",
"memory": "18800"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) \n {\n if(root == NULL)\n {\n return root ;\n } \n if(root->left != NULL)\n {\n root->left->next = root->right ;\n }\n if(root->right != NULL && root->next != NULL)\n {\n root->right->next = root->next->left ;\n }\n connect(root->left);\n connect(root->right);\n return root ;\n }\n};",
"memory": "18800"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == NULL) {\n return NULL;\n }\n if (root->left != NULL) {\n root->left->next = root->right;\n }\n if (root->right != NULL && root->next != NULL) {\n root->right->next = root->next->left;\n }\n connect(root->left);\n connect(root->right);\n return root;\n }\n};",
"memory": "18900"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\n//Upvote and Comment \n\n// class Solution {\n// public:\n// Node* connect(Node* root) {\n// //base case\n// if(root == NULL) return NULL;\n// //connects the left subtree of same level with right subtree of that same level \n// if(root->left != NULL) root->left->next = root->right;\n// //connect the rightmost node of a level to the leftmost node of the next level.\n// if(root->right != NULL && root->next != NULL) root->right->next = root->next->left;\n// //recursive calls for left and right subtrees.\n// connect(root->left);\n// connect(root->right);\n// return root;\n// }\n// };\n\n\n//Upvote and Comment\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n //Initialize pointers\n Node *prev = root, *curr;\n while (prev) {\n curr = prev;\n while (curr && curr->left) { \n //connects the left subtree of same level with right subtree of that same level \n curr->left->next = curr->right;\n //connect the rightmost node of a level to the leftmost node of the next level.\n if (curr -> next) curr->right->next = curr->next->left;\n curr = curr->next;\n }\n prev = prev -> left;\n }\n return root;\n }\n};",
"memory": "18900"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 1 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == nullptr) return root;\n queue<Node*> q;\n int levelN = 1,count = 0;\n\n q.push(root);\n while (!q.empty()) {\n Node* current = q.front();\n count++;\n q.pop();\n\n if (current->left) q.push(current->left);\n if (current->right) q.push(current->right);\n if (count == levelN) {\n count = 0;\n levelN = 2 * levelN;\n }\n else {\n if (!q.empty()) current->next = q.front();\n }\n }\n return root;\n }\n};",
"memory": "19000"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 1 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root) return NULL;\n\n queue<Node*> q;\n q.push(root);\n \n while(!q.empty()) {\n int sz = q.size();\n Node* prev = NULL;\n for(int i=0;i<sz;i++) {\n Node* ff = q.front();\n q.pop();\n if(prev) prev->next = ff ;\n prev = ff ;\n \n if(ff->left)q.push(ff->left);\n if(ff->right)q.push(ff->right);\n }\n }\n return root;\n \n }\n};",
"memory": "19000"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 1 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root)\n {\n return NULL;\n }\n queue<Node*> q;\n q.push(root);\n while(!q.empty())\n {\n int n= q.size();\n Node* prev = NULL;\n for(int i=0;i<n;i++)\n {\n auto temp = q.front();\n q.pop();\n if(temp->left)\n {\n q.push(temp->left);\n }\n if(temp->right)\n {\n q.push(temp->right);\n }\n if(prev!=NULL)\n {\n prev->next=temp;\n }\n prev=temp;\n }\n prev->next=NULL;\n }\n return root;\n }\n};",
"memory": "19100"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 1 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == nullptr) return root;\n queue<Node*> q;\n int levelN = 1,count = 0;\n\n q.push(root);\n while (!q.empty()) {\n Node* current = q.front();\n count++;\n q.pop();\n\n if (current->left) q.push(current->left);\n if (current->right) q.push(current->right);\n if (count == levelN) {\n count = 0;\n levelN = 2 * levelN;\n }\n else {\n if (!q.empty()) current->next = q.front();\n }\n }\n return root;\n }\n};",
"memory": "19100"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 2 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (!root)\n return root;\n\n queue<Node*> pq;\n pq.push(root);\n pq.push(NULL);\n while (!pq.empty()) {\n Node* curr = pq.front();\n pq.pop();\n if (curr == NULL) {\n if (!pq.empty()) {\n pq.push(NULL);\n }\n } else {\n curr->next = pq.front();\n if (curr->left)\n pq.push(curr->left);\n if (curr->right)\n pq.push(curr->right);\n }\n }\n return root;\n }\n};",
"memory": "19200"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 2 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root) return NULL;\n\n queue<Node*> q;\n q.push(root);\n \n while(!q.empty()) {\n int sz = q.size();\n Node* prev = NULL;\n for(int i=0;i<sz;i++) {\n Node* ff = q.front();\n q.pop();\n if(prev) prev->next = ff ;\n prev = ff ;\n if(i == sz-1){\n prev->next = NULL;\n }\n if(ff->left)q.push(ff->left);\n if(ff->right)q.push(ff->right);\n }\n }\n return root;\n \n }\n};",
"memory": "19200"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root)return root;\n queue<Node *>q;\n q.push(root);\n while(!q.empty()){\n int n=q.size();\n for(int i=0;i<n;i++){\n Node *cur=q.front();\n q.pop();\n if(!q.empty()){\n cur->next=q.front();\n }\n if(i==n-1){\n cur->next=nullptr;\n }\n if(cur->left)q.push(cur->left);\n if(cur->right)q.push(cur->right);\n }\n }\n return root;\n }\n};",
"memory": "19300"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (!root) {\n return root;\n }\n queue<Node*> q{{root}};\n while (!q.empty()) {\n Node* p = nullptr;\n for (int n = q.size(); n; --n) {\n Node* node = q.front();\n q.pop();\n if (p) {\n p->next = node;\n }\n p = node;\n if (node->left) {\n q.push(node->left);\n }\n if (node->right) {\n q.push(node->right);\n }\n }\n }\n return root;\n }\n};",
"memory": "19300"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cout.tie(nullptr);\n std::cin.tie(nullptr);\n return true;\n}();\n\nclass Solution {\npublic:\n std::queue<Node*> q;\n Node* connect(Node* root) {\n q.push(root);\n int t,j;\n Node* tmp;\n while(true)\n {\n if(q.front()==NULL)\n {\n break;\n }\n j=q.size();\n t=j;\n while(j--)\n {\n q.push(q.front()->left);\n q.push(q.front()->right);\n tmp=q.front();\n q.pop();\n if(j==0)\n {\n tmp->next=NULL;\n }\n else\n {\n tmp->next=q.front();\n }\n }\n \n }\n return root;\n \n }\n};",
"memory": "19400"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(root == nullptr) return root;\n if(root->left == nullptr && root->right == nullptr){\n root->next = nullptr;\n return root;\n }\n queue<Node*> nodes;\n queue<int> levels;\n nodes.push(root->left);\n levels.push(2);\n nodes.push(root->right);\n levels.push(2);\n while(!(nodes.size() == 1)){\n Node* temp = nodes.front();\n int level_temp = levels.front();\n nodes.pop();\n levels.pop();\n if(temp->left != nullptr && temp->right != nullptr){\n nodes.push(temp->left);\n nodes.push(temp->right);\n levels.push(level_temp+1);\n levels.push(level_temp+1);\n }\n Node* node_next = nodes.front();\n int level_next = levels.front();\n if(level_temp == level_next){\n temp->next = node_next;\n }\n else temp->next = nullptr; \n }\n Node* final = nodes.front();\n final->next = nullptr;\n nodes.pop();\n levels.pop();\n return root;\n }\n};",
"memory": "19400"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n\n void bfs(Node* root){\n if(root==NULL) return;\n queue<Node*> q, tq;\n Node *last, *curr;\n q.push(root);\n while(!q.empty()){ \n last = NULL;\n while(!q.empty()){\n curr = q.front();\n q.pop();\n if(curr->left) tq.push(curr->left);\n if(curr->right) tq.push(curr->right);\n if(last) last->next = curr;\n last = curr;\n }\n \n while(!tq.empty()){\n q.push(tq.front());\n tq.pop();\n }\n }\n\n }\n Node* connect(Node* root) {\n bfs(root);\n return root;\n }\n};",
"memory": "19500"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n void dfs(Node* root){\n queue<Node*> frontier;\n\n frontier.push(root);\n while(!frontier.empty()){\n int popping = frontier.size();\n vector<Node*> connections;\n for(int i = 0; i < popping; i++){\n Node* front = frontier.front();\n frontier.pop();\n connections.push_back(front);\n if(front->left != NULL){\n frontier.push(front->left);\n }\n if(front->right != NULL){\n frontier.push(front->right);\n }\n \n \n \n }\n for(int j = 0; j < connections.size(); j++){\n cout << connections[j]->val << \" \";\n if(j == connections.size() - 1) connections[j]->next = nullptr;\n else{\n connections[j]->next = connections[j+1];\n }\n }\n cout << endl;\n connections.clear();\n\n }\n }\n\n Node* connect(Node* root) {\n Node* ans = root;\n if(root != NULL){\n dfs(ans);\n }\n \n \n return ans;\n \n }\n};",
"memory": "19500"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(root == NULL) return root;\n vector<Node *> res;\n res.push_back(root);\n while(res.size()) {\n int n = res.size();\n for(int i = 0; i < n - 1; i++) {\n res[i]->next = res[i + 1];\n }\n res.back()->next = NULL;\n vector<Node*> nextLevel;\n for(auto &node: res) {\n if(node->left)\n nextLevel.push_back(node->left);\n if(node->right) \n nextLevel.push_back(node->right);\n }\n res = nextLevel;\n }\n return root;\n \n }\n};",
"memory": "19600"
} |
116 | <p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,6,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n \n queue<Node*> q;\n //vector<vector<int>> v;\n if(root == NULL) return root;\n q.push(root);\n\n while(!q.empty())\n {\n int size = q.size();\n vector<Node*> s;\n for(int i = 0 ; i<size ; i++)\n {\n Node *temp = q.front();\n q.pop();\n\n if(temp->left !=NULL) q.push(temp->left);\n if(temp->right !=NULL)q.push(temp->right);\n s.push_back(temp);\n }\n\n //doing update.....\n int j = 0;\n for( j = 0 ; j<s.size()-1;j++)\n {\n s[j]->next = s[j+1];\n }\n s[j]->next = NULL;\n }\n return root;\n }\n};",
"memory": "19700"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (!root) {\n return nullptr;\n }\n cout<<root->val<<endl;\n if (root->left && root->right) {\n root->left->next = root->right;\n }\n if (root->next) {\n Node* nextChild = nullptr;\n for (Node* temp=root->next; temp; temp=temp->next) {\n if (!temp->left && !temp->right) {\n continue;\n }\n if (temp->left) {\n nextChild = temp->left;\n break;\n } else if (temp->right) {\n nextChild = temp->right;\n break;\n }\n }\n if (root->left && root->right) {\n root->right->next = nextChild;\n } else if (root->left) {\n root->left->next = nextChild;\n } else if (root->right) {\n root->right->next = nextChild;\n }\n }\n connect(root->right);\n connect(root->left);\n return root;\n }\n};",
"memory": "18200"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (!root) {\n return nullptr;\n }\n cout<<root->val<<endl;\n if (root->left && root->right) {\n root->left->next = root->right;\n }\n if (root->next) {\n Node* nextChild = nullptr;\n for (Node* temp=root->next; temp; temp=temp->next) {\n if (!temp->left && !temp->right) {\n continue;\n }\n if (temp->left) {\n nextChild = temp->left;\n break;\n } else if (temp->right) {\n nextChild = temp->right;\n break;\n }\n }\n if (root->left && root->right) {\n root->right->next = nextChild;\n } else if (root->left) {\n root->left->next = nextChild;\n } else if (root->right) {\n root->right->next = nextChild;\n }\n }\n //tricky!! right subtree first\n connect(root->right);\n connect(root->left);\n return root;\n }\n};",
"memory": "18300"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == nullptr) return root;\n\n Node* leader = root;\n\n while (leader != nullptr) {\n Node* next = leader;\n leader = nullptr;\n Node node(-1);\n Node* prev = &node;\n while (next != nullptr) {\n if (next->left != nullptr) {\n prev->next = next->left;\n prev = prev->next;\n if (leader == nullptr) leader = prev;\n }\n \n if (next->right != nullptr) {\n prev->next = next->right;\n prev = prev->next;\n if (leader == nullptr) leader = prev;\n }\n next = next->next;\n }\n \n }\n\n return root;\n }\n};",
"memory": "18300"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n Node* connect(Node* root) {\n Node* node = root; // the node that is above the current needling\n\n while (node != nullptr) {\n Node dummy(0); // the dummy node before needling\n // Needle the children of the node.\n for (Node* needle = &dummy; node; node = node->next) {\n if (node->left != nullptr) { // Needle the left child.\n needle->next = node->left;\n needle = needle->next;\n }\n if (node->right != nullptr) { // Needle the right child.\n needle->next = node->right;\n needle = needle->next;\n }\n }\n node = dummy.next; // Move the node to the next level.\n }\n\n return root;\n }\n };\n",
"memory": "18400"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n Node* connect(Node* root) {\n Node* node = root; // the node that is above the current needling\n\n while (node != nullptr) {\n Node dummy(0); // the dummy node before needling\n // Needle the children of the node.\n for (Node* needle = &dummy; node; node = node->next) {\n if (node->left != nullptr) { // Needle the left child.\n needle->next = node->left;\n needle = needle->next;\n }\n if (node->right != nullptr) { // Needle the right child.\n needle->next = node->right;\n needle = needle->next;\n }\n }\n node = dummy.next; // Move the node to the next level.\n }\n\n return root;\n }\n};",
"memory": "18400"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node *findnext(Node *root)\n {\n if(!root)\n {\n return root;\n }\n if(root->left)\n {\n return root->left;\n }\n if(root->right)\n {\n return root->right;\n }\n\n return findnext(root->next);\n }\n Node* connect(Node* root) {\n if(!root)\n {\n return root;\n }\n\n if(root->left)\n {\n if(root->right)\n {\n root->left->next=root->right;\n }\n else\n {\n root->left->next=findnext(root->next);\n }\n \n }\n\n if(root->right)\n {\n root->right->next=findnext(root->next);\n }\n\n \n \n \n connect(root->right);\n connect(root->left);\n\n\n return root;\n }\n};",
"memory": "18500"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n // if(root==nullptr) {\n // return root;\n // }\n // Node* temp = root;\n // queue<Node*> q;\n // q.push(temp);\n // while(!q.empty()){\n // int size =q.size();\n // for(int i= 0;i<size;i++){\n // auto p = q.front();\n // q.pop();\n // if(i==size-1){\n // p->next=nullptr;\n // }else{\n // p->next=q.front();\n // }\n // if(p->left!=nullptr){\n // q.push(p->left);\n // }\n // if(p->right!=nullptr){\n // q.push(p->right);\n // }\n // }\n // }\n // return root;\n\n\n //without using extra space \n Node* ans = root;\n while(root!=nullptr){\n Node* temp = new Node(0);\n Node* curr = temp;\n while(root!=nullptr){\n if(root->left!=nullptr){\n curr->next=root->left;\n curr = curr->next;\n }\n if(root->right!=nullptr){\n curr->next=root->right;\n curr = curr->next;\n }\n root = root->next;\n }\n root= temp->next;\n }\n return ans;\n }\n};",
"memory": "18500"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n// void solve(Node* &left, Node* &right){\n// if(right==NULL||left==NULL) return;\n\n// left->next=right;\n// if(left->left&& left->right)\n// solve(left->left, left->right);\n// if(right->left&& right->right)\n// solve(right->left, right->right);\n\n \n// if(left->right&& right->left)\n// solve(left->right, right->left);\n// if(left->right&& !right->left&&right->right)\n// solve(left->right, right->right);\n\n// if(!left->right&&left->left&& right->left)\n// solve(left->left, right->left);\n// if(!left->right&&left->left&&! right->left&&right->right)\n// solve(left->left, right->right);\n \n// }\n Node* connect(Node* root) \n {\n if (!root)\n return root;\n queue<Node*> q;\n q.push(root);\n q.push(NULL);\n while (q.size() > 1)\n {\n Node* curr = q.front();\n q.pop();\n if (!curr)\n {\n q.push(NULL);\n continue;\n }\n curr->next = q.front();\n if (curr->left)\n q.push(curr->left);\n if (curr->right)\n q.push(curr->right);\n }\n return root;\n }\n};",
"memory": "18600"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n Node* connect(Node* root) {\n Node* node = root; // the node that is above the current needling\n\n while (node != nullptr) {\n Node dummy(0); // the dummy node before needling\n // Needle the children of the node.\n for (Node* needle = &dummy; node; node = node->next) {\n if (node->left != nullptr) { // Needle the left child.\n needle->next = node->left;\n needle = needle->next;\n }\n if (node->right != nullptr) { // Needle the right child.\n needle->next = node->right;\n needle = needle->next;\n }\n }\n node = dummy.next; // Move the node to the next level.\n }\n\n return root;\n }\n};",
"memory": "18600"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 1 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == nullptr) return root;\n queue<Node*> q1;\n queue<int> q2;\n\n q1.push(root);\n q2.push(0);\n while (!q1.empty()) {\n Node* current = q1.front();\n int level = q2.front();\n q1.pop();\n q2.pop();\n\n if (current->left) {\n q1.push(current->left);\n q2.push(level + 1);\n }\n if (current->right) {\n q1.push(current->right);\n q2.push(level + 1);\n }\n if (!q1.empty() && q2.front() == level) {\n current->next = q1.front();\n } \n }\n return root;\n }\n};",
"memory": "18700"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 1 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root) return NULL;\n queue<Node*> q;\n q.push(root);\n while(!q.empty()) {\n int sz = q.size();\n Node* prev = NULL;\n for(int i=0;i<sz;i++) {\n Node* ff = q.front();\n q.pop();\n if(prev) prev->next = ff ;\n prev = ff;\n if(ff->left)q.push(ff->left);\n if(ff->right)q.push(ff->right);\n }\n }\n return root;\n }\n};",
"memory": "18700"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 2 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n typedef pair<Node*,int> nq;\n deque<Node*> q;\n deque<int> p;\n Node* connect(Node* root) {\n if (!root) return root;\n Node* prev = nullptr; int d = 0;\n q.push_back(root); p.push_back(0);\n while (!q.empty()) {\n int f = p.front(); Node* w = q.front();\n if (d == f && prev) {\n prev->next = w; prev = prev->next;\n } else { d = f; prev = w; }\n if (w->left) {\n q.push_back(w->left);\n p.push_back(f+1);\n }\n if (w->right) {\n q.push_back(w->right);\n p.push_back(f+1);\n }\n q.pop_front(); p.pop_front();\n }\n return root;\n }\n};",
"memory": "18800"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 2 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == nullptr) return root;\n queue<Node*> q1;\n queue<int> q2;\n\n q1.push(root);\n q2.push(0);\n while (!q1.empty()) {\n Node* current = q1.front();\n int level = q2.front();\n q1.pop();\n q2.pop();\n\n if (current->left) {\n q1.push(current->left);\n q2.push(level + 1);\n }\n if (current->right) {\n q1.push(current->right);\n q2.push(level + 1);\n }\n if (!q1.empty() && q2.front() == level) {\n current->next = q1.front();\n } \n }\n return root;\n }\n};",
"memory": "18800"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n typedef pair<Node*,int> nq;\n deque<nq> q;\n Node* connect(Node* root) {\n if (!root) return root;\n int d = 0; Node* prev = nullptr;\n q.push_back(nq{root,0});\n while (!q.empty()) {\n nq t = q.front();\n if (d == t.second && prev) {\n prev->next = t.first; prev = t.first;\n } else { d = t.second; prev = t.first; }\n if (t.first->left) q.push_back(nq{t.first->left,t.second+1});\n if (t.first->right) q.push_back(nq{t.first->right,t.second+1});\n q.pop_front();\n }\n return root;\n }\n};",
"memory": "18900"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == nullptr) return root;\n queue<Node*> q1;\n queue<int> q2;\n\n q1.push(root);\n q2.push(0);\n while (!q1.empty()) {\n Node* current = q1.front();\n int level = q2.front();\n q1.pop();\n q2.pop();\n\n if (current->left) {\n q1.push(current->left);\n q2.push(level + 1);\n }\n if (current->right) {\n q1.push(current->right);\n q2.push(level + 1);\n }\n if (!q1.empty() && q2.front() == level) {\n current->next = q1.front();\n } \n }\n return root;\n }\n};",
"memory": "18900"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n std::queue<Node*> visited;\n std::queue<Node*> currentLevel;\n\n if (!root)\n return root;\n\n visited.push(root);\n\n while (visited.size()) {\n Node *prev = NULL;\n while (visited.size()) {\n auto current = visited.front();\n if (prev)\n prev->next = current;\n visited.pop();\n if (current->left) {\n currentLevel.push(current->left);\n // std::cout << \"push \" << current->left->val << std::endl;\n }\n if (current->right) {\n currentLevel.push(current->right);\n // std::cout << \"push \" << current->right->val << std::endl;\n }\n prev = current;\n }\n // std::cout << \"done = \" << done << std::endl;\n \n // std::cout << \"currentLevel size = \" << currentLevel.size() << std::endl;\n \n while (currentLevel.size()) {\n visited.push(currentLevel.front());\n // std::cout << \"visited: push \" << currentLevel.front()->val << std::endl;\n currentLevel.pop();\n }\n // std::cout << \"visited size = \" << visited.size() << std::endl;\n }\n\n return root;\n }\n};",
"memory": "19000"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(root==NULL) return root;\n \n queue<Node*>q;\n q.push(root);\n \n \n while(!q.empty())\n {\n int n = q.size();\n\n vector<Node*>v;\n \n for(int i=0; i<n; i++)\n {\n Node *node = q.front();\n q.pop();\n \n if(node->left) q.push(node->left);\n if(node->right) q.push(node->right);\n \n v.push_back(node);\n }\n \n for(int i=1; i<v.size(); i++)\n {\n Node* left = v[i-1];\n Node* right = v[i];\n \n left->next = right;\n }\n }\n \n return root;\n }\n};",
"memory": "19000"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root)return nullptr;\n queue<Node*> q;\n q.push(root);\n while(!q.empty()){\n int size=q.size();\n vector<Node*> ds;\n for(int i=0;i<size;i++){\n Node* node=q.front();\n q.pop();\n if(node->left!=nullptr){\n q.push(node->left);\n ds.push_back(node->left);\n }\n if(node->right!=nullptr){\n q.push(node->right);\n ds.push_back(node->right);\n } \n }\n for(int i=0;i<ds.size();i++){\n if(i!=ds.size()-1){\n ds[i]->next=ds[i+1];\n }\n }\n }\n return root; \n }\n};",
"memory": "19100"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root){\n return root;\n }\n queue<Node*>q;\n q.push(root);\n while(!q.empty()){\n int x = q.size();\n vector<Node*>v;\n for(int i=0;i<x;i++){\n Node*curr = q.front();\n q.pop();\n if(curr->left){\n q.push(curr->left);\n }\n if(curr->right){\n q.push(curr->right);\n }\n v.push_back(curr);\n }\n for(int j=0;j<v.size()-1;j++){\n v[j]->next = v[j+1];\n }\n }\n return root;\n }\n};",
"memory": "19200"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) \n {\n if(root == nullptr) return nullptr;\n queue<pair<int,Node*>> q;\n q.push({0,root});\n int prev_h = 0;\n Node* prev = nullptr;\n while(!q.empty())\n {\n Node* u = q.front().second;\n int h = q.front().first;\n q.pop();\n if(u == nullptr) continue;\n if(h!=prev_h)\n {\n if(prev!=nullptr) prev->next = nullptr;\n }\n else\n {\n if(prev!=nullptr) prev->next = u;\n }\n q.push({h+1,u->left});\n q.push({h+1,u->right});\n prev_h = h;\n prev = u;\n }\n\n return root; \n }\n};",
"memory": "19200"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (!root)\n {\n return root;\n }\n queue<Node*> q;\n q.emplace(root);\n vector<vector<Node*>> levels;\n \n while (!q.empty())\n {\n int size = q.size();\n vector<Node*> v;\n for (int i = 0; i < size; i++)\n {\n Node* temp = q.front();\n q.pop();\n if (temp->left)\n {\n q.emplace(temp->left);\n v.push_back(temp->left);\n }\n if (temp->right)\n {\n q.emplace(temp->right);\n v.push_back(temp->right);\n }\n }\n if (!v.empty())\n {\n levels.push_back(v);\n }\n v.clear();\n }\n \n for (int i = 0; i < levels.size(); i++)\n {\n for (int j = 0; j < levels[i].size()-1; j++)\n {\n levels[i][j]->next = levels[i][j+1];\n }\n }\n \n return root;\n }\n};",
"memory": "19300"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) \n {\n vector<Node*>l;\n vector<vector<Node*>>v;\n queue<Node *>q;\n if(root==NULL)return root;\n\n q.push(root);\n while(q.empty()!=true)\n {\n int count=q.size();\n for(int i=0;i<count;i++)\n {\n Node *temp=q.front();\n if(temp!=NULL)l.push_back(temp);\n if(temp!=NULL&&temp->left!=NULL)q.push(temp->left);\n if(temp!=NULL&&temp->right!=NULL)q.push(temp->right);\n q.pop();\n }\n v.push_back(l);\n l.clear();\n } \n for(auto x:v)\n {\n for(int i=0;i<x.size()-1;i++)\n {\n x[i]->next=x[i+1];\n }\n }\n return root; \n }\n};",
"memory": "19400"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n unordered_map<int,vector<Node*>> um;\n void helper(Node*node,int i){\n if(node){\n helper(node->left,i+1);\n helper(node->right,i+1);\n um[i].push_back(node);\n }\n }\n Node* connect(Node* root) {\n helper(root,0);\n for(auto &v:um){\n v.second.push_back(NULL);\n for(int i=0;i<v.second.size()-1;i++){\n v.second[i]->next=v.second[i+1];\n }\n }return root;\n }\n};",
"memory": "19400"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if (root == nullptr){\n return nullptr;\n }\n std::list<Node*> list;\n list.push_back(root);\n while (list.size() > 0){\n int size = list.size();\n Node* prev = nullptr;\n for (int i = 0; i < size; i++) {\n Node* current = list.front();\n list.pop_front();\n if (prev != nullptr){\n prev->next = current;\n }\n if (current->left != nullptr){\n list.push_back(current->left);\n }\n if (current->right != nullptr){\n list.push_back(current->right);\n }\n\n prev = current;\n }\n }\n\n return root;\n }\n};",
"memory": "19500"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _next)\n : val(_val), left(_left), right(_right), next(_next) {}\n};\n*/\nclass Solution {\n \npublic:\n Node* connect(Node* root) {\n if (root==NULL) return NULL;\n vector<vector<Node*>> v;\n \n queue<Node*> q;\n q.push(root);\n while(!q.empty()){\n int s=q.size();\n vector<Node*> temp;\n for(int i=0;i<s;i++){\n Node* sky=q.front();\n q.pop();\n if(sky->left) q.push(sky->left);\n if(sky->right) q.push(sky->right);\n temp.push_back(sky);\n }\n temp.push_back(NULL);\nv.push_back(temp);\n }\n\n for(int i=0;i<v.size();i++){\n for(int j=0;j<v[i].size()-1;j++){\n v[i][j]->next=v[i][j+1];\n }\n }\n return root;\n }\n \n};",
"memory": "19600"
} |
117 | <p>Given a binary tree</p>
<pre>
struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
</pre>
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/15/117_sample.png" style="width: 500px; height: 171px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,5,null,7]
<strong>Output:</strong> [1,#,2,3,#,4,5,7,#]
<strong>Explanation: </strong>Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> root = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 6000]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong></p>
<ul>
<li>You may only use constant extra space.</li>
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
</ul>
| 3 | {
"code": "class Solution {\n\tpublic:\n\t\tNode* connect(Node* root) {\n\t\t\tif(!root) return NULL;\n\n\t\t\tvector<vector<Node*>> lvl;\n\t\t\tqueue <Node*> q;\n\t\t\tq.push(root);\n\n\t\t\twhile(!q.empty()){\n\t\t\t\tint size = q.size();\n\t\t\t\tvector <Node*> temp;\n\n\t\t\t\tfor(int i = 0 ; i < size ; i++){\n\t\t\t\t\tNode* node = q.front();\n\t\t\t\t\tq.pop();\n\t\t\t\t\tif(node -> left) q.push(node -> left);\n\t\t\t\t\tif(node -> right) q.push(node -> right);\n\t\t\t\t\ttemp.push_back(node);\n\t\t\t\t}\n\n\t\t\t\ttemp.push_back(NULL);\n\t\t\t\tlvl.push_back(temp);\n\t\t\t} \n\n\t\t\tfor(int i = 0 ; i < lvl.size() ; i++){\n\t\t\t\tfor(int j = 0 ; j < lvl[i].size() - 1 ; j++) lvl[i][j] -> next = lvl[i][j + 1];\n\t\t\t}\n\n\t\t\treturn root;\n\t\t}\n\t};",
"memory": "19600"
} |
118 | <p>Given an integer <code>numRows</code>, return the first numRows of <strong>Pascal's triangle</strong>.</p>
<p>In <strong>Pascal's triangle</strong>, each number is the sum of the two numbers directly above it as shown:</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" style="height:240px; width:260px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> numRows = 5
<strong>Output:</strong> [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> numRows = 1
<strong>Output:</strong> [[1]]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numRows <= 30</code></li>
</ul>
| 0 | {
"code": "long long int factorial(int n)\n{\n long long int res = 1, i;\n for (i = 1; i <= n; i++)\n res *= i;\n return res;\n}\nunsigned nck( unsigned n, unsigned k )\n{\n if (k > n) return 0;\n if (k * 2 > n) k = n-k;\n if (k == 0) return 1;\n\n int result = n;\n for( int i = 2; i <= k; ++i ) {\n result *= (n-i+1);\n result /= i;\n }\n return result;\n}\nclass Solution {\npublic:\n vector<vector<int>> generate(int numRows) {\n vector<vector<int>>pascal;\n int c;\n vector<int>tmp;\n for(int i=0;i<numRows;i++){\n for(int j=i;j>=0;j--){\n //c=factorial(i)/(factorial(j)*factorial((i-j)));\n tmp.push_back(nck(i,j));\n }\n pascal.push_back(tmp);\n tmp.clear();\n }\n return pascal;\n }\n};",
"memory": "7800"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.