Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node{
- int data;
- node *left;
- node *right;
- node(int n){
- data=n;
- left=NULL;
- right=NULL;
- }
- }*root=NULL;
- typedef struct node nodeT;
- nodeT* insertNode(nodeT *root,int data){
- nodeT *curr=root;
- if(curr==NULL){
- return root=new nodeT(node(data));
- }
- nodeT *prev;
- while(curr!=NULL){
- prev=curr;
- if(curr->data<=data){curr=curr->right;}
- else{curr=curr->left;}
- }
- if(prev->data<=data){prev->right=new nodeT(node(data));}
- else{prev->left=new nodeT(node(data));}
- return root;
- }
- void printT(nodeT *curr,int data){
- deque<int> q;
- bool found=false;
- while(curr!=NULL){
- q.push_back(curr->data);
- if(curr->data==data){found=true;}
- if(curr->data<=data){curr=curr->right;}
- else{curr=curr->left;}
- }
- if(found){
- while(!q.empty()){
- if(q.back()!=data){q.pop_back();}
- else{break;}
- }
- }
- while(!q.empty()){
- printf("%d ",q.front());
- q.pop_front();
- }
- if(!found){printf("0");}
- }
- int main(){
- int n;scanf("%d",&n);
- for(int i=0;i<n;i++){
- int a; scanf("%d",&a);
- root=insertNode(root,a);
- }
- scanf("%d",&n);
- for(int i=0;i<n;i++){
- int a; scanf("%d ",&a);
- printT(root,a);
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement