A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
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:
9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42
Sample Output:
58 25 82 11 38 67 45 73 42
已知二叉查找树的所有非叶子节点的子节点信息,求其层序序列
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100;
int n,cnt=0,in[maxn],nds[maxn][2];
struct node {
    int data;
    node * left=NULL;
    node * right=NULL;
    node() {}
    node(int _data):data(_data) {}
};
node * inOrder(int index) {
    if(index==-1) {
        return NULL;
    }
    node * root = new node();
    root->left=inOrder(nds[index][0]);
    root->data = in[cnt++];
    root->right=inOrder(nds[index][1]);
    return root;
}
void levelOrder(node * root){
    queue<node*> q;
    q.push(root);
    int index = 0;
    while(!q.empty()){
        node * now = q.front();
        q.pop();
        printf("%d",now->data);
        if(++index<n)printf(" "); 
        if(now->left!=NULL)q.push(now->left);
        if(now->right!=NULL)q.push(now->right);
    }
}
int main(int argc,char * argv[]) {
    scanf("%d",&n);
    int f,r;
    for(int i=0; i<n; i++) {
        scanf("%d %d",&f, &r);
        nds[i][0]=f;
        nds[i][1]=r;
    }
    for(int i=0; i<n; i++) scanf("%d",&in[i]);
    sort(in,in+n);
    node * root = inOrder(0);
    levelOrder(root);
    return 0;
}#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100;
int n,cnt=0,in[maxn];
struct node {
    int data;
    int index=-1; 
    int level=-1; 
    int left=-1;
    int right=-1;
    node() {}
    node(int _data):data(_data) {}
    node(int _left, int _right) {
        left=_left;
        right=_right;
    }
}nds[maxn];
void inOrder(int root, int index, int level) {
    if(root==-1) {
        return;
    }
    inOrder(nds[root].left,2*index+1,level+1);
    nds[root].index=index;
    nds[root].level=level;
    nds[root].data=in[cnt++];
    inOrder(nds[root].right,2*index+2,level+1);
}
bool cmp(node &n1,node &n2){
    if(n1.level!=n2.level)return n1.level<n2.level;
    return n1.index<n2.index;
}
int main(int argc,char * argv[]) {
    scanf("%d",&n);
    int f,r;
    for(int i=0; i<n; i++) {
        scanf("%d %d",&nds[i].left, &nds[i].right);
    }
    for(int i=0; i<n; i++) scanf("%d",&in[i]);
    sort(in,in+n);
    inOrder(0,0,0);
    sort(nds,nds+n,cmp);
    for(int i=0;i<n;i++){
        if(i!=0)printf(" ");
        printf("%d",nds[i].data);
    }
    return 0;
}
PAT Advanced 1099 Build A Binary Search Tree (30) [?叉查找树BST]
原文:https://www.cnblogs.com/houzm/p/12333843.html