PAT(二叉搜索树)——1099. 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.
Snip20160811_82

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

题目大意:

给出二叉搜索树的结构和所有的值,输出这些值组成二叉搜索树的层序遍历。

题目解析:

类似于1064题的思路,由给定的值序列,从小到大排列得到二叉搜索树的中序遍历序列,然后填入给定的结构即可。

具体代码:

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 1005

struct node{
	int value;
	int left=-1;
	int right=-1;
}ans[MAXN];
int n,index=0;
int list[MAXN];

void create(int root){
	if(root!=-1&&root<n){
		create(ans[root].left);
		ans[root].value=list[index++];
		create(ans[root].right);
	}
}
void level(){
	queue<int> q;
	q.push(0);
	while(!q.empty()){
		int tmp=q.front();q.pop();
		if(tmp==-1) continue;
		if(tmp==0)
			cout<<ans[tmp].value;
		else
			cout<<" "<<ans[tmp].value;
		q.push(ans[tmp].left);
		q.push(ans[tmp].right);
	}
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    	cin>>ans[i].left>>ans[i].right;
    for(int i=0;i<n;i++)
    	cin>>list[i];
    sort(list,list+n);
    create(0);
	level();
    return 0;
}
全部评论

相关推荐

🎓学历背景:末二本+北邮硕想找段日常&nbsp;是简历写的有问题吗&nbsp;目前有家100-499的小厂过了,但大厂现在一个都没面过,官网投递一直在筛简历
牛客44176770...:我也28届,也是投了一个多月,四月底投的,面了6.7场,有个大厂,没结果应该是挂了,有三个小厂面试的很顺利,结果没下文了,互联网我恨你!这五月我时间都在投简历和改简历上了,结果没啥收获,算法也没刷,因为约的面试都没有算法索性就只看项目和八股唉,真的好累啊
我的简历长这样
点赞 评论 收藏
分享
05-29 19:11
已编辑
北方民族大学 Java
😭😭😭😭本人26届双非本,后端选手。从25年秋招开始,一直到春招5月份,一共面了12次字节。可以说后面能继续投递面上字节大概率是因为前面一直累计的面评还不错,但是最终的结果往往不尽如人意,黄梁一梦。timeline:如标题,总共面了12次字节,4个不同的岗位。第一次:抖音生活服务测开二面完排序挂第二次:TikTok国际化电商测开三面完排序挂第三次:飞书后端安全团队三面完挂第四次:飞书后端偏基架团队三面完过,HR面完之后询问综合排序不推进。我知道像BAT这样的公司,双非本想拿到一张入场券有多难,也知道每次挂在排序/三面/HR面,那种差一步上岸又被打回原点的落差感有多磨人。可是最后一次字节的这个岗位,已经是5月中旬才开始面得了,春招末期的岗位,我本以为真的缺人,三面过的那天,我真的以为就差一步hr面就稳了,但是,最终的结果很遗憾,综合排序综合排序,不推进了。如果是技术能力的问题,我想也不会每一轮技术面给我通过。思来想去。难道真的就是因为我们双非有案底,所以最后的一切又算什么呢。付出这么多的时间精力,还是抵不过双非学历太差吗?既然如此一开始直接卡掉简历不用给面试不就行了嘛,每一轮面试都给我们生的希望,最后的最后又回到了那个必输的起点。12次字节,说不遗憾是假的,也无数次怀疑过自己:是不是我算法刷得还不够?是不是项目亮点讲得不够好?是不是学历就是一道跨不过去的坎?但回头看,这一年的秋招到春招,从面对面试官紧张到说话卡壳,到后来的从容面对,再到如今甚至能和面试官探讨AI&amp;大模型技术的一些方案思路,我已经比去年的自己强太多了。可能字节于我,真的是一场盛大的单恋,拼尽全力奔赴,却还是没能收到想要的回应。前路漫漫,字节的梦碎了,但我的路还在继续,希望下一站,会有属于我的一场徐风。
不愿吃饼的山羊很友好:你的心理素质是真的强大,如果是我碰到这样都会疯了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务