PAT 1064. Complete Binary Search Tree (30)(中序遍历来给完全搜索树赋值,题目是给出一个列数字,把它构建成完全搜索树并输出)
来源:互联网 发布:淘宝护盾 编辑:程序博客网 时间:2024/06/11 05:28
官网
1064. Complete 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.
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.
Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input:
10
1 2 3 4 5 6 7 8 9 0
Sample Output:
6 3 8 1 5 7 9 0 2 4
题目大意
- 1.给出一个列数字,把它构建成完全搜索树并层次遍历输出。
解题思路
- 1.因为是完全搜索树,所以给出树的节点的个数,这棵树长什么样子就确定了,然后把给出的这列数字从小到大排序,那么再用中序遍历遍历这棵树,并给这棵树从小到大赋值,我们可以发现,这个完全搜索树已经构建成功了。
AC代码
#include<iostream>#include<deque>#include<vector>#include<algorithm>using namespace std;int n,k=0;vector<int> keep;typedef struct node{ int value; node * left; node * right; node():left(NULL),right(NULL){} //node (int v):value(v),left(NULL),right(NULL){}}* Tree;//依据这个树的大小n,把这棵树的框架搭起来Tree build(){ Tree root = new node; k++; deque<Tree> p; p.push_back(root); while (!p.empty()) { Tree t = p.front(); if (k<n) { t->left = new node; p.push_back(t->left); k++; }else { break; } if (k<n) { t->right = new node; p.push_back(t->right); k++; }else { break; } p.pop_front(); } return root;}//利用中序遍历给每个节点赋值void inOrder(Tree root){ if (root == NULL) { return; } inOrder(root->left); root->value = keep[k++]; inOrder(root->right);}//层次遍历输出bool flag = false;void print(Tree root){ deque<Tree> p; p.push_back(root); while (!p.empty()) { Tree t = p.front(); if (!flag) { cout << t->value; flag= true; }else { cout << " " << t->value; } if (t->left != NULL) { p.push_back(t->left); } if (t->right != NULL) { p.push_back(t->right); } p.pop_front(); }}int main(int argc, char *argv[]){ cin >> n; keep.resize(n); for (int i = 0; i < n; ++i) { cin >> keep[i]; } //排序 sort(keep.begin(),keep.end()); Tree root = build(); k = 0; inOrder(root); print(root); cout << endl; return 0;}
- PAT 1064. Complete Binary Search Tree (30)(中序遍历来给完全搜索树赋值,题目是给出一个列数字,把它构建成完全搜索树并输出)
- 根据中序遍历顺序构建完全二叉搜索树-04-树6 Complete Binary Search Tree (30分)
- pat 甲1064. Complete Binary Search Tree (完全二叉搜索树)
- pat(A) 1064. Complete Binary Search Tree(完全二叉树的中序建树)
- PAT-1064 Complete Binary Search Tree(完全二叉树)
- PAT 1043. Is It a Binary Search Tree (25)(判断是否是搜索树并构建和后序输出)(待修改)
- PAT 1064. Complete Binary Search Tree (30) 快速建立完全二叉树
- 04-树6 Complete Binary Search Tree 完全二叉搜索树
- PAT 1064. Complete Binary Search Tree (二叉树遍历)
- pat甲1110. Complete Binary Tree(完全二叉树)
- Complete Binary Search Tree(完全二叉搜索树)用数组表示和计算左子树的规模
- 1064. Complete Binary Search Tree (30)-PAT
- 【PAT】1064. Complete Binary Search Tree (30)
- PAT 1064. Complete Binary Search Tree (30)
- pat 1064. Complete Binary Search Tree (30)
- PAT 1123. Is It a Complete AVL Tree (30) 平衡树构建+ 完全二叉树判断
- 完全二叉树(Complete Binary Tree)
- 由初始序列构造二叉搜索树,输出层次遍历,并判断是否是完全二叉树
- Python 变量类型
- Hadoop CDH四种安装方式总结及实例指导
- SSO (Single Sign On)
- http协议:通俗易懂的小教程
- mvn创建项目执行到Generating Project in Batch mode 卡住
- PAT 1064. Complete Binary Search Tree (30)(中序遍历来给完全搜索树赋值,题目是给出一个列数字,把它构建成完全搜索树并输出)
- VS优化Release代码速度
- JSP登录
- lua中的面向对象模拟,类,继承,多态
- 算法基础
- HashMap并发异常小记
- 2016/9/9阿里笔试编程题-字符串的查找替换
- Android数据解析-----解析xml数据
- (矩阵)快速幂