二叉树的构建与前中后层序遍历(二叉树)

列出叶结点

对于给定的二叉树,本题要求你按从上到下、从左到右的顺序输出其所有叶节点。

输入格式:

首先第一行给出一个正整数 N(≤10),为树中结点总数。树中的结点从 0 到 N−1 编号。随后 N 行,每行给出一个对应结点左右孩子的编号。如果某个孩子不存在,则在对应位置给出 "-"。编号间以 1 个空格分隔。

输出格式:

在一行中按规定顺序输出叶节点的编号。编号间以 1 个空格分隔,行首尾不得有多余空格。

输入样例:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

输出样例:

4 1 5
#include<iostream>
#include<queue>
#include<vector>
#include<string> 
using namespace std;
struct node{
	int data;
	node* lchild;
	node* rchild;
};
vector<int> v[11];
int a[11];
node* create(int s){
	node* head = new node;
	head->data = s;
	if(v[s][0] != -1){
		head->lchild = create(v[s][0]);
	}else{
		head->lchild = NULL;
	}
	if(v[s][1] != -1){
		head->rchild = create(v[s][1]);
	}else{
		head->rchild = NULL;
	}
	return head;
}
void BFS(node* head){  //层序遍历如果左右子树都没数据时输出 
	int flag = 1;
	queue<node*> q;
	q.push(head);
	while(!q.empty()){
		node* temp = q.front();
		q.pop();
		if(temp->lchild==NULL && temp->rchild == NULL){ 
			if(flag == 0) printf(" ");
			printf("%d",temp->data);
			flag = 0;
			continue;
		}
		if(temp->lchild!=NULL) q.push(temp->lchild);
		if(temp->rchild!=NULL) q.push(temp->rchild);
	}
}
int main(){
	int n,num;
	cin>>n;
	string s1,s2;
	for(int i=0;i<n;i++){
		cin>>s1>>s2;
		if(s1!="-"){
			num = s1[0]-'0';
			v[i].push_back(num);	
			a[num] = 1;
		}else{
			v[i].push_back(-1);  //-1表示无数据 
		}
		if(s2!="-"){
			num = s2[0]-'0';
			v[i].push_back(s2[0]-'0');
			a[num] = 1;
		}else{
			v[i].push_back(-1);
		}
	}
	node* head = NULL;
	for(int i=0;i<n;i++){
		if(a[i]==0){
			head = create(i);
			break;
		}
	}
	BFS(head);
	return 0;
} 

树的遍历

给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(≤30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

输出样例:

4 1 6 3 5 7 2

 解题代码:

#include<iostream>
#include<queue>
using namespace std;
const int maxn = 31;
struct node{
	int data;
	node* lchild;
	node* rchild;
};
int post[maxn],in[maxn],n;//后序数组,中序数组,节点数 
//四个参数,为所要求树的后序起始和中序起始
node* create(int postL,int postR,int inL,int inR){
	if(postL > postR) 
		return NULL; //如果后序序列的长度小于0时,直接返回NULL 
	node* root = new node; //初始化根节点 
	root->data = post[postR];
	int k = 0;  //找出根节点在后序序列中的位置 
	for(int i=inL;i<=inR;i++){
		if(post[postR] == in[i]){
			k = i;
			break;
		} 
	}
	int numL = k - inL; //左子树的长度 
	root->lchild = create(postL, postL + numL - 1,inL,k-1);
	root->rchild = create(postL + numL, postR - 1, k + 1,inR);
}
void BFS(node* head){ //BFS层序遍历树 
	queue<node*> q;  //注意储存的是地址
	q.push(head);
	int cnt = 0;
	while(!q.empty()){
		node* temp = q.front();
		q.pop();
		cnt++;
		printf("%d",temp->data);
		if(cnt < n) printf(" ");
		if(temp->lchild != NULL) q.push(temp->lchild);
		if(temp->rchild != NULL) q.push(temp->rchild); 
	}
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>post[i];
	}
	for(int i=0;i<n;i++){
		cin>>in[i];
	}
	node* head = create(0,n-1,0,n-1); //根据后序和中序创建树 
	BFS(head); //层序遍历树 
	return 0;
}

实现二叉树先序,中序和后序遍历

链接:https://www.nowcoder.com/questionTerminal/566f7f9d68c24691aa5abd8abefa798c
来源:牛客网

分别按照二叉树先序,中序和后序打印所有的节点。

输入描述:

第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。

以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)

输出描述:

输出三行,分别表示二叉树的先序,中序和后序。

示例1

输入

3 1
1 2 3
2 0 0
3 0 0

输出

1 2 3
2 1 3
2 3 1

备注:

1≤n≤10000001 \leq n \leq 10000001≤n≤1000000
1≤root,fa,lch,rch≤n1 \leq root, fa, lch, rch \leq n1≤root,fa,lch,rch≤n
#include<iostream>
using namespace std;
struct node{
    int data;
    node* lchild;
    node* rchild;
};
int a[1000001][3];
node* create(int s){
    node* head = new node;
    head->data = s;
    if(a[s][1]!=0){
        head->lchild = create(a[s][1]);
    }else{
        head->lchild = NULL;
    }
    if(a[s][2]!=0){
        head->rchild = create(a[s][2]);
    }else{
        head->rchild = NULL;
    }
    return head;
}
void pre(node* root){   //先序遍历
    if(root == NULL) return ;
    printf("%d ", root->data);
    pre(root->lchild);
    pre(root->rchild);
}
void in(node* root){   //中序遍历
	if(root == NULL) return ;
    in(root->lchild);
    printf("%d ", root->data);
    in(root->rchild);
}
void post(node* root){ //后序遍历
	if(root == NULL) return ;
    post(root->lchild);
    post(root->rchild);
    printf("%d ", root->data);
}
int main()
{
    int n,root;
    int k,l,r;
    cin>>n>>root;
    for(int i=1;i<=n;i++){
    	cin>>k;
        cin>>a[k][1]>>a[k][2];
    }
    node* head = create(root);
    pre(head);
    printf("\n");
    in(head);
    printf("\n");
    post(head);
    return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值