【PAT甲级】1099 Build A Binary Search Tree (30 分)

题意:

输入一个正整数N(<=100),接着输入N行每行包括0~N-1结点的左右子结点,接着输入一行N个数表示数的结点值。输出这颗二叉排序树的层次遍历。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 pair<int,int>a[107];
 5 int b[107];
 6 int cnt=0;
 7 int ans[107];
 8 void dfs(int x){
 9     if(x==-1)
10         return ;
11     dfs(a[x].first);
12     ans[x]=b[++cnt];
13     dfs(a[x].second);
14 }
15 void bfs(int x){
16     queue<int>q;
17     q.push(x);
18     while(!q.empty()){
19         int now=q.front();
20         q.pop();
21         if(a[now].first!=-1)
22             q.push(a[now].first);
23         if(a[now].second!=-1)
24             q.push(a[now].second);
25         if(now!=x)
26             cout<<" ";
27         cout<<ans[now];
28     }
29 }
30 int main(){
31     ios::sync_with_stdio(false);
32     cin.tie(NULL);
33     cout.tie(NULL);
34     int n;
35     cin>>n;
36     for(int i=1;i<=n;++i){
37         int x,y;
38         cin>>x>>y;
39         a[i-1]={x,y};
40     }
41     for(int i=1;i<=n;++i)
42         cin>>b[i];
43     sort(b+1,b+1+n);
44     dfs(0);
45     bfs(0);
46     return 0;
47 }

原文地址:https://www.cnblogs.com/ldudxy/p/11964473.html

时间: 2024-11-02 05:14:51

【PAT甲级】1099 Build A Binary Search Tree (30 分)的相关文章

PAT Advanced 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 greate

PAT 甲级 1099 Build A Binary Search Tree

https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 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 th

PAT Advanced 1099 Build A Binary Search Tree (30) [?叉查找树BST]

题目 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 gre

1099. Build A Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1099. Build A Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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

PAT 1099. Build A Binary Search Tree (30)

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 nod

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 greate

PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int

PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子.中序遍历的顺序正好是序列从小到大的顺序,因此中序遍历的时候顺便赋值就可以了,最后层次遍历输出. 思路一:中序遍历的时候赋值 #include <iostream> #include <cstdio> #include <algorithm> #include <str

pat1099. Build A Binary Search Tree (30)

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