|
| 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