求二叉树的层次遍历oj

求二叉树的层次遍历

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
已知一颗二叉树的前序遍历和中序遍历,求二叉树的层次遍历。
Input
输入数据有多组,输入T,代表有T组测试数据。每组数据有两个长度小于50的字符串,第一个字符串为前序遍历,第二个为中序遍历。
Output
每组输出这颗二叉树的层次遍历。
Example Input
2
abc
bac
abdec
dbeac
Example Output
abc
abcde
Hint
 
Author
fmh
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
    char data;
    struct bitree *l,*r;
};
char a[100],b[55];//a前序b中序
int l1,h1,l2,h2;
struct node *creat() //前序建立
{
    struct node *root;
    char c;
    c = a[l1++];
    if(c == ',')
        return NULL;
    else
    {
        root = (struct node *)malloc(sizeof(struct node));
        root->data = c;
        root->l=creat();
        root->r=creat();
    }
    return root;
}
struct node *creat2(int n,char *a,char *b)//前序中序建立
{
    struct node *root;
    int i;
    if(n == 0)
        return NULL;
    root = (struct node *)malloc(sizeof(struct node));
    root->data=a[0];
    for(i = 0;i < n;i++)
    {
        if(b[i] == a[0])
            break;
    }
    root->l=creat2(i,a+1,b);
    root->r=creat2(n-i-1,a+i+1,b+i+1);

    return root;
}
struct node *creat3(int n,char *a,char *b)//中序后序建立
{
    struct node *root;
    int i;
    if(n == 0)
        return NULL;
    root = (struct node *)malloc(sizeof(struct node));
    root->data=b[n-1];
    for(i = 0;i < n;i++)
    {
        if(a[i] == b[n-1])
            break;
    }
    root->l=creat3(i,a,b);
    root->r=creat3(n-i-1,a+i+1,b+i);
    return root;
}
void preorder(struct node *root) //前序遍历
{
    struct node *p;
    p = root;
    if(p!=NULL)
    {
        printf("%c",p->data);
        preorder(p->l);
        preorder(p->r);
    }
}
void inorder(struct node *root)//中序遍历
{
    struct node *p;
    p = root;
    if(p!=NULL)
    {
        inorder(p->l);
        printf("%c",p->data);
        inorder(p->r);
    }
}
void postorder(struct node *root)//后序遍历
{
    struct node *p;
    p = root;
    if(p!=NULL)
    {
        postorder(p->l);
        postorder(p->r);
        printf("%c",p->data);
    }
}
void lorder(struct node *t)//层次遍历
{
    if(!t)          //考虑空树
        return ;
    struct node *q[100000],*p;
    int f,r;
    q[1] = t,f=r=1;
    while(f<=r)
    {
        p = q[f];
        f++;
        printf("%c",p->data);
        if(p->l!=NULL)
        {
            r++;
            q[r]=p->l;
        }
        if(p->r!=NULL)
        {
            r++;
            q[r]=p->r;
        }
    }
}
int treehight(struct node *t) //二叉数高度
{
    int h,lh,rh;
    if(t == NULL)
    {
        h = 0;
    }
    else
    {
        lh = treehight(t->l);
        rh = treehight(t->r);
        if(lh>rh)
            h = lh+1;
        else
            h = rh+1;
    }
    return h;
}
int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",a,b);
        struct node *root;
        n = strlen(a);
        root = creat2(n,a,b);
        lorder(root);
        printf("\n");
    }
    return 0;
}

以下是使用 C++ 实现二叉树层次遍历的示例代码: ```c++ #include <iostream> #include <queue> using namespace std; // 定义二叉树结构体 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 二叉树层次遍历函数 void levelOrder(TreeNode* root) { // 如果根节点为空,则直接返回 if (!root) return; // 创建队列,存储每一层的节点 queue<TreeNode*> q; // 将根节点添加到队列中 q.push(root); // 循环遍历队列中的节点,直到队列为空 while (!q.empty()) { // 获取队列中的节点数量,也就是当前层的节点数量 int size = q.size(); // 循环遍历当前层的所有节点 for (int i = 0; i < size; i++) { // 获取队列中的头节点,并将其弹出 TreeNode* node = q.front(); q.pop(); // 输出当前节点的值 cout << node->val << " "; // 如果当前节点有左子节点,则将其添加到队列中 if (node->left) q.push(node->left); // 如果当前节点有右子节点,则将其添加到队列中 if (node->right) q.push(node->right); } // 输出换行符,表示当前层已经遍历完成 cout << endl; } } int main() { // 创建二叉树 TreeNode *root = new TreeNode(1); root->left = new TreeNode(2); root->right = new TreeNode(3); root->left->left = new TreeNode(4); root->left->right = new TreeNode(5); root->right->left = new TreeNode(6); root->right->right = new TreeNode(7); // 二叉树层次遍历 levelOrder(root); return 0; } ``` 其中,我们使用队列来存储每一层的节点,然后循环遍历队列中的节点,并对于每个节点,输出其值,并将其左右子节点添加到队列中。当当前层的所有节点遍历完成后,我们输出一个换行符,表示当前层已经遍历完成。最后,我们的二叉树层次遍历就完成了。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值