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
思路:先将输入数据排序,再中序遍历二叉树的框架填充数据即可。使用前序遍历填充可以,只是需要计算出根节点的位置。为此先使用后序遍历求出每个结点的字数的结点数量。
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 110;
typedef struct Node
{
int left;
int right;
int v;
int num; //以该结点为根结点的树的结点总数,前序建树时使用
Node(){}
Node(int l, int r):left(l),right(r),v(0),num(0){}
}node;
// 后序遍历求每棵子树的结点数量
// 配合前序建树使用
int setNum(node tree[], int root)
{
if(root == -1) return 0;
else
{
tree[root].num = setNum(tree, tree[root].left) + setNum(tree, tree[root].right) + 1;
return tree[root].num;
}
}
// 先序遍历建树
void fillTree(node tree[], int root, vector<int> data, int l, int r)
{
if(l <= r)
{
int leftRoot = tree[root].left;
int leftNum = leftRoot == -1 ? 0 : tree[leftRoot].num;
int rootData = leftNum + l;
tree[root].v = data[rootData];
fillTree(tree, tree[root].left, data, l, rootData - 1);
fillTree(tree, tree[root].right, data, rootData + 1, r);
}
}
// 中序遍历建树
int index = 0;
void inOrderBuild(node tree[], int root, vector<int> data)
{
if(root == -1) return;
inOrderBuild(tree, tree[root].left, data);
tree[root].v = data[index++];
inOrderBuild(tree, tree[root].right, data);
}
// 层序遍历
void layerT(node tree[], int root, vector<int> &result)
{
queue<int> q;
q.push(root);
while(!q.empty())
{
node tem = tree[q.front()];
result.push_back(tem.v);
if(tem.left != -1) q.push(tem.left);
if(tem.right != -1) q.push(tem.right);
q.pop();
}
}
int main()
{
int n;
node tree[maxn];
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
int left, right;
scanf("%d %d", &left, &right);
tree[i] = node(left, right);
}
vector<int> in;
for(int i = 0; i < n; i++)
{
int tem;
scanf("%d", &tem);
in.push_back(tem);
}
sort(in.begin(), in.end());
setNum(tree, 0);
//inOrderBuild(tree, 0, in); //中序遍历建树,可正常解答
fillTree(tree, 0, in, 0, n - 1); // 先序遍历建树
vector<int> result;
layerT(tree, 0, result);
for(int i = 0; i < result.size(); i++)
{
if(i != 0)
printf(" ");
printf("%d", result[i]);
}
return 0;
}