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 分)——二叉搜索树,中序遍历,层序遍历的更多相关文章

  1. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  2. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  3. PAT-1099(Build A Binary Search Tree)Java实现+二叉排序树的中序遍历和层次遍历

    Build A Binary Search Tree PAT-1099 本题有意思的一个点就是:题目已经给出了一颗排序二叉树的结构,需要根据这个结构和中序遍历序列重构一棵二叉排序树. 解法:可以根据中 ...

  4. 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 ...

  5. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  6. LeetCode OJ:Recover Binary Search Tree(恢复二叉搜索树)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. 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 ...

  8. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆

    描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...

随机推荐

  1. Java--实现单点登录

    1 什么是单点登陆 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用 ...

  2. Why is one loop so much slower than two loops?

    Question: Suppose a1, b1, c1, and d1 point to heap memory and my numerical code has the following co ...

  3. DotNetCore 结合 Nginx 将网站部署到阿里云

    基础环境配置 域名和服务器请先自行购买 基于 云服务器ECS 创建一个应用实例,选择系统镜像为 Ubuntu 16.04,在本机通过 SSH 进行远程连接,并进行相关配置 ssh root@http: ...

  4. Django Rest Framework之认证

    代码基本结构 url.py: from django.conf.urls import url, include from web.views.s1_api import TestView urlpa ...

  5. 20个Chrome DevTools调试技巧

    译者按: Chrome DevTools很强大,甚至可以替代IDE了! 原文: Art of debugging with Chrome DevTools 译者: Fundebug 为了保证可读性,本 ...

  6. python爬虫实例--网易云音乐排行榜爬虫

    网易云音乐,以前是有个api 链接的json下载的,现在没了, 只有音乐id,title , 只能看播放请求了, 但是播放请求都是加密的值,好坑... 进过各种努力, 终于找到了个大神写的博客,3.6 ...

  7. C++解析 xml,用到pugixml库

    参考网站: https://www.cnblogs.com/haomiao/p/5041065.html https://blog.csdn.net/iot_change/article/detail ...

  8. 消除2个按钮之间1px细节引起的冲突

    1.代码 <!doctype html> <html lang="en"> <head> <meta charset="UTF- ...

  9. 使用CSS兄弟选择器完成复杂垂直边距(vertical margins)的设计

    -------------------sibling选择器如何在完成复杂设计要求的同时,保持CSS可读 这是web前端开发过程中开始简单逐步变的复杂的例子之一:将一篇文章中的所有元素应用垂直边距(ve ...

  10. cmake:善用find_package()提高效率暨查找JNI支持

    cmake提供了很多实用的cmake-modules,通过find_package()命令调用这些modules,用于写CMakeLists.txt脚本时方便的查找依赖的库或其他编译相关的信息,善用这 ...