Skip to content

Commit 6c91b0c

Browse files
authored
Merge pull request #766 from Manda303/main
Max and min element in Binary Tree.cpp
2 parents c149ee2 + 612e8d5 commit 6c91b0c

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

Queue using two Stacks.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class StackQueue{
6+
private:
7+
stack<int> s1;
8+
stack<int> s2;
9+
public:
10+
void push(int B);
11+
int pop();
12+
13+
};
14+
int main()
15+
{
16+
17+
int T;
18+
cin>>T;
19+
while(T--)
20+
{
21+
StackQueue *sq = new StackQueue();
22+
23+
int Q;
24+
cin>>Q;
25+
while(Q--){
26+
int QueryType=0;
27+
cin>>QueryType;
28+
if(QueryType==1)
29+
{
30+
int a;
31+
cin>>a;
32+
sq->push(a);
33+
}else if(QueryType==2){
34+
cout<<sq->pop()<<" ";
35+
36+
}
37+
}
38+
cout<<endl;
39+
}
40+
41+
42+
}
43+
// } Driver Code Ends
44+
45+
46+
/* The structure of the class is
47+
class StackQueue{
48+
private:
49+
// These are STL stacks ( http://goo.gl/LxlRZQ )
50+
stack<int> s1;
51+
stack<int> s2;
52+
public:
53+
void push(int);
54+
int pop();
55+
}; */
56+
57+
/* The method push to push element into the queue */
58+
void StackQueue :: push(int x)
59+
{
60+
// Your Code
61+
while(!s1.empty())
62+
{
63+
s2.push(s1.top());
64+
s1.pop();
65+
}
66+
s1.push(x);
67+
while(!s2.empty())
68+
{
69+
s1.push(s2.top());
70+
s2.pop();
71+
}
72+
73+
}
74+
75+
/*The method pop which return the element poped out of the queue*/
76+
int StackQueue :: pop()
77+
{
78+
// Your Code
79+
int val=-1;
80+
if(!s1.empty())
81+
{
82+
val=s1.top();
83+
s1.pop();
84+
}
85+
return val;
86+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// A tree node
5+
struct node
6+
{
7+
int data;
8+
struct node *left;
9+
struct node *right;
10+
11+
node(int x){
12+
data = x;
13+
left = NULL;
14+
right = NULL;
15+
}
16+
};
17+
18+
// Returns maximum value in a given Binary Tree
19+
int findMax(struct node* root);
20+
// Returns minimum value in a given Binary Tree
21+
int findMin(struct node* root);
22+
23+
void insert(struct node *root,int n1,int n2,char lr)
24+
{
25+
if(root==NULL)
26+
return;
27+
if(root->data==n1)
28+
{
29+
switch(lr)
30+
{
31+
case 'L': root->left=new node(n2);
32+
break;
33+
case 'R': root->right=new node(n2);
34+
break;
35+
}
36+
}
37+
else
38+
{
39+
insert(root->left,n1,n2,lr);
40+
insert(root->right,n1,n2,lr);
41+
}
42+
}
43+
// Driver program to test above functions
44+
int main()
45+
{
46+
int t;
47+
cin>>t;
48+
while(t--)
49+
{
50+
int n;
51+
cin>>n;
52+
struct node *root=NULL;
53+
while(n--)
54+
{
55+
char lr;
56+
int n1,n2;
57+
cin>>n1>>n2;
58+
cin>>lr;
59+
if(root==NULL)
60+
{
61+
root=new node(n1);
62+
switch(lr){
63+
case 'L': root->left=new node(n2);
64+
break;
65+
case 'R': root->right=new node(n2);
66+
break;
67+
}
68+
}
69+
else
70+
{
71+
insert(root,n1,n2,lr);
72+
}
73+
}
74+
75+
cout<<findMax(root)<<" "<<findMin(root)<<endl;
76+
}
77+
return 0;
78+
}
79+
// } Driver Code Ends
80+
81+
82+
// Returns maximum value in a given Binary Tree
83+
int findMax(struct node* root)
84+
{
85+
// Your code goes here
86+
queue <node *> q;
87+
q.push(root);
88+
int m=INT_MIN;
89+
while(!q.empty())
90+
{
91+
int count=q.size(),sum=0;
92+
for(int i=0;i<count;i++)
93+
{
94+
node *cur=q.front();
95+
m=max(cur->data,m);
96+
q.pop();
97+
if(cur->left!=NULL)
98+
q.push(cur->left);
99+
if(cur->right!=NULL)
100+
q.push(cur->right);
101+
}
102+
}
103+
return m;
104+
}
105+
106+
// Returns minimum value in a given Binary Tree
107+
int findMin(struct node* root)
108+
{
109+
// Your code goes here
110+
queue <node *> q;
111+
q.push(root);
112+
int m=INT_MAX;
113+
while(!q.empty())
114+
{
115+
int count=q.size(),sum=0;
116+
for(int i=0;i<count;i++)
117+
{
118+
node *cur=q.front();
119+
m=min(cur->data,m);
120+
q.pop();
121+
if(cur->left!=NULL)
122+
q.push(cur->left);
123+
if(cur->right!=NULL)
124+
q.push(cur->right);
125+
}
126+
}
127+
return m;
128+
}

0 commit comments

Comments
 (0)