GeeksForGeeks - POTD | GFG POTD Answer
کانال بسته
1 218
مشترکین
اطلاعاتی وجود ندارد24 ساعت
-97 روز
-5730 روز
آرشیو پست ها
class Solution{
public:
int isSumProperty(Node *root)
{
if (!root || (!root->left && !root->right))
return true;
int l = root->left ? root->left->data : 0;
int r = root->right ? root->right->data : 0;
return (root->data == l + r) &&
isSumProperty(root->left) &&
isSumProperty(root->right);
}
};
8th February : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution{
public:
/*You are required to complete this method*/
int height(Node* root){
if(!root) return 0;
int left = height(root->left);
int right = height(root->right);
return max(left,right)+1;
}
bool solve(Node* root,int hgt,int cnt){
if(!root) return true;
if(!root->left && !root->right) return hgt == cnt;
return solve(root->left,hgt,cnt+1) && solve(root->right,hgt,cnt+1);
}
/*You are required to complete this method*/
bool check(Node *root)
{
//Your code here
int hgt = height(root);
int cnt = 1;
return solve(root,hgt,cnt);
}
};
7th February : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution{
public:
/* Should return minimum distance between a and b
in a tree with given root*/
int ans=0;
int solve(Node *root, int a, int b){
if(!root)
return 0;
int left=solve(root->left,a,b);
int right=solve(root->right,a,b);
if(root->data==a || root->data==b){
if(left|| right)
ans=max(left,right);
else
return 1;
}
else if(left&& right)
ans=left+right;
else if(left||right)
return max(left,right)+1;
return 0;
}
int findDist(Node* root, int a, int b) {
int a5s=solve(root,a,b);
return ans;
}
};
6th February : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution
{
public:
set ans;
void solve(Node *root, int lvl, vector v, int k) {
if(!root)
return;
v.push_back(root);
if(!root->left and !root->right)
{
int ind = lvl - k;
if(ind >= 0)
ans.insert(v[ind]);
return;
}
solve(root->left, lvl + 1, v, k);
solve(root->right, lvl + 1, v, k);
}
int printKDistantfromLeaf(Node* root, int k)
{
ans.clear();
vector v;
solve(root, 0, v, k);
return ans.size();
}
};
👨🏻💻If You Are Making A Website /App.
What Type Of Web / App You'll Make?
What Will Be The Main Features In It?
👨🏻💻 Comment Down ⬇️
5th February : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
class Solution {
public:
Node* sortedInsert(Node* head, int data) {
Node* newNode = new Node(data);
if (!head) {
newNode->next = newNode;
return newNode;
}
Node* current = head;
if (data < head->data) {
while (current->next != head)
current = current->next;
current->next = newNode;
newNode->next = head;
return newNode;
}
while (current->next != head && current->next->data < data)
current = current->next;
newNode->next = current->next;
current->next = newNode;
return head;
}
};public:
Node* sortedInsert(Node* head, int data) {
Node* newNode = new Node(data);
if (!head) {
newNode->next = newNode;
return newNode;
}
Node* current = head;
if (data < head->data) {
while (current->next != head)
current = current->next;
current->next = newNode;
newNode->next = head;
return newNode;
}
while (current->next != head && current->next->data < data)
current = current->next;
newNode->next = current->next;
current->next = newNode;
return head;
}
};class Solution {
public:
Node* sortedInsert(Node* head, int data) {
Node* newNode = new Node(data);
if (!head) {
newNode->next = newNode;
return newNode;
}
Node* current = head;
if (data < head->data) {
while (current->next != head)
current = current->next;
current->next = newNode;
newNode->next = head;
return newNode;
}
while (current->next != head && current->next->data < data)
current = current->next;
newNode->next = current->next;
current->next = newNode;
return head;
}
};
⚡REDEEM FAST GUYZ⚡
👉🏼Less Than 1000 Available
⭐REDEEM NOW⭐
class Solution {
public:
string convertToString(Node* head)
{
string s = "";
bool l = 0;
while(head)
{
if(head->data != 0)
l = 1;
if(l)
s += ('0' + head->data);
head = head->next;
}
return s;
}
Node* subLinkedList(Node* head1, Node* head2) {
string s1 = convertToString(head1), s2 = convertToString(head2);
if(s1.size() < s2.size())
swap(s1, s2);
else if((s1.size() == s2.size()) && s1 <= s2)
swap(s1, s2);
int n = s1.size(), m = s2.size();
int carry = 0;
int j = m - 1;
for(int i = n - 1; i >= 0; i--)
{
int val = (s1[i] - '0') - carry - ((j >= 0) ? (s2[j] - '0') : 0);
if(val < 0)
{
carry = 1;
s1[i] = ('0' + 10 + val);
}
else
{
carry = 0;
s1[i] = ('0' + val);
}
j--;
}
int i = 0;
while(i < n && s1[i] == '0')
i++;
LinkedList* ans = new LinkedList();
while(i < n)
ans->insert((s1[i++] - '0'));
if(ans->head == nullptr)
ans->insert(0);
return ans->head;
}
};
4th February : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
//User function Template for C++
class Solution {
public:
string convertToString(Node* head)
{
string s = "";
bool l = 0;
while(head)
{
if(head->data != 0)
l = 1;
if(l)
s += ('0' + head->data);
head = head->next;
}
return s;
}
Node* subLinkedList(Node* head1, Node* head2) {
string s1 = convertToString(head1), s2 = convertToString(head2);
if(s1.size() < s2.size())
swap(s1, s2);
else if((s1.size() == s2.size()) && s1 <= s2)
swap(s1, s2);
int n = s1.size(), m = s2.size();
int carry = 0;
int j = m - 1;
for(int i = n - 1; i >= 0; i--)
{
int val = (s1[i] - '0') - carry - ((j >= 0) ? (s2[j] - '0') : 0);
if(val < 0)
{
carry = 1;
s1[i] = ('0' + 10 + val);
}
else
{
carry = 0;
s1[i] = ('0' + val);
}
j--;
}
int i = 0;
while(i < n && s1[i] == '0')
i++;
LinkedList* ans = new LinkedList();
while(i < n)
ans->insert((s1[i++] - '0'));
if(ans->head == nullptr)
ans->insert(0);
return ans->head;
}
};
4th February : C++ Solution☝🏼
————————————————————
🙋🏻♂️Discussion ⁉️
Join ✅ @GFG_Answer
