• 二叉排序树



    /*
    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; }
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    电磁学讲义3:电场
    电磁学讲义2:库仑定律
    电磁学讲义1:静电的基本现象
    安卓(Android)手机如何安装APK?
    理论物理极础9:相空间流体和吉布斯-刘维尔定理
    物理学家的LOGO
    Zhulina 的高分子刷理论
    一步一步学Silverlight 2系列(5):实现简单的拖放功能
    地图上显示X,Y 坐标代码
    一步一步学Silverlight 2系列(4):鼠标事件处理
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9286034.html
Copyright © 2020-2023  润新知