您的位置:首页 > 产品设计 > UI/UE

浙大pat | 浙大pat 牛客网甲级 1010 Build A Binary Search Tree (30) 二叉搜索树

2018-03-23 12:50 471 查看
题目描述A Binary Search Tree (BST) is recursively defined as a binarytree which has the following properties:
The left subtree of a node contains only nodes with keys less than the node'skey.
The right subtree of a node contains only nodes with keys greater than or equalto 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 distinctinteger keys, there is only one way to fill these keys into the tree so thatthe resulting tree satisfies the definition of a BST.  You are supposed to output the level ordertraversal sequence of that tree.  The sampleis illustrated by Figure 1 and 2.
  

输入描述:
Each input file contains one test case.  For each case, the first line gives apositive integer N (<=100) which is the total number of nodes in thetree.  The next N lines each contains theleft and the right children of a node in the format "left_indexright_index", provided that the nodes are numbered from 0 to N-1, and 0 isalways the root.  If one child ismissing, then -1 will represent the NULL child pointer.  Finally N distinct integer keys are given inthe last line.

输出描述:
For each test case, print in one line the level order traversalsequence of that tree.  All the numbersmust be separated by a space, with no extra space at the end of the line.

输入例子:
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

输出例子:
58 25 82 11 38 67 45 73 42
哎,要是考试的时候每一题都是这个难度就好了,这一题可以说是想当的简单,题目条例清晰,没有陷阱,也没有隐藏条件,并且让你干什么一清二楚
首先使用一个DFS得到每个节点左儿子个数和右儿子个数,
然后将输入数字排序,在使用一个DFS2来将排序之后的数字分配给每个节点,
之后使用一次BFS按照层次遍历的顺序输出每个节点的值,这样样就能够得到最终的结果
这一题条理非常的清晰
这里需要注意的是在统计左儿子节点和右儿子节点的数量的时候返回值要加一,这表示当前节点也参与了统计
以后在做二叉树的题目的时候使用多次BFS和DFS然后将每次遍历的结果统计起来,可能会有意想不到的好结果!
注意这个单词level order traversal sequence 表示的层次序遍历序列,也就是层次遍历,广度优先遍历,traversal表示横越,遍历的意思#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;
pair<int, int> leftOrRightCount[103];
int theEdges[103][2] = { -1 };
int everyNum[103];
int DFS1(int t)
{
if (t == -1) return 0;
int left = DFS1(theEdges[t][0]);
int right = DFS1(theEdges[t][1]);
leftOrRightCount[t] = make_pair(left, right);
return left + right+1;
}
void DFS2(int left, int right, int t,vector<int> &num)
{
if (t == -1) return;
everyNum[t] = num[left+ leftOrRightCount[t].first];
DFS2(left, left + leftOrRightCount[t].first - 1, theEdges[t][0], num);
DFS2(left + leftOrRightCount[t].first + 1, right, theEdges[t][1], num);
}

int main()
{
int N;
int a, b;
cin >> N;
vector<int> num(N);
for (int i = 0; i < N; i++)
{
cin >> a >> b;
theEdges[i][0] = a;
theEdges[i][1] = b;
}
for (int i = 0; i < N; i++)
{
cin >> num[i];
}
sort(num.begin(), num.end());
DFS1(0);
DFS2(0, N - 1, 0, num);
queue<int> theQueue;
theQueue.push(0);
b = 1;
while (!theQueue.empty())
{
a = theQueue.front();
theQueue.pop();
cout << everyNum[a];
if ((b++) != N) cout << " ";
if (theEdges[a][0] != -1) theQueue.push(theEdges[a][0]);
if (theEdges[a][1] != -1) theQueue.push(theEdges[a][1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: