GeeksForGeeks - POTD | GFG POTD Answer
Closed channel
1 218
Subscribers
No data24 hours
-97 days
-5730 days
Posts Archive
class Solution{
public:
// Your are required to complete this function
// function should return root of the modified BST
vector<Node*> nums;
void inorder(Node* root){
if(!root) return;
inorder(root->left);
nums.push_back(root);
inorder(root->right);
}
Node* buildBST(int l, int r, vector<Node*>& nums){
if(l>r) return NULL;
int m = (l+r)/2;
nums[m]->left = buildBST(l, m-1, nums);
nums[m]->right = buildBST(m+1, r, nums);
return nums[m];
}
Node* buildBalancedTree(Node* root)
{
inorder(root);
return buildBST(0, nums.size()-1, nums);
}
};
β¨π°π°π°π°π°π°π°β¨
β¨π°COURSES π°β¨
β¨π°π°π°π°π°π°π°β¨
1β£ D0T \/\/ΔB Dβ¬V
2β£ $uprβ¬mβ¬ DSβ
3β£ P\/\/ Wβ¬B Dβ¬V
4β£ βLPHβ B@tch
5β£ UDMY βNDR01D Dβ¬V
β οΈHow To Access Linkβ οΈ
β¨SHARE WITH YOUR FRIENDSβ¨
Repost from [MOOCs] Free Courses & Certificate
π° Python In Hindi π°
ππΌ Free Lectures β
βFree Certificateβ
πLink - https://linkpays.in/Python-Hindi
(How To Access The Link)
14th October : C++ Solution βπΌ
βββββββββββββββββββββ
ππ»ββοΈDiscussion βοΈ
Join β
@GFG_Answer
class Solution
{
public:
unordered_setst1;
vectorans;
void inorder(Node *root, int num){
if(!root){return;}
inorder(root->left, num);
if(num==0){st1.insert(root->data);}
if(num==1){
if(st1.find(root->data)!=st1.end()){
ans.push_back(root->data);
}
}
inorder(root->right, num);
}
vector findCommon(Node *root1, Node *root2)
{
inorder(root1, 0);
inorder(root2, 1);
return ans;
}
};
I Will Post One MOOCs Link Daily.
π°Free Course + Certificateπ°
ππΌ Do You Want This βοΈ
β¨ Free MOOCs Platform β¨
β 300,000+ Courses & Free Certificates
πLink - Mind Luster
π°One Of The Best MOOCs Siteπ°
β¨ Free MOOCs Platform β¨
β 300,000+ Courses & Free Certificate
πLink - Mind Luster
π°One Of The Best MOOCs Siteπ°
13th October : C++ Solution βπΌ
βββββββββββββββββββββ
ππ»ββοΈAny Query βοΈ
Join Us β
- @GFG_Answer
β We Are 200 Now β
class Solution{
public:
int out;
void inOrder(Node* node, int x){
if(!node)
return;
inOrder(node->left, x);
if(node->data <= x)
out = node->data;
else
return;
inOrder(node->right, x);
}
int floor(Node* root, int x) {
out = -1;
inOrder(root, x);
return out;
}
};
12th October : C++ Solution βπΌ
βββββββββββββββββββββ
ππ»ββοΈDiscussion βοΈ
Join β
@GFG_Answer
β οΈ Get Free T-Shirtπ β οΈ
β‘ T.me/GeeksForGeeks_POTD/437
class Solution {
public:
unordered_map mp;
string solve(Node* node)
{
if(node==NULL)return "";
string ans="";
string l=solve(node->left);
string r=solve(node->right);
ans=l+"#"+to_string(node->data)+"#"+r;
if(node->left || node->right)mp[ans]++;
return ans;
}
int dupSub(Node *root) {
string x=solve(root);
for(auto o:mp)
{
if(o.second>1)
{
return true;
}
}
return false;
}
};
So, 555 Subs Pe -
β¨β‘PW WE DEVβ‘β¨
Upload KareβοΈ
11th October : C++ Solution βπΌ
βββββββββββββββββββββ
ππ»ββοΈDiscussion βοΈ
Join β
@GFG_Answer
β οΈ Get Free T-Shirtπ β οΈ
β‘ T.me/GeeksForGeeks_POTD/437
class Solution{
public:
//Function to check whether a binary tree is balanced or not.
int height(Node *root)
{
if(root == NULL) return 0;
return max(height(root -> left), height(root -> right)) + 1;
}
bool isBalanced(Node *root)
{
if(root == NULL) return true;
if(isBalanced(root -> left) && isBalanced(root -> right))
{
if(abs(height(root -> left) - height(root -> right)) <= 1) return true;
}
return false;
}
};
10th October : C++ Solution βπΌ
βββββββββββββββββββββ
ππ»ββοΈDiscussion βοΈ
Join β
@GFG_Answer
class Solution
{
private:
void atLevelK(Node* root, int k, vector &ans){
if(root){
if(k==0) ans.push_back(root->data);
atLevelK(root->left,k-1,ans);
atLevelK(root->right,k-1,ans);
}
}
int helper(Node* root, int k, vector &ans, int target, bool &t){
if(root==NULL) return -1;
if(!t and root->data==target){
atLevelK(root,k,ans);
t = true;
return 1;
}
int l = helper(root->left,k,ans,target,t);
int r = helper(root->right,k,ans,target,t);
if(l==-1 and r==-1) return -1;
else if(r==-1){
if(k-l==0){
ans.push_back(root->data);
return -1;
}
else atLevelK(root->right,k-l-1,ans);
return l+1;
}
else{
if(k-r==0){
ans.push_back(root->data);
return -1;
}
else atLevelK(root->left,k-r-1,ans);
return r+1;
}
}
public:
vector KDistanceNodes(Node* root, int target , int k){
vector ans;
bool t = false;
helper(root,k, ans,target,t);
sort(ans.begin(),ans.end());
return ans;
}
};
9th October : C++ Solution βπΌ
βββββββββββββββββββββ
ππ»ββοΈDiscussion βοΈ
Join β
@GFG_Answer
class Solution{
public:
//Function to find the height of a binary tree.
int max_h = 0;
int solve(struct Node* node, int h){
if(node->right == NULL && node->left == NULL)return max_h = max(max_h, h);
else if(node->right == NULL){
solve(node->left, h+1);
}
else if(node->left == NULL){
solve(node->right, h+1);
}
else{
solve(node->right, h+1);
solve(node->left, h+1);
}
}
int height(struct Node* node){
if(!node)return 0;
max_h = solve(node, 1);
return max_h;
}
};
