Skip to content

Commit fdbf78d

Browse files
committed
GFG and Leetcode
1 parent 7f8b313 commit fdbf78d

8 files changed

+282
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
vector<int> bottomView(Node *root)
2+
{
3+
vector<int> ans;
4+
map<int, vector<int>> m;
5+
queue<pair<Node *, int>> q;
6+
int dist = 0;
7+
q.push(make_pair(root, 0));
8+
while (!q.empty())
9+
{
10+
pair<Node *, int> p = q.front();
11+
Node *curr = p.first;
12+
dist = p.second;
13+
m[dist].push_back(curr->data);
14+
q.pop();
15+
if (curr->left)
16+
{
17+
q.push(make_pair(curr->left, dist - 1));
18+
}
19+
if (curr->right)
20+
{
21+
q.push(make_pair(curr->right, dist + 1));
22+
}
23+
}
24+
map<int, vector<int>>::iterator itr;
25+
for (itr = m.begin(); itr != m.end(); itr++)
26+
{
27+
ans.push_back(itr->second.back());
28+
}
29+
return ans;
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
bool isCompleteBT(Node *root)
2+
{
3+
bool bt = false;
4+
queue<Node *> q;
5+
q.push(root);
6+
while (!q.empty())
7+
{
8+
Node *curr = q.front();
9+
q.pop();
10+
if (curr == nullptr)
11+
{
12+
bt = true;
13+
}
14+
else
15+
{
16+
if (bt == true)
17+
{
18+
return false;
19+
}
20+
else
21+
{
22+
q.push(curr->left);
23+
q.push(curr->right);
24+
}
25+
}
26+
}
27+
return true;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
class Solution
3+
{
4+
public:
5+
int canRepresentBST(int arr[], int N)
6+
{
7+
int mini = INT_MIN;
8+
int maxi1 = INT_MIN;
9+
int maxi2 = arr[0];
10+
for (int i = 1; i < N; ++i)
11+
{
12+
if (arr[i] < mini)
13+
{
14+
return 0;
15+
}
16+
if (arr[i] < maxi1)
17+
{
18+
return 0;
19+
}
20+
else if (arr[i] > arr[i - 1]) //right side
21+
{
22+
mini = arr[i - 1];
23+
if (arr[i] > maxi2)
24+
{
25+
maxi1 = max(maxi2, maxi1);
26+
maxi2 = arr[i];
27+
}
28+
}
29+
}
30+
return 1;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution
13+
{
14+
public:
15+
int sum = 0;
16+
void revin(TreeNode *root)
17+
{
18+
if (root == nullptr)
19+
{
20+
return;
21+
}
22+
else
23+
{
24+
revin(root->right);
25+
root->val = root->val + sum;
26+
sum = root->val;
27+
revin(root->left);
28+
}
29+
}
30+
31+
TreeNode *bstToGst(TreeNode *root)
32+
{
33+
revin(root);
34+
return root;
35+
}
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution
2+
{
3+
public:
4+
int maxDepth(TreeNode *root)
5+
{
6+
if (root == nullptr)
7+
{
8+
return 0;
9+
}
10+
// int l = 1+ maxDepth(root->left);
11+
// int r = 1 + maxDepth(root->right);
12+
13+
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
14+
}
15+
};
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution
13+
{
14+
public:
15+
int deepestLeavesSum(TreeNode *root)
16+
{
17+
int sum = 0, ans = 0;
18+
queue<TreeNode *> q;
19+
if (root == nullptr)
20+
{
21+
return ans;
22+
}
23+
q.push(root);
24+
while (!q.empty())
25+
{
26+
int sz = q.size();
27+
sum = 0;
28+
while (sz--)
29+
{
30+
TreeNode *curr = q.front();
31+
q.pop();
32+
sum += curr->val;
33+
if (curr->left)
34+
{
35+
q.push(curr->left);
36+
}
37+
if (curr->right)
38+
{
39+
q.push(curr->right);
40+
}
41+
}
42+
ans = sum;
43+
}
44+
return ans;
45+
}
46+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
11+
class Solution
12+
{
13+
public:
14+
TreeNode *ans;
15+
TreeNode *getTargetCopy(TreeNode *original, TreeNode *cloned, TreeNode *target)
16+
{
17+
if (target == nullptr || cloned == nullptr)
18+
{
19+
return nullptr;
20+
}
21+
22+
if (cloned->val == target->val)
23+
{
24+
ans = cloned;
25+
}
26+
getTargetCopy(original, cloned->left, target);
27+
getTargetCopy(original, cloned->right, target);
28+
return ans;
29+
}
30+
};
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
// #pragma GCC optimize("Ofast")
13+
// #pragma GCC target("avx,avx2,fma")
14+
// static auto _ = [] ()
15+
// {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();
16+
class Solution
17+
{
18+
public:
19+
int rangeSumBST(TreeNode *root, int low, int high)
20+
{
21+
// int ans =0;
22+
// queue<TreeNode*> q;
23+
// if(root == nullptr)
24+
// {
25+
// return ans;
26+
// }
27+
// q.push(root);
28+
// while(!q.empty())
29+
// {
30+
// int sz = q.size();
31+
// while(sz--)
32+
// {
33+
// TreeNode* curr = q.front();
34+
// if(curr->val >=low and curr->val <=high)
35+
// {
36+
// ans += curr->val;
37+
// }
38+
// q.pop();
39+
// if(curr->left)
40+
// {
41+
// q.push(curr->left);
42+
// }
43+
// if(curr->right)
44+
// {
45+
// q.push(curr->right);
46+
// }
47+
// }
48+
// }
49+
// return ans;
50+
// }
51+
int ans = 0;
52+
if (root == nullptr)
53+
{
54+
return ans;
55+
}
56+
if (root->val <= high && root->val >= low)
57+
{
58+
ans += root->val;
59+
}
60+
ans += rangeSumBST(root->left, low, high);
61+
ans += rangeSumBST(root->right, low, high);
62+
root->left = root->right = nullptr;
63+
return ans;
64+
}
65+
};

0 commit comments

Comments
 (0)