id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n long long dp[505][505];\n long long mod = 1e9 + 7;\n long long solve(long n, long p) {\n if(n == 0 && p == 0)\n return 1;\n if(dp[n][p] != -1)\n return dp[n][p];\n long long ans = 0;\n if(n > 0) {\n ans = (ans + (n * ... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int mod = 1e9+7;\n\n vector<long long>fact;\n\n long long f(int n){\n if(n == 0) return fact[n] = 1;\n return fact[n] = (n * (f(n-1)%mod))%mod;\n }\n\n long long dp[502][502];\n\n long long solve(int p,int d, int n){\n if(p == 0) return dp[... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "typedef long long ll;\nvector<vector<ll>> dp;\nconstexpr int N = 500;\nconstexpr ll MOD = 1e9 + 7;\nclass Solution {\npublic:\n int countOrders(int n) {\n if (dp.empty()) {\n dp = vector<vector<ll>>(N + 1, vector<ll>(N + 1));\n dp[0][0] = 1;\n for (int j = 1; j <=... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\nprivate:\n int MOD = 1e9 + 7;\n vector<vector<int>> memo;\n \n long totalWays(int unpicked, int undelivered) {\n if (unpicked == 0 && undelivered == 0) {\n // We have completed all orders.\n return 1;\n }\n \n if (unpicked < 0 ... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n\n int factorial(int x, int modulo)\n {\n long long result = 1;\n\n for(int i = 2; i <= x; i++)\n result = (result * i) % modulo;\n\n return result;\n }\n\n int countOrdersRec(int pickupUsed, int deliveryUsed, int n, vector<vector<int>>... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int mod = 1000000007;\n long long rec(long long pick,long long deliv,int &n,vector<vector<int>>&dp){\n if(deliv==n)return 1;\n if(dp[pick][deliv]!=-1) return dp[pick][deliv];\n if(pick==deliv){\n return dp[pick][deliv]= (long long)((n-pick)*... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int mod = 1000000007;\n long long rec(int pick,int deliv,int &n,vector<vector<int>>&dp){\n if(deliv==n)return 1;\n if(dp[pick][deliv]!=-1) return dp[pick][deliv];\n if(pick==deliv){\n return dp[pick][deliv]= (long long)((n-pick)*rec(pick+1,d... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int countOrders(int n) {\n \n vector<vector<int>> dp(n+1,vector<int> (n+1,-1));\n int mod = 1e9+7;\n function<int(int,int)> rec = [&](int op,int cl){\n if(op+cl==0)return 1;\n if(dp[op][cl]!=-1)return dp[op][cl];\n ... |
1,461 | <p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>
<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). </p>
<p>Since the answer may be too large, return it modulo 10^9 + 7.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int MOD = 1e9 + 7;\n \n int countOrders(int n) {\n vector<vector<long>> dp (n + 1, vector<long>(n + 1, 0));\n\n for (int unpicked = 0; unpicked <= n; unpicked++) {\n for (int undelivered = unpicked; undelivered <= n; undelivered++) {\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 0 | {
"code": "class Solution {\npublic:\n int n;\n int parent[10004];\n\n int findParent(int node) {\n if(node == parent[node]) return node;\n return parent[node] = findParent(parent[node]);\n }\n\n void Union(int u , int v) {\n int parent_u = findParent(u);\n int parent_v = fi... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 0 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<bool> visited(n, false);\n\n for(int i=0;i<n;i++){\n if(leftChild[i] != -1){\n if(visited[leftChild[i]]) return false;\n visi... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 0 | {
"code": "class Solution {\npublic:\n int components;\n vector<int>parent;\n int find(int node){\n if(parent[node]==node){\n return node;\n }\n return parent[node]=find(parent[node]);\n }\n bool uni(int par,int child){\n \n if(find(child)!=child){\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 0 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> inDegree(n, 0);\n\n for (int i = 0; i < n; i++) {\n if (leftChild[i] != -1) {\n inDegree[leftChild[i]]++;\n if (inDegree... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 0 | {
"code": "class Solution {\npublic:\n int countNodes(vector<int> &l,vector<int> &r,int root) // DFS from root to validate that all nodes are visited.\n {\n if(root==-1)\n return 0;\n return 1+countNodes(l,r,l[root])+countNodes(l,r,r[root]);\n }\n bool validateBinaryTreeNodes(int... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 0 | {
"code": "class Solution {\n\nprivate:\n vector<int> parent, size;\n \n int Find(int node){\n if (parent[node] == node) return node;\n return parent[node] = Find(parent[node]);\n }\n\n void Union(int node1, int node2){\n int root1 = Find(node1);\n int root2 = Find(node2);\n... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class DisjointSet {\n\npublic:\n vector<int> rank, parent, size;\n DisjointSet(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n in... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class DisjointSet {\n\npublic:\n vector<int> rank, parent, size;\n DisjointSet(int n) {\n rank.resize(n + 1, 0);\n parent.resize(n + 1);\n size.resize(n + 1);\n for (int i = 0; i <= n; i++) {\n parent[i] = i;\n size[i] = 1;\n }\n }\n\n in... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class disjoint{\n public:\n vector<int> parent;\n vector<int> size;\n disjoint(int n){\n parent.resize(n+1);\n size.resize(n+1,1);\n for(int i=0;i<=n;i++){\n parent[i]=i;\n }\n }\n int par(int u){\n if(parent[u]==u) return u;\n return p... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class disjoint{\n public:\n vector<int> parent;\n vector<int> size;\n disjoint(int n){\n parent.resize(n+1);\n size.resize(n+1,1);\n for(int i=0;i<=n;i++){\n parent[i]=i;\n }\n }\n int par(int u){\n if(parent[u]==u) return u;\n return p... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool isBinaryTreeValid(int current, vector<int>& leftChild,\n vector<int>& rightChild, vector<bool>& visited) {\n\n if (leftChild[current] != -1) {\n if (visited[leftChild[current]]){ \n return false;\n }\n... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class DisjointSet {\nprivate:\n vector<int> parents;\n int components;\n\npublic:\n DisjointSet(int n) : parents(n), components(n) {\n for (int i = 0; i < n; i++) {\n parents[i] = i;\n }\n }\n\n int find(int x) {\n if (parents[x] == x) return x;\n\n ret... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<bool> isRoot(n, true);\n for (int child : leftChild) {\n if (child != -1) isRoot[child] = false;\n }\n\n for (int child: rightChild) {\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n int findParent(int x, vector<int> &parent) {\n if (x == parent[x]) {\n return x;\n }\n\n return findParent(parent[x], parent);\n }\n\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n int cnt... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n // The graph is a tree when:\n // 1. does not contain cycle\n // 2. two path does not lead to same node\n // 2. does not have more than one component\n\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n \n // parent[i] -> parent, i -> child\n vector<int> parent(n, -1);\n\n for(int i = 0; i < n; i++){\n\n int node = i;\n int left = leftChi... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "/*\nA binary tree is valid if:\n- Single component\n- No cycle.\n\n- only one root(checked by single component hence not necessary).\n- every node has only one parent (not necessary -> checked by cycle itself).\n\nWe will do bfs after finding the root to check the above two conditions.\n\n*/\n\nclass Solut... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n \n // parent[i] -> parent, i -> child\n vector<int> parent(n, -1);\n\n for(int i = 0; i < n; i++){\n\n int node = i;\n int left = leftChi... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\nbool dfs(int node,vector<int>&vis,vector<int>&path,vector<int>adj[])\n{\n vis[node]=1;\n path[node]=1;\n\n for(auto it: adj[node])\n {\n if(!vis[it])\n {\n\n if(dfs(it,vis,path,adj)==true)\n return true;\n }\n else\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool dfs(int node,vector<int>&visited,vector<int>adj[]){\n visited[node]=1;\n for(auto it:adj[node]){\n if(!visited[it]){\n if(!dfs(it,visited,adj))return false;\n }else return false;\n }\n return true;\n }\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool dfs(int node,vector<int>&parent,vector<int>&visited,vector<int>adj[]){\n visited[node]=1;\n for(auto it:adj[node]){\n if(!visited[it]){\n //parent[it]=node;\n if(!dfs(it,parent,visited,adj))return false;\n }else r... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n unordered_map<int, int> nodeToParent;\n for(int i = 0; i < n; ++i) {\n if(i == leftChild[i] || i == rightChild[i])\n return false;\n if(... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n unordered_map<int, int> nodeToParent;\n for(int i = 0; i < n; ++i) {\n if(i == leftChild[i] || i == rightChild[i])\n return false;\n if(... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild)\n {\n vector<int>adj[n];\n vector<int>visit(n,0);\n vector<int>indegree(n,0);\n for(int i=0;i<n;i++)\n {\n if(leftChild[i]!... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 1 | {
"code": "class Solution {\npublic:\n \n bool isValidTree(int root, vector<int> &leftChild, vector<int> &rightChild) {\n queue<int> q {};\n set<int> st {};\n q.push(root);\n st.insert(root);\n \n while(q.size()) {\n int top = q.front();\n q.pop();... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\n private:\n bool hasCycle(int node, vector<int>& lC, vector<int>& rC, vector<bool>& vis) {\n\n if (node == -1) return false;\n if (vis[node]) return true;\n vis[node] = true;\n\n bool leftCycle = hasCycle(lC[node], lC, rC, vis);\n bool rightCycle =... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n // time/space: O(n)/O(n)\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n // find the parent for each node\n unordered_map<int, int> node2parent;\n for (int node = 0; node < n; node++) {\n if (leftCh... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "// Legenda\n#include<bits/stdc++.h>\nusing namespace std;\nbool isPalindrome(string S)\n{\n // Stores the reverse of the\n // string S\n string P = S;\n \n // Reverse the string P\n reverse(P.begin(), P.end());\n \n // If S is equal to P\n if (S == P) {\n // Return \"Yes\"\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "\nclass Solution\n{\npublic:\n vector<int> vis;\n\n bool dfs(int u, vector<int> &lefc, vector<int> &rc){\n if(u == -1)return false;\n if(vis[u])return true;\n vis[u] = 1;\n return dfs(lefc[u], lefc, rc) || dfs(rc[u], lefc, rc);\n }\n\n bool validateBinaryTreeNodes(in... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class DisjointSet { \npublic:\n vector<int> rank, parent, size;\n DisjointSet(int n) {\n rank.resize(n+1, 0); \n parent.resize(n+1);\n size.resize(n+1); \n for(int i = 0;i<=n;i++) {\n parent[i] = i; \n size[i] = 1; \n }\n }\n\n int findU... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n int countNode(int &root, vector<int> &leftChild, vector<int> &rightChild){\n queue<pair<int, int>> traversal;\n traversal.push({root, -1});\n int count = 0;\n while(!traversal.empty()){\n int size = traversal.size();\n while(s... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> ind(n, 0);\n\n vector<vector<int>> adj(n);\n \n for (int i = 0; i < n; i++) {\n if (leftChild[i] != -1) {\n ind[leftChild[... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> visited(n, 0);\n for (int i = 0; i < n; i++) {\n if (visited[i] == 0) {\n visited[i] = 1;\n queue<int> q;\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<vector<int>> adj(n);\n vector<int> indegree(n,0);\n for(int i=0;i<leftChild.size();i++){\n if(leftChild[i]!=-1){\n adj[i].push_back(l... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\nbool dfsCheck(int node, vector<vector<int>>&adj,vector<int>&vis) {\n\t\tvis[node] = 1;\n\t\tfor (auto it : adj[node]) {\n\t\t\tif (!vis[it]) {\n\t\t\t\tif (dfsCheck(it, adj, vis) == true)\n\t\t\t\t\treturn true;\n\t\t\t}\n\t\t\telse{\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\t\tret... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n unordered_map<int, int> parent;\n\n bool findParent(int n, vector<int>& leftChild, vector<int>& rightChild) {\n #pragma unroll\n for (int i=0; i < n; i++) {\n int left = leftChild[i], right=rightChild[i];\n if (left != -1) {\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n // binary tree - root node has no parent. all other nodes have 1 parent. \n vector<int> inDegree(n, 0);\n vector<vector<int>> g(n);\n for(int i=0; i<n; i++){\n... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild,\n vector<int>& rightChild) {\n unordered_map<int, int> inDegree;\n for (int i = 0; i < n; i++) {\n if (leftChild[i] != -1)\n inDegree[leftChild[... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\nbool dfsCheck(int node, vector<vector<int>>&adj,vector<int>&vis, vector<int>&pathVis) {\n\t\tvis[node] = 1;\n\t\tpathVis[node] = 1;\n\n\t\t// traverse for adjacent nodes\n\t\tfor (auto it : adj[node]) {\n\t\t\t// when the node is not visited\n\t\t\tif (!vis[it]) {\n\t\t\t\tif (df... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n unordered_map<int,int> mp;\n vector<int>adj[n];\n for(int i=0;i<n;i++)\n {\n if(leftChild[i]!=-1)\n {\n adj[i].push_back(l... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> inDeg(n,0);\n vector<int> adj[n];\n for(int i=0;i<n;i++){\n if(leftChild[i]!=-1){\n inDeg[leftChild[i]]++; \n ad... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n unordered_set<int> st; //child\n // st.insert(0);\n // for(int i=0;i<n;i++){\n // if(leftChild[i] != -1){\n // if(st.find(leftChild[i]) != s... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n int dfs(int node,vector<int>adj[],int par){\n int ans=1;\n for(auto j:adj[node]){\n if(j!=par){\n ans+=dfs(j,adj,node);\n }\n }\n return ans;\n }\n bool validateBinaryTreeNodes(int n, vector<int>& leftChil... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<int> adj[], vector<int>& vis) {\n vis[node] = 1;\n for (auto it : adj[node]) {\n if (!vis[it]) {\n dfs(it, adj, vis);\n }\n }\n }\n // Cycle detection in a directed graph\n bool iscycle(int... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> par(n,-1);\n vector<vector<int>> g(n);\n\n for(int i = 0 ; i < n ; i++){\n if(leftChild[i]!=-1 && par[leftChild[i]] != -1) return false;\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int u, vector<int>adj[], vector<int>&vis){\n vis[u]=1;\n for(auto it:adj[u]){\n if(!vis[it]) dfs(it, adj, vis);\n }\n }\n\nbool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> adj[n];\n vector<int> in... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool bfs(vector<int> adj[], vector<bool> &visited, int curr)\n {\n if(visited[curr]) return false;\n visited[curr]= true;\n for( auto x: adj[curr])\n {\n bool ret = bfs(adj, visited, x);\n if(ret==false)\n {\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<int> adj[], vector<int> &vis){\n vis[node]=1;\n for(auto it:adj[node]){\n if(!vis[it]) dfs(it, adj, vis);\n }\n }\n bool validateBinaryTreeNodes(int n, vector<int>& l, vector<int>& r) {\n vector<int> adj[n... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, \n vector<int>& rightChild) {\n int root = findRoot(n, leftChild, rightChild);\n if (root == -1) {\n return false;\n }\n\n vector<int> vis(n, 0);\... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "struct Tree{\n int val,p;\n Tree* left;\n Tree* right;\n Tree(int x, int y, Tree* l, Tree* r): val(x), p(y), left(l), right(r) {}\n \n};\nclass Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<Tree*>a;\n in... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n void dfs(int node, vector<int> adj[], vector<int>& vis) {\n vis[node] = 1;\n for (auto it : adj[node]) {\n if (!vis[it]) {\n dfs(it, adj, vis);\n }\n }\n }\n // Cycle detection in a directed graph\n bool iscycle(int... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool ans=1;\n void dfs(int i,vector<int>&vis,vector<int>adj[]){\n vis[i]=1;\n for(auto it:adj[i]){\n if(!vis[it]){\n dfs(it,vis,adj);\n }else{\n ans=0;\n }\n }\n }\n bool validateBina... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\nprivate:\n bool dfs(int node,vector<int>& leftChild,vector<int>& rightChild,vector<bool>& vis){\n if(node==-1) return true;\n if(vis[node]) return false;\n vis[node]=true;\n return dfs(leftChild[node],leftChild,rightChild,vis) && dfs(rightChild[node],leftChi... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool ans = 1;\n vector<int> topo(int V, vector<int> adj[],vector<int>&ans) \n\t{\n\t vector<int>indeg(V,0);\n\t for(int i = 0; i < V; i++){\n\t for(auto x : adj[i]){\n\t indeg[x]++;\n\t }\n\t }\n\t queue<int>q;\n\t for(int i = 0; i <... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\n public:\n bool ans = 1;\n vector<int> topo(int V, vector<int> adj[],vector<int>&ans) \n \t{\n \t vector<int>indeg(V,0);\n \t for(int i = 0; i < V; i++){\n \t for(auto x : adj... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Tree{\npublic:\n int val;\n Tree *left;\n Tree *right;\n Tree* parent;\n\n Tree(int n){\n val = n;\n left = nullptr;\n right = nullptr;\n parent = nullptr;\n }\n};\n\nclass Solution {\n\nbool dfs(vector<bool> &vis, Tree* node){\n if(!node)\n ret... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n int findRoot(int n, vector<int>& leftChild, vector<int>& rightChild){\n unordered_set<int> set;\n set.insert(leftChild.begin(), leftChild.end());\n set.insert(rightChild.begin(), rightChild.end());\n for(int i=0;i<n;i++){\n if(set.find(i... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool bfs(map<int, vector<int>>& adj, int root, int n) {\n queue<int> q;\n vector<bool> visited(n, false);\n q.push(root);\n visited[root] = true;\n int count = 0;\n \n while (!q.empty()) {\n int u = q.front();\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n int root = findRoot(n, leftChild, rightChild);\n if (root == -1) {\n return false;\n }\n \n unordered_set<int> seen;\n stack<int> stac... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "class Solution {\npublic:\n int findroot(vector<int>& lc , vector<int>& rc , int n){\n int root = -1 ;\n int cnt = 0 ;\n unordered_set<int> ch ;\n ch.insert(lc.begin() , lc.end() ) ;\n ch.insert(rc.begin() , rc.end()) ;\n\n for(int i = 0 ; i < n ; i++){\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 2 | {
"code": "int findRoot(int n, const vector<int>& leftChild, const vector<int>& rightChild) {\n unordered_set<int> s;\n s.insert(leftChild.begin(), leftChild.end());\n s.insert(rightChild.begin(), rightChild.end());\n for (int i = 0; i < n; ++i) {\n if (s.count(i) == 0) return i;\n }\n return... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(int n, vector<vector<int>>&vec,int i,vector<bool>&vis){\n vis[i]=true;\n for(int j=0;j<vec[i].size();j++){\n if(vis[vec[i][j]])\n return false;\n if(!dfs(n,vec,vec[i][j],vis))\n return false;\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(int n, vector<vector<int>>&vec,int i,vector<bool>&vis){\n vis[i]=true;\n for(int j=0;j<vec[i].size();j++){\n if(vis[vec[i][j]])\n return false;\n if(!dfs(n,vec,vec[i][j],vis))\n return false;\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n\n\n bool bfs(int src,vector<int> & parent,vector<int>& vis,vector<int> adj[])\n {\n queue<int> q;\n vis[src] = 1;\n parent[src] = -1;\n q.push(src);\n while(!q.empty())\n {\n int s = q.front(); q.pop();\n\n fo... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "#include <vector>\n#include <deque>\n#include <unordered_set>\n\nclass Solution {\npublic:\n bool validateBinaryTreeNodes(int n, std::vector<int>& leftChild, std::vector<int>& rightChild) {\n std::deque<int> queue; // Create a queue for BFS traversal.\n std::unordered_set<int> visited; // ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int>vis(n,0);\n vector<int>pathvis(n,0);\n vector<vector<int>>graph(n);\n vector<int>indegree(n,0);\n for(int i=0;i<n;i++)\n {\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int>vis(n,0);\n vector<int>pathvis(n,0);\n vector<vector<int>>graph(n);\n vector<int>indegree(n,0);\n for(int i=0;i<n;i++)\n {\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "// Approach 2: Breadth First Search (BFS)\nclass Solution {\npublic:\n int findRoot(int n, vector<int> leftChild, vector<int> rightChild) {\n unordered_set<int> children;\n children.insert(leftChild.begin(), leftChild.end());\n children.insert(rightChild.begin(), rightChild.end());\... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n\n bool dfs(TreeNode* &root, vector<int> &leftChild, vector<int> &rightChild, unordered_map<int, int> &myMap, int &n) {\n if (root == NULL) return true;\n\n if (leftChild[root->val] != -1) {\n --n;\n myMap[leftChild[root->val]]++;\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n //adjacency list traverse ;\n unordered_map<int,vector<int>> adj;\n //child to parent store;\n //i. e. to identify who the parent of current child and make nec... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n //adjacency list traverse ;\n unordered_map<int,vector<int>> adj;\n //child to parent store;\n //i. e. to identify who the parent of current child and make nec... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n //adjacency list traverse ;\n unordered_map<int,vector<int>> adj;\n //child to parent store;\n //i. e. to identify who the parent of current child and make nec... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n // Traversal...\n unordered_map<int, vector<int>> par2child;\n unordered_map<int, int> child2par; // Node mapping to find root...\n for(int i = 0; i<n; i++){ /... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n unordered_map<int,int>child_to_parent;\n unordered_map<int,vector<int>>adj;\n for(int i=0;i<n;i++){\n if(leftChild[i]!=-1){\n adj[i].push_ba... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& left, vector<int>& right) {\n vector<int>adj[n]; set<int>s;\n for(int i=0;i<left.size();i++){\n if(left[i]==-1) continue;\n adj[i].push_back(left[i]);\n }\n for(int i=0;i<right.... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n vector<list<int>> graph;\n vector<int> visited;\n bool isCycle;\n void dfs(int src){\n visited[src] = 1;\n for(auto &neigh : graph[src]){\n if(!visited[neigh]){\n dfs(neigh);\n }\n else{\n i... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool dfs(vector<vector<int>>& graph, int src, vector<bool>& vis, vector<bool>& inPath) {\n vis[src] = true;\n inPath[src] = true; // Mark the node as being in the current path\n \n for (auto nbr : graph[src]) {\n if (inPath[nbr]) return ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n vector<list<int>> graph;\n vector<int> visited;\n bool isCycle;\n void dfs(int src){\n visited[src] = 1;\n for(auto &neigh : graph[src]){\n if(!visited[neigh]){\n dfs(neigh);\n }\n else{\n i... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> indegree(n,0);\n vector<int> adj[n];\n int sum = 0;\n int cnt = 0;\n unordered_map<int,int> parent;\n queue<int> q;\n vector<i... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n \n unordered_map<int, list<int>> adjList;\n unordered_map<int, int> childParentMap;\n for(int i = 0; i < n; ++i)\n {\n int root = i;\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(int src, int& cnt, unordered_map<int, list<int>>& adj) {\n cnt++;\n \n for(auto nbr : adj[src]) {\n dfs(nbr, cnt, adj);\n }\n }\n\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool bfs(map<int,vector<int>>&adj,int root,int n)\n {\n queue<int> q;\n q.push(root);\n int count=0;\n while(!q.empty())\n {\n int u=q.front();\n q.pop();\n count++;\n for(auto v:adj[u])\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n map<int,int> ctp;\n map<int,vector<int>> mp;\n for(int i=0 ;i<n;i++){\n int lc = leftChild[i];\n int rc = rightChild[i];\n if(lc!=-1)... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\nprivate:\n bool solve(int node,vector<int> adj[],vector<int>& visited)\n {\n visited[node]=1;\n for(auto it:adj[node])\n {\n if(!visited[it])\n {\n if(solve(it,adj,visited)==false) return false;\n }\n el... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\nvoid dfs(vector<vector<int>>& adj, vector<bool>& visited, int key, bool& check, int& node)\n{\n\tvisited[key] = true;\n\t++node;\n\tfor (auto it : adj[key])\n\t{\n\t\tif (!visited[it])\n\t\t\tdfs(adj, visited, it, check, node);\n\t\telse\n\t\t{\n\t\t\tcheck = false;\n\t\t\treturn... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\nvoid dfs(vector<vector<int>>& adj, vector<bool>& visited, int key, bool& check, int& node)\n{\n\tvisited[key] = true;\n\t++node;\n\tfor (auto it : adj[key])\n\t{\n\t\tif (!visited[it])\n\t\t\tdfs(adj, visited, it, check, node);\n\t\telse\n\t\t{\n\t\t\tcheck = false;\n\t\t\treturn... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n int solve(int n,vector<int>& left,vector<int>& right){\n unordered_set<int> children;\n children.insert(left.begin(), left.end());\n children.insert(right.begin(), right.end());\n \n for (int i = 0; i < n; i++) {\n if (children... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n void dfsTraversal(int node, unordered_map<int, list<int>> &adjList, unordered_map<int, bool> &vis, stack<int> &topo) {\n vis[node] = true;\n\n for (auto nbr : adjList[node]) {\n if (!vis[nbr])\n dfsTraversal(nbr, adjList, vis, topo);\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n void dfsTraversal(int node, unordered_map<int, list<int>> &adjList, unordered_map<int, bool> &vis, stack<int> &topo) {\n vis[node] = true;\n\n for (auto nbr : adjList[node]) {\n if (!vis[nbr])\n dfsTraversal(nbr, adjList, vis, topo);\n ... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n\n int traverse(int idx, vector<int>& vis, vector<vector<int>>& adj, int par) {\n\n if(vis[idx])\n return 0;\n \n vis[idx] = 1;\n int ans = 1;\n\n for(auto it: adj[idx]) {\n \n if(it == par)\n c... |
1,275 | <p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary... | 3 | {
"code": "class Solution {\npublic:\n\n void dfs(int curr,vector<int> &visited,vector<vector<int>> &adjList){\n\n for(auto x:adjList[curr]){\n if(!visited[x]){\n visited[x]=1;\n dfs(x,visited,adjList);\n }\n }\n \n }\n\n bool validateB... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.