Skip to content

Commit 1f59fbe

Browse files
committed
practise 02-Jun-2020
1 parent f6e10ae commit 1f59fbe

6 files changed

+285
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Invert a binary tree.
2+
3+
Example:
4+
5+
Input:
6+
7+
4
8+
/ \
9+
2 7
10+
/ \ / \
11+
1 3 6 9
12+
Output:
13+
14+
4
15+
/ \
16+
7 2
17+
/ \ / \
18+
9 6 3 1
19+
Trivia:
20+
This problem was inspired by this original tweet by Max Howell:
21+
22+
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
23+
24+
25+
26+
27+
/**
28+
* Definition for a binary tree node.
29+
* struct TreeNode {
30+
* int val;
31+
* TreeNode *left;
32+
* TreeNode *right;
33+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
34+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
35+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
36+
* };
37+
*/
38+
class Solution {
39+
public:
40+
TreeNode* invertTree(TreeNode* root) {
41+
if(!root) return root;
42+
queue<TreeNode*>q;
43+
q.push(root);
44+
while(!q.empty()){
45+
TreeNode* cur = q.front();
46+
q.pop();
47+
swap(cur->left, cur->right);
48+
if(cur->left) q.push(cur->left);
49+
if(cur->right) q.push(cur->right);
50+
}
51+
return root;
52+
}
53+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
2+
3+
Given linked list -- head = [4,5,1,9], which looks like following:
4+
5+
6+
7+
8+
9+
Example 1:
10+
11+
Input: head = [4,5,1,9], node = 5
12+
Output: [4,1,9]
13+
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
14+
Example 2:
15+
16+
Input: head = [4,5,1,9], node = 1
17+
Output: [4,5,9]
18+
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
19+
20+
21+
Note:
22+
23+
The linked list will have at least two elements.
24+
All of the nodes' values will be unique.
25+
The given node will not be the tail and it will always be a valid node of the linked list.
26+
Do not return anything from your function.
27+
28+
29+
30+
31+
/**
32+
* Definition for singly-linked list.
33+
* struct ListNode {
34+
* int val;
35+
* ListNode *next;
36+
* ListNode(int x) : val(x), next(NULL) {}
37+
* };
38+
*/
39+
class Solution {
40+
public:
41+
void deleteNode(ListNode* node) {
42+
ListNode *tmp=node->next;
43+
node->val=tmp->val;
44+
node->next=tmp->next;
45+
delete tmp;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.
2+
3+
Example:
4+
5+
Input: 1->2->4, 1->3->4
6+
Output: 1->1->2->3->4->4
7+
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* struct ListNode {
12+
* int val;
13+
* ListNode *next;
14+
* ListNode() : val(0), next(nullptr) {}
15+
* ListNode(int x) : val(x), next(nullptr) {}
16+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
22+
ListNode *start, *last;
23+
if(!l1) return l2;
24+
if(!l2) return l1;
25+
if(l1->val <= l2->val){
26+
start=l1;
27+
l1=l1->next;
28+
}
29+
else {
30+
start=l2;
31+
l2=l2->next;
32+
}
33+
last=start;
34+
while(l1 && l2){
35+
if(l1->val <= l2->val){
36+
last->next=l1;
37+
last=last->next;
38+
l1=l1->next;
39+
40+
} else {
41+
last->next=l2;
42+
last=last->next;
43+
l2=l2->next;
44+
}
45+
}
46+
if(l1){
47+
last->next=l1;
48+
}
49+
if(l2){
50+
last->next=l2;
51+
}
52+
return start;
53+
}
54+
};
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
ios_base::sync_with_stdio(false);
6+
cin.tie(NULL);
7+
int t;
8+
cin>>t;
9+
while(t--){
10+
int n;
11+
cin>>n;
12+
long long a[n];
13+
for(int i=0;i<n;i++) cin>>a[i];
14+
stack<long long> s;
15+
vector<long long> v;
16+
for(int i=n-1;i>=0;i--){
17+
18+
if(s.empty()==true)
19+
v.push_back(-1);
20+
21+
else if(s.empty()==false && s.top()> a[i])
22+
v.push_back(s.top());
23+
24+
else if(s.empty()==false && s.top()<=a[i]){
25+
while(s.empty()==false && s.top()<=a[i]){
26+
s.pop();
27+
if(s.empty()==true)
28+
v.push_back(-1);
29+
else if(s.empty()==false && s.top()> a[i])
30+
v.push_back(s.top());
31+
}
32+
}
33+
34+
s.push(a[i]);
35+
}
36+
for(int i=n-1;i>=0;i--){
37+
cout<<v[i]<<" ";
38+
}
39+
cout<<endl;
40+
}
41+
42+
43+
44+
return 0;
45+
}

stack/stock_span_problem.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int main(){
5+
ios_base::sync_with_stdio(false);
6+
cin.tie(NULL);
7+
int t;
8+
cin>>t;
9+
while(t--){
10+
int n;
11+
cin>>n;
12+
int a[n];
13+
for(int i=0;i<n;i++) cin>>a[i];
14+
15+
stack<pair<int, int> >s;
16+
vector<int> v;
17+
18+
for(int i=0;i<n;i++){
19+
if(s.empty()) v.push_back(-1);
20+
else if(!s.empty() && s.top().first > a[i]) v.push_back(s.top().second);
21+
else if(!s.empty() && s.top().first<=a[i]){
22+
while(!s.empty() && s.top().first<=a[i]){
23+
s.pop();
24+
if(s.empty()) v.push_back(-1);
25+
else if(!s.empty() && s.top().first > a[i]) v.push_back(s.top().second);
26+
}
27+
}
28+
s.push({a[i], i});
29+
}
30+
for(int i=0;i<n;i++) cout<<i-v[i]<<" ";
31+
cout<<endl;
32+
}
33+
34+
35+
36+
return 0;
37+
}

tree/invert_a_binary_tree.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
struct node{
5+
int data;
6+
struct node* left;
7+
struct node* right;
8+
};
9+
10+
struct node* newNode(int value){
11+
struct node* tmp=(struct node*)malloc(sizeof(struct node));
12+
tmp->data=value;
13+
tmp->left=tmp->right=NULL;
14+
return tmp;
15+
};
16+
17+
18+
node* invertTree(struct node* root) {
19+
if(!root) return root;
20+
queue<node*>q;
21+
q.push(root);
22+
while(!q.empty()){
23+
struct node* cur = q.front();
24+
q.pop();
25+
swap(cur->left, cur->right);
26+
if(cur->left) q.push(cur->left);
27+
if(cur->right) q.push(cur->right);
28+
}
29+
return root;
30+
}
31+
32+
void inorder(struct node* root){
33+
if(!root) return;
34+
inorder(root->left);
35+
cout<<root->data<<" ";
36+
inorder(root->right);
37+
}
38+
39+
int main(){
40+
struct node* root=newNode(1);
41+
root->left=newNode(2);
42+
root->right=newNode(3);
43+
root->left->right=newNode(4);
44+
inorder(root);
45+
cout<<endl;
46+
root = invertTree(root);
47+
inorder(root);
48+
return 0;
49+
}

0 commit comments

Comments
 (0)