PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历
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 <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
using namespace std;
const int maxn = ;
int n;
struct node{
int data;
int l=-,r=-;
}bst[maxn];
int index=;
int q[maxn];
void inorder(int root){
if(bst[root].l!=-) inorder(bst[root].l);
bst[root].data = q[index++];
if(bst[root].r!=-) inorder(bst[root].r);
}
void level(int root){
queue<int> que;
int cnt=;
que.push(root);
while(!que.empty()){
int now=que.front();
que.pop();
printf("%d",bst[now].data);
cnt++;
if(cnt!=n) printf(" ");
if(bst[now].l!=-) que.push(bst[now].l);
if(bst[now].r!=-) que.push(bst[now].r);
}
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int l,r;
scanf("%d %d",&l,&r);
bst[i].l=l;
bst[i].r=r;
}
for(int i=;i<n;i++){
scanf("%d",&q[i]);
}
sort(q,q+n);
inorder();
level();
}
注意点:其实很简单的题目,又一次倒在了递归上。知道要把给定数据sort,但没想sort完后就是这棵树的中序遍历结果,只要把正常中序遍历时的打印改成赋值就能得到那棵树了。没想到中序遍历,所以自己一直在想怎么把这个有序序列一个个填到树里去,一直也没搞明白,看这题通过率0.5多,我还不会做,凉了。
看到树的题目,一般都会要建树,遍历,而对bst,一般都是要对给定序列排序的,这样能得到他的中序遍历结果,有助于建树
PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历的更多相关文章
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT-1099(Build A Binary Search Tree)Java实现+二叉排序树的中序遍历和层次遍历
Build A Binary Search Tree PAT-1099 本题有意思的一个点就是:题目已经给出了一颗排序二叉树的结构,需要根据这个结构和中序遍历序列重构一棵二叉排序树. 解法:可以根据中 ...
- LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)
题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...
- LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆
描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...
随机推荐
- socket、web socket
WEB SOCKET: 说明:为浏览器提供双工异步通信,浏览器可以向服务端发送消息.服务端也可以向浏览器发送消息. SOCKET: 说明:网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接 ...
- java Name [jdbc/myjavadb] is not bound in this Context. Unable to find [jdbc].
一.出错时的情况: 首先,这是一个servlet项目 1.项目的web.xml配置了:(后来发现不配置这个也行,但是tomcat一定要配置) <resource-ref> <desc ...
- TortoiseSVN 1.9.5安装 与 Eclipse4.4.2及以上版本中安装SVN插件
引自: http://blog.csdn.net/chenchunlin526/article/details/54631458 TortoiseSVN 1.9.5安装 与 Eclipse4.4.2及 ...
- SAP MM PIR里的Lower Limit & Upper Limit
SAP MM PIR里的Lower Limit & Upper Limit 在PIR的价格的detail数据里,有2个字段:Lower Limit和Upper Limit.在今天之前,笔者从未 ...
- Salesforce的公式和验证规则
公式 在Salesforce中,有些功能不需要从数据库中直接读取的数据,而是基于这些数据之间的关系来做出判断.这种情况下就要用到"公式"功能. 公式的概念和Excel中的公式类似, ...
- PyCharm实现高效远程调试代码
PyCharm实现高效远程调试代码 (薛刚强) 为方便Python代码学习和项目开发,目前选择专业的 IDE 开发工具 ,如 PyCham.针对个人使用的技巧做个笔记,分享给大家,有描述 ...
- spring使用BeanPostProcesor实现AOP源码分析
源码 AbstractApplicationContext#public void refresh() throws BeansException, IllegalStateException { f ...
- (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
访问某页面时,出现了这个异常: java.lang.IllegalArgumentException: Wildcard string cannot be null or empty. Make su ...
- zookeeper.Net
原文转至:http://www.cnblogs.com/shanyou/p/3221990.html 之前整理过一篇文章<zookeeper 分布式锁服务>,本文介绍的 Zookeeper ...
- [20180317]12c TABLE ACCESS BY INDEX ROWID BATCHED2.txt
[20180317]12c TABLE ACCESS BY INDEX ROWID BATCHED2.txt --//简单探究12c TABLE ACCESS BY INDEX ROWID BATCH ...