DFS深度优先搜索中序填充 左-中-右1099. Build A Binary Search Tree (30)
时间限制100 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
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 42Sample Output:58 25 82 11 38 67 45 73 42有N个结点(0-N-1),接下来N个结点的左结点和右结点标号,无为-1;再接下来给出N个要填充的值;填充要点左<中,中<=右;
做法先按非降序排列#include<algorithm> 使用
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月02日 10:34 | 答案正确 | 30 | 1099 | C++ (g++ 4.7.2) | 1 | 436 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 180 | 18/18 |
1 | 答案正确 | 1 | 436 | 3/3 |
2 | 答案正确 | 1 | 180 | 3/3 |
3 | 答案正确 | 1 | 308 | 1/1 |
4 | 答案正确 | 1 | 180 | 5/5 |
#include<iostream> #include<algorithm> #include<queue> #define NOEXIST -1 using namespace std; struct BinarySearch { int left_index, right_inde; int value; }; void Dureadln(int*data, BinarySearch*SubTree, int N) { int index; for (index = 0; index < N; index++) cin >> SubTree[index].left_index >> SubTree[index].right_inde; while (index--) cin >> data[index]; } bool dataCMp(const int &A,const int&B) { return A < B; } void buildSubtreevalueDFS(int*data, BinarySearch*SubTree, int* dataindex,int root) { if (NOEXIST != SubTree[root].left_index) buildSubtreevalueDFS(data, SubTree, dataindex, SubTree[root].left_index); SubTree[root].value = data[(*dataindex)++]; if (NOEXIST != SubTree[root].right_inde) buildSubtreevalueDFS(data, SubTree, dataindex, SubTree[root].right_inde); } void printfBFS(BinarySearch*SubTree, int root) { queue<int>Qroot; int len,newroot; cout << SubTree[root].value; Qroot.push(root); while (!Qroot.empty()) { len = Qroot.size(); while (len--) { root = Qroot.front(); if (NOEXIST != SubTree[root].left_index) { newroot = SubTree[root].left_index; cout <<" "<< SubTree[newroot].value ; Qroot.push(newroot); } if (NOEXIST != SubTree[root].right_inde) { newroot = SubTree[root].right_inde; cout <<" "<< SubTree[newroot].value ; Qroot.push(newroot); } Qroot.pop(); } } cout << endl; } int main() { int*data; BinarySearch*SubTree; int N; int dataindex,root; cin >> N; data = new int[N]; SubTree = new BinarySearch[N]; Dureadln(data,SubTree, N); sort(data, data + N, dataCMp); dataindex = 0; root = 0; buildSubtreevalueDFS(data, SubTree, &dataindex,root); printfBFS( SubTree, root); delete[]SubTree; delete[]data; system("pause"); return 0; }