1099. Build A Binary Search Tree (30) <BST树>

来源:互联网 发布:端面铣数控怎样编程 编辑:程序博客网 时间:2024/06/04 00:37

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

    Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "left_index right_index", provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

    Output Specification:

    For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

    Sample Input:
    91 62 3-1 -1-1 45 -1-1 -17 -1-1 8-1 -173 45 11 58 82 25 67 38 42
    Sample Output:
    58 25 82 11 38 67 45 73 42
    相当于给你一个建立好的树,给你一些权值,让你输出层序遍历


    BST树:

     左结点权值小于结点权值

     右结点权值大于等于结点权值

    就相当于对这个树进行中序遍历一定是从小到大排列


    给你的权值sort一下,就是树的中序遍历

    然后对树的结点权值进行归为。最后输出层序遍历


    自己的问题:

    刚开始就使用链表做的,一直22分。

    百度搜都是用数组做的,好不容易找到一个说是因为

    假定了结点按先序遍历是按顺序编号的(当然是受样例的影响)这句话我也没看太懂。改用数组做了

  • 其实用数组做比链表还简单。。。。。。。

  • #include<cstdio>#include<cmath>#include<algorithm>#include<iostream>#include<cstring>#include<queue>#include<vector>#include<set>#include<map>#include<stack>using namespace std;typedef struct tree{int left,right;int num;}tree;tree t[100001];int key[100001];int ct=0;void zhong(int index){if(t[index].left!=-1){zhong(t[index].left);} t[index].num=key[ct++];if(t[index].right!=-1){zhong(t[index].right);}}void ceng(int index){queue<int> que;que.push(index);cout<<t[index].num;while(que.size()){int fz=que.front();que.pop();if(t[fz].left!=-1) {cout<<" "<<t[t[fz].left].num;que.push(t[fz].left);}if(t[fz].right!=-1) {que.push(t[fz].right);cout<<" "<<t[t[fz].right].num;}}}int main(){int n;cin>>n;for(int i=0;i<n;i++){scanf("%d %d",&t[i].left,&t[i].right);}for(int i=0;i<n;i++){scanf("%d",&key[i]);}sort(key,key+n);zhong(0);ceng(0);return 0;}
    另附链表做的未AC代码

  • #include<cstdio>#include<cmath>#include<algorithm>#include<iostream>#include<cstring>#include<queue>#include<vector>#include<set>#include<map>#include<stack>using namespace std;typedef struct tree{int num,fz;struct tree *right,*left;}tree,*linktree;int key[10001];int n;int cnt=0;static linktree t[10001];void qian(linktree head){if(head){int next=head->fz;qian(t[next]->left);t[next]->num=key[cnt++];//cout<<t[next]->num<<" ";qian(t[next]->right);}}int out[10001];int cn=0;void cengxu(linktree head){int next=0;queue<linktree> que;que.push(head);while(que.size()){linktree l=new tree();l=que.front();que.pop();next=l->fz;out[cn++]=(l->num);if(t[next]->left!=NULL){que.push(t[next]->left);}if(t[next]->right!=NULL){que.push(t[next]->right);}}}int main(){int f[1001]={0};cin>>n;t[0]=new tree();t[0]->fz=0;f[0]=1;for(int i=0;i<n;i++){int a,b;cin>>a>>b;if(f[i]==0){t[i]=new tree();f[i]=1;}if(a!=-1){   t[a]=new tree();   f[a]=1;   t[a]->fz=a;   t[i]->left=t[a];}else t[i]->left=NULL;if(b!=-1){   t[b]=new tree();   f[b]=1;   t[b]->fz=b;   t[i]->right=t[b];}else t[i]->right=NULL;}for(int i=0;i<n;i++) scanf("%d",&key[i]);sort(key,key+n);qian(t[0]);cengxu(t[0]);for(int i=0;i<cn;i++){if(i==0) printf("%d",out[i]);else printf(" %d",out[i]);}return 0;}



  • 原创粉丝点击