头歌_二叉树

第一关:

第一关
#pragma once
# include <bits/stdc++.h>
using namespace std;

namespace exa{

struct bnode {
    struct bnode * lchild, * rchild;
    char data;
};
typedef bnode * bitre;

//读取扩展二叉树的先序序列来构建原二叉树
static void load_bitre(bitre & t) {
     char c;
        scanf("%c", &c);
        if (c == '.')
        {
            t = NULL;//指针为空
            return;
        }
        else
        {
            bitre temp = (bitre)malloc(sizeof(struct bnode));//分配新地址
            if (!temp) return;//分配不成功
            temp->data = c;
            t = temp;
            load_bitre(t->lchild);
            load_bitre(t->rchild);
        }
}

//输出二叉树的先序和后序遍历序列
//输出格式为:
//PreOrder: xxxxxxx
//InOrder: xxxxxxx
 static void pre(bitre t) {
        printf("%c", t->data);
        if (t->lchild) pre(t->lchild);
        if (t->rchild) pre(t->rchild);
    }
    static void mid(bitre t) {
        if (t->lchild) mid(t->lchild);
        printf("%c", t->data);
        if (t->rchild) mid(t->rchild);
    }
    static void display_bitre(bitre& root) {
        printf("PreOrder: ");
        pre(root);//先序,根左右
        printf("\n");
        printf("InOrder: ");
        mid(root);//中序,左根右
    }

}
第二关
#include "test.h"
#include "../btrechar.h"  // 引用库函数文件
namespace exa {     //请在命名空间内编写代码,否则后果自负

void visite_leaf(bitre t)
{
        if (t != NULL)//Blank 1
        {
            if (t->lchild == NULL && t->rchild == NULL) {
                cout << endl;
                cout << t->data;
            }
            visite_leaf(t->lchild);                 // 递归调用算法对左子树执行
            visite_leaf(t->rchild);                 // 递归调用算法对右子树执行
        }
}
void solve()
{
    bitre t;
    load_bitre(t);                              // 取出二叉树
    display_bitre(t);                   // 显示二叉树
    visite_leaf(t);                             // 调用算法运行以检验
}

}
第三关
#include "test.h"
#include "../btrechar.h"  // 引用库函数文件
namespace exa {     //请在命名空间内编写代码,否则后果自负

void bitrepreorder(bitre t)
{
    if (t != NULL){
         putchar(t->data);

        bitrepreorder(t->lchild);       // 递归调用算法对t的左子树执行算法
       
        bitrepreorder(t->rchild);       // 递归调用算法对t的右子树执行算法
    }
}
void solve()
{
    bitre t;
    load_bitre(t);
    display_bitre(t);
    cout<<endl;
    bitrepreorder(t);
    puts("");
}

}
第四关
#include "test.h"
#include "../btrechar.h"  // 引用库函数文件
namespace exa {     //请在命名空间内编写代码,否则后果自负
#define MAX(a,b)((a)>(b)?(a):(b))
int solve(bitre & t) {

       if (t == NULL) return 0;
        int len = 0;
        if (t->lchild)
        {
            int lh = solve(t->lchild);
            len = MAX(lh, len);
        }
        if (t->rchild)
        {
            int rh = solve(t->rchild);
            len = MAX(len, rh);
        }
        return len + 1;
}

}
第五关
#include "test.h"
#include "../btrechar.h"  // 引用库函数文件
namespace exa {     //请在命名空间内编写代码,否则后果自负

int cnt = 1;
	//中序遍历,左根右
	void solve(bitre& t) {
		if (t == NULL) return;
		cnt++;
		if (t->lchild) solve(t->lchild);
		printf("%c %d\n", t->data, cnt - 1);//因为这个点为子树头结点
		if (t->rchild) solve(t->rchild);
		cnt--;//一定要加,回溯
	}

}
第六关
#include "test.h"
#include "../btrechar.h"  // 引用库函数文件
namespace exa {     //请在命名空间内编写代码,否则后果自负

    bitre Create(char*, int, int);
    bitre solve(char* c, int n) {
        return Create(c, 0, n);
    }
    bitre Create(char* c, int i, int n)
    {//建树
        bitre temp = NULL;//创建新空指针
        if (i < n && c[i] != '.')
        {
            temp = (bitre)malloc(sizeof(struct bnode));//分配内存
            temp->data = c[i];//头
            temp->lchild = Create(c, 2 * i + 1, n);//递归创建左子树
            temp->rchild = Create(c, 2 * i + 2, n);
        }
        return temp;
    }
}
第七关
#include "test.h"
#include "../btrechar.h"  // 引用库函数文件
namespace exa {     //请在命名空间内编写代码,否则后果自负

bitre solve(bitre & t) {
  if(t==NULL) return NULL;
    bitre newt=new bnode();//创建新指针
    newt->data=t->data;
    newt->lchild=solve(t->lchild);
    newt->rchild=solve(t->rchild);
    return newt;
}

}
第八关
#include "test.h"
#include "../btrechar.h"  // 引用库函数文件
namespace exa {     //请在命名空间内编写代码,否则后果自负


void solve(bitre & t) {
 if(t==NULL) return;
    if(t->lchild) solve(t->lchild);
    if(t->rchild) solve(t->rchild);
    bitre temp=t->lchild;
    t->lchild=t->rchild;
    t->rchild=temp;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值