/*
https://www.nowcoder.com/profile/2538016/codeBookDetail?submissionId=28987592
*/
#include <bits/stdc++.h>
using namespace std;
typedef struct node{
int data;
struct node* lchild;
struct node* rchild;
}Bitree;
node *insert(node *T, int d,int &f){
if(T==NULL){
T=(node*)malloc(sizeof(node));
T->data=d;
T->lchild=NULL;
T->rchild=NULL;
}else if(T->data>d){
f=T->data;
T->lchild=insert(T->lchild,d,f);
}else{
f=T->data;
T->rchild=insert(T->rchild,d,f);
}
return T;
}
int main(){
int n,f,d;
while(~scanf("%d",&n)){
node *T=NULL;
for(int i=0;i<n;i++){
f=-1;
scanf("%d",&d);
T=insert(T,d,f);
printf("%d
", f);
}
}
return 0;
}
/*
https://www.nowcoder.com/profile/2538016/codeBookDetail?submissionId=28989528
*/
#include<iostream> #include<string.h> #include<set> using namespace std; struct Node { int value; struct Node *LeftNode=NULL; struct Node *RightNode=NULL; struct Node *pre=NULL; }; int pre[1005]; int in[1005]; int post[1005]; int t; void insert(Node *node,Node *p) { if(node==NULL) { return; } if(p->value<node->value) { if(node->LeftNode==NULL) { node->LeftNode=p; } else { insert(node->LeftNode,p); } } else { if(node->RightNode==NULL) { node->RightNode=p; } else { insert(node->RightNode,p); } } } void preOrder(Node *node) { if(node==NULL) return; pre[t++]=node->value; preOrder(node->LeftNode); preOrder(node->RightNode); } void inOrder(Node *node) { if(node==NULL) return; inOrder(node->LeftNode); in[t++]=node->value; inOrder(node->RightNode); } void postOrder(Node *node) { if(node==NULL) return; postOrder(node->LeftNode); postOrder(node->RightNode); post[t++]=node->value; } int main() { int n,a; while(cin>>n>>a){ set<int> s; memset(in,0,sizeof(in)); memset(pre,0,sizeof(pre)); memset(post,0,sizeof(post)); Node *root=new Node; Node *p; root->value=a; s.insert(a); for(int i=0; i<n-1; i++) { cin>>a; if(s.count(a)==1) continue; s.insert(a); p=new Node; p->value=a; insert(root,p); } t=0; preOrder(root); t=0; inOrder(root); t=0; postOrder(root); for(int i=0; i<s.size(); i++) { cout<<pre[i]<<" "; } cout<<endl; for(int i=0; i<s.size(); i++) { cout<<in[i]<<" "; } cout<<endl; for(int i=0; i<s.size(); i++) { cout<<post[i]<<" "; } cout<<endl; } return 0; }
/*
https://www.nowcoder.com/profile/2538016/codeBookDetail?submissionId=28990885
*/
#include <bits/stdc++.h> using namespace std; char pre[26]; char in[26]; char post[26]; void Post(int low1,int high1,int low2,int high2,int low,int high){ if(low>high) return; char c=pre[low1]; post[high]=c; int k=0; while(in[low2+k]!=c) k++; Post(low1+1,low1+k,low2,low2+k-1,low,low+k-1); Post(low1+k+1,high1,low2+k+1,high2,low+k,high-1); return; } int main(){ while(~scanf("%s",pre)){ scanf(" %s",in); int len=strlen(pre); Post(0,len-1,0,len-1,0,len-1); printf("%s",post); printf(" "); } return 0; }
/*
https://www.nowcoder.com/profile/2538016/codeBookDetail?submissionId=28992669
*/
#include <bits/stdc++.h> using namespace std; typedef struct node{ char data; struct node *lchild,*rchild; }*Bitree,Tnode; Tnode T[200]; int loc,size; Bitree creat(){ T[loc].lchild=T[loc].rchild=NULL; return &T[loc++]; } Bitree Insert(Bitree T,char x){ if(!T){ T=creat(); T->data=x; return T; } if(x<T->data) T->lchild=Insert(T->lchild,x); else T->rchild=Insert(T->rchild,x); return T; } void preorder(Bitree T,char pre[]){ if(!T) return; pre[size++] = T->data; pre[size]=0; preorder(T->lchild,pre); preorder(T->rchild,pre); return; } int main(){ Bitree T1,T2; int n; while(cin>>n){ T1=T2=NULL; loc=0; char str1[20]; char str2[20]; cin>>str1; int len1=strlen(str1); for(int i=0;i<len1;i++){ T1=Insert(T1,str1[i]); } for(int i=0;i<n;i++){ T2=NULL; cin>>str2; for(int i=0;str2[i]!=0;i++) T2=Insert(T2,str2[i]); char a[24],b[24]; preorder(T1,a); size=0; preorder(T2,b); size=0; puts(strcmp(a,b)==0?"YES":"NO"); } } return 0; }