pat-玩转二叉树

L2-011. 玩转二叉树

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。

输入格式:

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

输出格式:

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

输入样例:
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
输出样例:
4 6 1 7 5 3 2


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{
    int data;
    struct node*l;
    struct node*r;
}tree;
tree* creat1(int xian[],int zhong[] ,int len)
{
    if(len<=0)
    return NULL;
    tree*head = new tree;
    head->data = *xian;
    int i = 0;
    for(;i<len;i++)
    {
        if(zhong[i]==*xian)
        {
            break;
        }
    }
    head->l = creat1(xian+1,zhong,i);
    head->r = creat1(xian+i+1,zhong+i+1,len-i-1);
    return head;
}
void cccout(tree*root)
{
    tree*p = NULL;
    queue<tree*>g;
    if(root)
    {
        g.push(root);
    }
    while(!g.empty())
    {
        p = g.front();
        g.pop();
        cout<<p->data<<" ";
        if(p->l)
        {
            g.push(p->l);
        }
        if(p->r)
        {
            g.push(p->r);
        }
    }
}
void xxian(tree*root)
{
    if(root)
    {
        cout<<root->data<<" ";
        xxian(root->l);
        xxian(root->r);
    }
}
void ccout(tree*root)
{
    queue<tree*>q;
    tree*p =NULL;
    if(root)
    {
      q.push(root);
    }
    int top = 1;
    while(!q.empty())
    {
        p = q.front();
        q.pop();
        if(top)top =0;
        else printf(" ");
        cout<<p->data;

        if(p->l)
        {
           q.push(p->l);
        }
         if(p->r)
        {

           q.push(p->r);
        }


    }


}
void swap(tree*root)
{
    if(!root) return ;
    swap(root->l);
    swap(root->r);
    swap(root->l,root->r);

}//或者在层序遍历的时候倒过来
int main()
{
    int n;
    cin>>n;
    int xian[36],zhong[36];
    for(int i=0;i<n;i++)
    {
        cin>>zhong[i];
    }
    for(int i=0;i<n;i++)
    {
        cin>>xian[i];
    }
    tree*root = new tree;
    root = creat1(xian,zhong,n);
    swap(root);
    ccout(root);
    return 0;
}
静态数组版
#include  <iostream>
#include  <cstdio>
#include  <cstring>
#include  <queue>
using namespace std;
const int maxn=1000000;
int n,num(0);
int pre[maxn],in[maxn],tree[maxn];

void build(int tag,int left,int right)
{
	tree[tag]=pre[++num];
	if(left==right)
		return;
	int index=-1;
	for(int i=left;i<=right;i++)
	{
		if(in[i]==tree[tag])
		{
			index=i;
			break;
		}
	}
	if(index==-1)
		return;
	if(index-1>=left)
		build(tag<<1,left,index-1);
	if(index+1<=right)
		build(tag<<1|1,index+1,right);
}

void bfs()
{
	queue<int> que;
	que.push(1);
	int tag=tree[1];
	while(!que.empty())
	{
		int temp=que.front();
		que.pop();
		if(tree[temp]!=tag)
			printf(" ");
		printf("%d",tree[temp]);
		if(tree[temp*2+1]!=0)
			que.push(temp*2+1);
		if(tree[temp*2]!=0)
			que.push(temp*2);
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&in[i]);
	for(int i=1;i<=n;i++)
		scanf("%d",&pre[i]);
	build(1,1,n);
	bfs();
	printf("\n");
	return 0;
}

 



### 二叉树的基础概念 二叉树是一种重要的数据结构,其中每个节点最多有两个子节点,通常称为左孩子和右孩子。这种结构允许快速查找、插入和删除操作,在许多应用场景中表现出色[^1]。 ### 特殊类型的二叉树 几种特殊形式的二叉树具有特定属性: - **满二叉树**:除了最后一层外,每一层上的所有结点都有两个子结点。 - **完全二叉树**:除最后一层外,这是个满二叉树;而在最后一层上只缺少右边若干子结点。 - **平衡二叉树**:它是一棵空树或它的任意节点的两颗子树的高度差不超过1。 ### 实现基本操作 为了更好地理解如何处理二叉树,下面展示了一个简单的Python类定义来表示一棵二叉树及其一些基础方法: ```python class TreeNode: def __init__(self, value=0, left=None, right=None): self.value = value self.left = left self.right = right def insert_into_binary_tree(root, val): if not root: return TreeNode(val) queue = [root] while queue: current_node = queue.pop(0) if not current_node.left: current_node.left = TreeNode(val) break else: queue.append(current_node.left) if not current_node.right: current_node.right = TreeNode(val) break else: queue.append(current_node.right) return root ``` 这段代码实现了向二叉树中插入新值的功能。这里采用广度优先遍历的方式找到第一个可以放置新节点的位置并完成插入动作。 ### 应用场景举例 了解了上述理论之后,就可以利用二叉树解决实际问题了。比如在文件系统的目录结构里,每一个文件夹都可以看作是一个父级节点,其下的文件或者子文件夹则是对应的子节点。同样地,在数据库索引构建方面也广泛应用到了B+树这样的变种二叉树结构,提高了查询速度。另外,在编译原理当中,抽象语法树也是基于类似的思路建立起来用于表达源码逻辑关系的一种重要工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值