文章

12

粉丝

0

获赞

12

访问

630

头像
层次遍历 题解:
P1924 复旦大学2023年机试题
发布于2026年2月25日 15:09
阅读数 12

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int a[N];
int n;
int idx = 0;

typedef struct node{
	int data;
	node* lchild;
	node* rchild;
}*BiTree;

BiTree build(int minv, int maxv){
	if(idx >= n)
		return NULL;
	if(a[idx] < minv || a[idx] > maxv)
		return NULL;
	
	BiTree root = new node;
	root->data = a[idx];
	root->lchild = NULL;
	root->rchild = NULL;
	
	idx++;
	root->lchild = build(minv,root->data);
	root->rchild = build(root->data,maxv);
	
	return root;
}

void LevelTravel(BiTree T){
	queue<node*> q;
	q.push(T);
	
	while(!q.empty()){
		node* t = q.front();
		q.pop();
		
		cout<<t->data<<' ';
		
		if(t->lchild != NULL){
			q.push(t->lchild);
		}
		if(t->rchild != NULL){
			q.push(t->rchild);
		}
		
	}
	
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	

	cin>>n;
	for(int i = 0 ; i < n; i ++){
		cin>>...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发