一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点,
- 其左子树中所有结点的键值小于等于该结点的键值;
- 其右子树中所有结点的键值大于该结点的键值;
- 其左右子树都是二叉搜索树。
我们用map来记录节点和节点值, 随意选一个节点为根节点,递归建树,建树过程中可以记录树的深度
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i, a, b) for (int i = a; i <= b; i ++)
#define endl '\n'
#define fi first
#define se second
const int N = 1e3 + 10;
unordered_map<int, int> s;
vector<int> k, l, m, o, p;
void build(int x, int y) { //建树
if (!s.count(x)) {
s[x] = y;
return;
}
if (y < s[x]) build(x << 1, y);
else build(x << 1 | 1, y);
}
void bfs(int x) { //前序对比
k.push_back(s[x]);
if (s[x << 1]) bfs(x << 1);
if (s[x << 1 | 1]) bfs(x << 1 | 1);
l.push_back(s[x]);
}
void bfs1(int x) {
m.push_back(s[x]);
if (s[x << 1 | 1]) bfs1(x << 1 | 1);
if (s[x << 1]) bfs1(x << 1);
o.push_back(s[x]);
}
signed main() {
cin.tie(0), cout.tie(0)->sync_with_stdio(false);
int t = 1;
//cin >> t;
while (t --) {
int n;
cin >> n;
fo (i, 1, n) {
int x;
cin >> x;
p.push_back(x);
build(1, x);
}
bfs(1), bfs1(1);
//for (auto i: ) cout << i << " ";
if (k == p || m == p) {
cout << "YES" << endl;
if ((p == k && m == p) || p == k) {
fo (i, 0, n - 1) cout << l[i] << " "[i == n - 1];
}
else fo (i, 0, n - 1) cout << o[i] << " "[i == n - 1];
}
else cout << "NO";
}
return 0;
}
若是一颗完全二叉搜索树
由于二叉搜索树中序遍历是递增的,我们先把数组排序再
采用id * 2, id * 2 + 1,方式建树
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i, a, b) for (int i = a; i <= b; i ++)
#define endl '\n'
#define fi first
#define se second
const int N = 1e3 + 10;
int a[N];
int n, cnt;
map<int, int> s;
void build(int x) {
if (x > n) return;
build(x << 1);
s[x] = a[++ cnt];
build(x << 1 | 1);
}
signed main() {
cin.tie(0), cout.tie(0)->sync_with_stdio(false);
int t = 1;
//cin >> t;
while (t --) {
int x;
cin >> n;
fo (i, 1, n) cin >> a[i];
sort(a + 1, a + 1 + n);
build(1);
for (auto i: s) cout << i.se << " ";
}
return 0;
}
给出左右节点
通过中序遍历建树
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int n;
int l[N] ,r[N]; //左儿子数组,右儿子数组。
int q[N]; //数组模拟队列,用于bfs()
int a[N] ,w[N]; //a存储读入的数值, w存储每个结点实际存的哪个数
void dfs(int u, int& k){
if(u == -1) return;
dfs(l[u], k);
w[u] = a[k++];//中序遍历,每个结点得到权值
dfs(r[u], k);
}
//层序遍历,就是广度优先搜索
void bfs(){
int hh =0 ,tt =0;
q[0] =0;
while(hh <= tt){
int t = q[hh++];
if(l[t] != -1) q[++tt] =l[t];
if(r[t] != -1) q[++tt] = r[t];
}
//输出层序遍历
cout<<w[q[0]];
for(int i=1; i<n; i++) cout<<" "<<w[q[i]];
}
int main(){
cin>> n;
for( int i = 0 ;i< n; i++) cin>> l[i] >> r[i];
for(int i =0; i< n; i++) cin>> a[i];
sort(a, a + n ); //二叉排序树的中序遍历序列,从小到大
int k = 0;
dfs(0, k); //dfs 按照中序遍历的顺序,往里填数据
bfs(); //层序遍历
}