id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\npublic:\n int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) {\n for(int i = 0; i < edges.size(); ++i){\n tree[edges[i][0]].push_back(edges[i][1]);\n tree[edges[i][1]].push_back(edges[i][0]);\n }\n\n for(int i = 0; i < hasA...
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\npublic:\n unordered_map<int,vector<int>>g ;\n unordered_map<int,bool>v;\n\n void graph(vector<vector<int>>&edges) {\n for (auto e:edges) {\n g[e[0]].push_back(e[1]);\n g[e[1]].push_back(e[0]);\n }\n }\n\n int dfs(int node,int cost,vector<...
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<int>> g; // to store the graph\n unordered_map<int, bool> visited; // to stop exploring same nodes again and again.\n\t\n void createGraph(vector<vector<int>>& edges) {\n for (auto e: edges) {\n g[e[0]].push_back(e[1]); // adjecency...
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\npublic:\n unordered_map<int, vector<int>> g; // to store the graph\n unordered_map<int, bool> visited; // to stop exploring same nodes again and again.\n\t\n void createGraph(vector<vector<int>>& edges) {\n for (auto e: edges) {\n g[e[0]].push_back(e[1]); // adjecenc...
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\n pair<int, bool> calcSteps(int src, map<int, set<int>>& edges,\n vector<bool>& hasApple) {\n int steps = 0;\n bool flg = hasApple[src];\n\n for (const int dst : edges[src]) {\n edges[dst].erase(src);\n\n auto [s, f...
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\n\nbool solve(unordered_map<int,list<int> >& adj,unordered_map<int,bool>& visited,vector<bool>& hasApple,int& ans,int node,vector<bool>& s){\n visited[node] = true;\n bool temp = false,k=false;\n\n for(auto it:adj[node]){\n \n if(!visited[it]){\n ans...
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\npublic:\n\n int recurTree( unordered_map<int,vector<int>>& adjList, unordered_set<int>& visited,\n vector<bool>& hasApple, int node, int & answer ){\n visited.insert( node );\n vector<int>number;\n for( int i=0; i<adjList[node].size(); i++ ){\n ...
1,554
<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <stro...
3
{ "code": "class Solution {\npublic:\n vector<list<int>>adj;\n set<int>visited;\n set<int>apple;\n pair<int,int> time(int src){\n // how much time does it take to fetch all nodes furthur and return to it it will \n // if(visited.find()!=visited.end()) return {0,0};\n visited.insert(sr...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
0
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n /*\n dp[i][j][remain] = number of ways to cut pizza[i: r][j: c] with remain cuts\n dp[i][j][remain] = sum(dp[r][c][remain-1]) if dp[r][c] is valid for cut\n */\n int mod = 1e9+7;\n in...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
0
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n int mod = 1e9+7;\n int m = pizza.size();\n int n = pizza[0].size();\n int apple[m+1][n+1];\n int dp[m][n][k];\n memset(apple, 0, sizeof(apple));\n memset(dp, 0, sizeof(dp));\n f...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
0
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n const int MOD = 1000000000 + 7;\n int m = pizza.size();\n int n = pizza[0].size();\n int dp[51][51][10] = {}; // dp[i][j][p] is the result begin from pizza[i][j] when k = p + 1\n int cnt[51][51] ...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
0
{ "code": "class Solution {\npublic:\n const int MOD = 1e9+7;\n int m, n;\n vector<vector<int>> sum;\n \n // 查询 (r1, c1) 到 (r2, c2) 矩形区域的苹果数量\n int query(int r1, int c1, int r2, int c2) {\n return sum[r2][c2] - sum[r2][c1] - sum[r1][c2] + sum[r1][c1];\n }\n\n int memo[15][55][55]; // 记忆...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
1
{ "code": "class Solution {\npublic:\n int dp[51][51][10];\n int mod = 1e9+7;\n int helper(int i, int j, vector<vector<int>> &apple, int k){\n int n = apple.size();\n int m = apple[0].size();\n if(apple[i][j]<k)\n return 0;\n if(k==1){\n if(apple[i][j]>=1){\n...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
1
{ "code": "// https://www.acwing.com/solution/content/13013/\nclass Solution {\npublic:\n bool check(int x1, int y1, int x2, int y2, const vector<vector<int>>& p) {\n return p[x1][y1] - p[x2 + 1][y1] - p[x1][y2 + 1] + p[x2 + 1][y2 + 1] > 0;\n }\n \n int ways(vector<string>& pizza, int k) {\n ...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
1
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n int rows = pizza.size(), cols = pizza[0].size();\n vector apples(rows + 1, vector<int>(cols + 1));\n vector f(rows, vector<int>(cols));\n for (int row = rows - 1; row >= 0; row--) {\n for (int c...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
1
{ "code": "class Solution {\npublic:\n int ways(vector<string>& P, int K) {\n K--;\n int mod = 1e9+7;\n int n = P.size(), m = P[0].size();\n vector sum(n+1, vector<int>(m+1, 0)); \n vector DP(K+1, vector(n+1, vector<long long>(m+1, 0)));\n\n for (int r=n-1;r>=0;r--)\n ...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
1
{ "code": "int mod = 1e9+7;\nclass Solution {\npublic:\n vector<vector<vector<int>>> dp;\n\n bool hasApple(vector<string>& pizza, int row, int col, int n, int m) {\n for (int i = row; i < n; i++) {\n for (int j = col; j < m; j++) {\n if (pizza[i][j] == 'A') return true;\n ...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
1
{ "code": "class Solution {\nprivate:\n int mod = 1000000007;\n int cutDP(int x, int y, vector<vector<int>> &sum, vector<vector<vector<int>>> &dp, int k ){\n if( !sum[y][x] )\n return 0;\n \n if( k == 1 ){\n return 1;\n }\n if( dp[k][y][x] != -1)\n re...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
2
{ "code": "class Solution {\npublic:\n\n int ways(vector<string>& pizza, int k) {\n int mod = 1e9 + 7;\n int r = pizza.size();\n int c = pizza[0].size();\n\n vector<vector<int>> pref(r + 1, vector<int>(c + 1));\n for(int i = r - 1;i >= 0;i --) {\n for(int j = c - 1;j >...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
2
{ "code": "const int MOD = 1000000007;\n\nclass Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n int rows = pizza.size(), cols = pizza[0].size();\n vector<vector<int>> count(rows + 1, vector<int>(cols + 1));\n for (int i = rows - 1; i >= 0; i--) {\n for (int j = cols...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
2
{ "code": "const int MOD = 1000000007;\n\nclass Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n int rows = pizza.size(), cols = pizza[0].size();\n vector<vector<int>> count(rows + 1, vector<int>(cols + 1));\n for (int i = rows - 1; i >= 0; i--) {\n for (int j = cols...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int kin) {\n vector<vector<vector<long long>>> dp(\n pizza.size(),\n vector<vector<long long>>(\n pizza[0].size(),\n vector<long long>(kin+1, 0)\n )\n );\n vect...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int kin) {\n vector<vector<vector<long long>>> dp(\n pizza.size(),\n vector<vector<long long>>(\n pizza[0].size(),\n vector<long long>(kin+1, 0)\n )\n );\n vect...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n #define ll long long \n const int mod = 1e9+7;\n \n ll f(int row, int col, int k, vector<vector<int>> &pre, vector<vector<vector<ll>>>& dp) {\n int n = pre.size() - 1;\n int m = pre[0].size() - 1;\n \n int rem = pre[n][m] - pre[row][m] - pre[n...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n const int MOD = 1000000007;\n int rows = pizza.size(), cols = pizza[0].size();\n \n // Create a prefix sum array\n vector<vector<int>> prefix(rows + 1, vector<int>(cols + 1, 0));\n for (int i...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "\nclass Solution {\npublic:\n const int mod = 1e9 + 7;\n long long memo[51][51][51];\n\n // starting from (x, y), get k pieces\n long long dfs(int k, int x, int y, vector<vector<int>>& sum, int m, int n) {\n if (memo[k][x][y] != -1) return memo[k][x][y];\n if (sum[x][y] == 0) retu...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n int n=pizza.size(), m=pizza[0].size();\n const int mod = 1e9+7;\n // int apples = 0;\n // for(auto &i:pizza){\n // for(auto j:i) if(j=='A') apples++;\n // }\n vector<vector<vector<...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n int m, n;\n int appless[55][55];\n long long mod = 1e9 + 7;\n vector<vector<vector<int>>> memo;\n\n int solve(int i, int j, int k) {\n if (appless[i][j] < k) return 0;\n if (k == 1) return appless[i][j] > 0;\n if (memo[i][j][k] != -1) return m...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n const int MOD = 1e9 + 7;\n int rows = pizza.size();\n int cols = pizza[0].size();\n\n // Preprocess the number of apples in the submatrix from (row,col) to (rows-1, cols-1)\n vector<vector<int>> app...
1,555
<p>Given a rectangular pizza represented as a <code>rows x cols</code>&nbsp;matrix containing the following characters: <code>&#39;A&#39;</code> (an apple) and <code>&#39;.&#39;</code> (empty cell) and given the integer <code>k</code>. You have to cut the pizza into <code>k</code> pieces using <code>k-1</code> cuts.&nb...
3
{ "code": "class Solution {\npublic:\n int ways(vector<string>& pizza, int k) {\n const int mod = 1e9 + 7;\n int m = pizza.size(), n = pizza[0].size();\n vector<vector<vector<int>>> f(m, vector<vector<int>>(n, vector<int>(k, -1)));\n vector<vector<int>> s(m + 1, vector<int>(n + 1));\n ...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
0
{ "code": "class Solution \n{\npublic:\n int maxPower(string s) \n {\n int c=0;\n int max=0;\n for(int i=0;i<s.length()-1;i++)\n {\n if(s[i]==s[i+1])\n {\n c++;\n }\n else\n {\n c=0;\n }\n...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
0
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n int max = 0;\n int curr = 0;\n char past = '0';\n for (char c : s) {\n if (c == past) {\n curr++;\n if (curr > max) {max = curr;}\n past = c;\n }\n ...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
0
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n int i=1;\n int count=1;\n int ma =1;\n while(i<s.length()){\n if(s[i-1]==s[i]){\n count++;\n }\n else{\n ma=max(ma,count);\n count=1;\n ...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
0
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n int ans = 1;\n int n = s.size();\n for (int i = 0; i < n; i++)\n {\n int j = i + 1;\n while (j < n && s[j] == s[j - 1]) j++;\n ans = max(ans, j - i);\n i = j - 1;\n }\n ...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
0
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n if(s.length()==1)return 1;\n int cnt=1;int maxi=0;\n for(int i=0;i<s.length()-1;i++){\n if(s[i]==s[i+1])cnt++;\n else cnt=1;\n maxi=max(maxi,cnt);\n }\n return maxi;\n }\n};", ...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
0
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n int max = 0;\n int curr = 0;\n char past = '0';\n for (char c : s) {\n if (c == past) {\n curr++;\n if (curr > max) {max = curr;}\n past = c;\n }\n ...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
0
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n int max_len = 1;\n int cur_len = 1;\n for (int i = 1; i < s.size(); i++)\n {\n if (s[i] == s[i-1]) cur_len++;\n else\n {\n if (cur_len > max_len) max_len = cur_len;\n ...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
2
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n int maxi=1;\n int count=1;\n \n for(int i=1;i<s.size();i++){\n char curr=s[i];\n char prev=s[i-1];\n \n if(prev==curr){\n count++;\n }\n else{\n maxi=max(maxi,count)...
1,542
<p>The <strong>power</strong> of the string is the maximum length of a non-empty substring that contains only one unique character.</p> <p>Given a string <code>s</code>, return <em>the <strong>power</strong> of</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>In...
2
{ "code": "class Solution {\npublic:\n int maxPower(string s) {\n int max = 0;\n int curr = 0;\n char prev = 0;\n for (char c : s)\n {\n if (prev != c)\n {\n max = std::max(max, curr);\n curr = 0;\n prev = c;\n ...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "class Solution {\npublic:\nint count=0;\n int goodNodes(TreeNode* root) {\n \n if (root==NULL)\n return count;\n //int count=1;\n count++;\n countg(root->left,root->val);\n countg(root->right,root->val);\n root->left=root->right=NULL;\n retu...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "class Solution {\npublic:\nint count=0;\n int goodNodes(TreeNode* root) {\n \n if (root==NULL)\n return count;\n //int count=1;\n count++;\n countg(root->left,root->val);\n countg(root->right,root->val);\n root->left=root->right=NULL;\n retu...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "class Solution {\n int c = 0;\n void dfs(TreeNode* root, int max) {\n if (!root) return;\n \n int currMax = max;\n if (root->val >= max) {\n c++;\n currMax = root->val;\n }\n\n int temp = currMax;\n dfs(root->left, currMax);\n\n ...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "class Solution {\npublic:\n int goodNodes(TreeNode* root) {\n return goodNodes(root, INT32_MIN);\n }\n int goodNodes(TreeNode* root, int maxyet){\n if (!root) return 0;\n int res = root->val >= maxyet;\n maxyet = max(maxyet, root->val);\n return res + goodNodes(r...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
1,544
<p>Given a binary tree <code>root</code>, a node <em>X</em> in the tree is named&nbsp;<strong>good</strong> if in the path from root to <em>X</em> there are no nodes with a value <em>greater than</em> X.</p> <p>Return the number of <strong>good</strong> nodes in the binary tree.</p> <p>&nbsp;</p> <p><strong clas...
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "//Approach1 : Recursion\n//\n// rootNode\n// |\n// V\n//Flow diagram : binaryTreePaths\n// |\n// V\n// call dfs with rootNode\n// path = \"\"\n// ...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "//Approach1 : Recursion\n//\n// rootNode\n// |\n// V\n//Flow diagram : binaryTreePaths\n// |\n// V\n// call dfs with rootNode\n// path = \"\"\n// ...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "class Solution {\npublic:\n void buildString(vector<int> &backtrack, vector<string> &ans) {\n string ans1 = \"\";\n for (int i = 0; i < backtrack.size(); i++) {\n ans1 += to_string(backtrack[i]);\n if (i != backtrack.size() - 1) {\n ans1 += \"->\";\n ...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "class Solution {\npublic:\n void recursively(TreeNode* root, vector<string>& paths, string path) {\n // If the node is a leaf, add the current path to the paths vector\n if (root->left == nullptr && root->right == nullptr) {\n path += to_string(root->val); // Add the leaf node ...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
0
{ "code": "class Solution {\npublic:\n vector<string> ans;\n\n // Helper function to find all root-to-leaf paths\n void rtl(TreeNode* root, vector<int>& v) {\n if (root == NULL) return; // Handle null root\n \n // Add the current node's value to the path\n v.push_back(root->val);\...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...
257
<p>Given the <code>root</code> of a binary tree, return <em>all root-to-leaf paths in <strong>any order</strong></em>.</p> <p>A <strong>leaf</strong> is a node with no children.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/paths-t...
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNod...