A1099. Build A Binary Search Tree

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:

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

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<vector>
 4 #include<queue>
 5 #include<algorithm>
 6 using namespace std;
 7 bool cmp(int a, int b){
 8     return a < b;
 9 }
10 typedef struct NODE{
11     int lchild, rchild;
12     int key;
13 }node;
14 node tree[1001];
15 int N, num[1001], index = 0;
16 void inOrder(int root){
17     if(root == -1)
18         return;
19     inOrder(tree[root].lchild);
20     tree[root].key = num[index++];
21     inOrder(tree[root].rchild);
22 }
23 void levelOrder(int root){
24     int cnt = 0;
25     queue<int> Q;
26     if(root != -1){
27         Q.push(root);
28     }
29     while(Q.empty() == false){
30         int temp = Q.front();
31         Q.pop();
32         cnt++;
33         if(cnt == N)
34             printf("%d", tree[temp].key);
35         else printf("%d ", tree[temp].key);
36         if(tree[temp].lchild != -1)
37             Q.push(tree[temp].lchild);
38         if(tree[temp].rchild != -1)
39             Q.push(tree[temp].rchild);
40     }
41 }
42 int main(){
43     scanf("%d", &N);
44     for(int i = 0; i < N; i++){
45         scanf("%d%d", &tree[i].lchild, &tree[i].rchild);
46     }
47     for(int i = 0; i < N; i++){
48         scanf("%d", &num[i]);
49     }
50     sort(num, num + N, cmp);
51     inOrder(0);
52     levelOrder(0);
53     cin >> N;
54     return 0;
55 }
View Code

总结:

1、题意:给出一个二叉树的具体形状,给出一些键值,要求将这些键值按照给定的形状插入,使之成为搜索树。

2、二叉搜索树的中序序列是从小到大的有序序列。根据这一性质,先对序列进行排序,就得到了搜索树的中序序列。再对给出的二叉树进行中序遍历,在遍历的过程中插入keys,就得到了一个搜索树。

原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8542889.html