PAT 1064 Complete Binary Search Tree

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <vector>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 class Node {
10 public:
11     int val;
12     Node* left;
13     Node* right;
14 public:
15     Node(): val(0), left(NULL), right(NULL) {}
16     Node(int _val, Node* _left = NULL, Node* _right = NULL): val(_val), left(_left), right(_right) {}
17 };
18
19 vector<Node*> build_next_level(vector<Node*> last_level, int &num) {
20     vector<Node*> res;
21     if (num < 1) return res;
22
23     for (auto iter = last_level.begin(); iter != last_level.end(); iter++) {
24         Node* parent = *iter;
25
26         Node* lchild = new Node();
27         res.push_back(lchild);
28         parent->left = lchild;
29         if (--num < 1) break;
30
31         Node* rchild = new Node();
32         res.push_back(rchild);
33         parent->right= rchild;
34         if (--num < 1) break;
35     }
36
37     return res;
38 }
39
40 void inorder_traverse(Node* root, int& order) {
41     if (root == NULL) return;
42     inorder_traverse(root->left, order);
43     root->val = order++;
44     inorder_traverse(root->right, order);
45 }
46
47 int main() {
48     int cnt;
49
50     scanf("%d", &cnt);
51
52     vector<int> nums(cnt, 0);
53
54     if (cnt < 1) return 0;
55
56     for (int i = 0; i<cnt; i++) {
57         int num = 0;
58         scanf("%d", &num);
59         nums[i] = num;
60     }
61     sort(nums.begin(), nums.end());
62
63     vector<vector<Node*> > levels;
64
65     vector<Node*> last_level;
66     last_level.push_back(new Node());
67
68     levels.push_back(last_level);
69
70     int node_cnt = cnt - 1; // we already pushed the root node
71     while (node_cnt > 0) {
72         vector<Node*> cur_level = build_next_level(last_level, node_cnt);
73         levels.push_back(cur_level);
74         swap(last_level, cur_level);
75     }
76
77     int order = 0;
78     inorder_traverse(levels[0][0], order);
79
80     node_cnt = cnt;
81
82     for (auto iter_levels = levels.begin(); iter_levels != levels.end(); iter_levels++) {
83         vector<Node*>& cur_level = *iter_levels;
84         for (auto iter = cur_level.begin(); iter != cur_level.end(); iter++) {
85             Node* node = *iter;
86             int val = nums[node->val];
87             if (--node_cnt > 0) {
88                 printf("%d ", val);
89             } else {
90                 printf("%d\n", val); // last element
91             }
92         }
93     }
94     return 0;
95 }

又是跟书本比较近的一题

时间: 2024-11-06 13:26:40

PAT 1064 Complete Binary Search Tree的相关文章

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1064. Complete 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 tha

PAT Advanced Level 1064 Complete Binary Search Tree (30)(30 分)

1064 Complete Binary Search Tree (30)(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

1064 Complete 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 properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a n

PAT Advanced 1064 Complete 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

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

PAT (Advanced Level) 1064. Complete Binary Search Tree (30)

因为是要构造完全二叉树,所以树的形状已经确定了. 因此只要递归确定每个节点是多少即可. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<string> #include<stack> #include<map> #include<algorithm> using

PAT甲题题解-1064. Complete Binary Search Tree (30)-中序和层次遍历,水

由于是满二叉树,用数组既可以表示父节点是i,则左孩子是2*i,右孩子是2*i+1另外根据二分搜索树的性质,中序遍历恰好是从小到大排序因此先中序遍历填充节点对应的值,然后再层次遍历输出即可. 又是一道遍历的水题... #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <queue> using namespace std;

PAT:1064. Complete Binary Search Tree (30) AC

#include<stdio.h> #include<algorithm> using namespace std; const int MAX=1010; int n; int arr[MAX]; //存放原始数组 int arrI=0; int CBT[MAX]; //二叉排序树层序遍历序列[思维]中序遍历在数组中存放的就是层序遍历序列 void inorder(int root) { if(root>n) return; inorder(root*2); CBT[roo

pat(A) 1064. Complete Binary Search Tree(完全二叉树的中序建树)

代码: #include<cstdio> #include<algorithm> #define N 1005 using namespace std; int a[N]; int T[N]; int pos; int n; int cmp(int a,int b) { return a<b; } void Build(int i) { if(i>n) return; int l=i<<1; int r=l+1; Build(l); T[i]=a[pos++