BODA二叉树遍历

4 篇文章 0 订阅
2 篇文章 0 订阅

前言 (曹老师yyds)

题目

问题A
问题B
问题C

感受

三道题都是关于二叉树遍历的题目,感觉没啥,就是证明了我是个菜鸡,感谢曹老师,让我有了继续努力的动力。

代码

A题代码

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
#define maxLen 35
LL tree[maxLen][maxLen] = { 0 };
LL read[maxLen][maxLen] = { 0 };
void ReadWriter(int m, int n)
{
	printf("%lld ",read[m][n]);
	if (m <= read[m][n] - 1)
	{
		ReadWriter(m, read[m][n]-1);
	}
	if (read[m][n] + 1 <= n)
	{
		ReadWriter(read[m][n]+1, n);
	}
}
int main()
{
	int n;
	scanf("%d", &n);
	tree[0][0] = 0;
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &tree[i][i]);
		tree[i][i - 1] = 1;
		read[i][i] = i;
	}
	tree[n][n + 1] = 1;
	for (int i = n; i >= 1; i--)
	{
		for (int j = i; j <= n; j++)
		{
			if (tree[i][j] == 0)
			{
				for (int k = i; k <= j; k++)
				{
					if (tree[i][k - 1] * tree[k + 1][j] + tree[k][k] > tree[i][j])
					{
						tree[i][j] = tree[i][k - 1] * tree[k + 1][j] + tree[k][k];
						read[i][j] = k;
					}
				}
			}
		}
	}
	printf("%lld\n", tree[1][n]);
	ReadWriter(1, n);
	printf("\n");
	return 0;
}

b题代码

#include <iostream>
#include <malloc.h>
#include <string.h>
#define TRUE  1
#define FALSE 0
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW    -2
#define OK 1
#define MAXQSIZE 100

using namespace std;

typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
	 TElemType data;
	BiTNode *lchild, *rchild;
}BiTNode,*BiTree;
typedef BiTree QElemtype;
typedef struct
{
	QElemtype *base;
	int front;
	int rear;
}SqQueue;

//3.h
Status CreatBitree(BiTree &T)//先序建立二叉树
{
	TElemType ch;
  	cin >> ch;
	if (ch == '@')
	{
		T = NULL;

	}
	else
	{
		T = (BiTNode *)malloc(sizeof(BiTNode));
		if (!T)
			return OVERFLOW;
		T->data = ch;
		CreatBitree(T->lchild);
		CreatBitree(T->rchild);
	}

	return OK;
}

void inOder(BiTree T)//中序遍历
{
	if (T)
	{
		inOder(T->lchild);
		cout << T->data;
		inOder(T->rchild);
	}
//    cout << endl;
}
void PreOder(BiTree T)//先序遍历
{
	if (T)
	{
		cout << T->data;
		PreOder(T->lchild);
		PreOder(T->rchild);
	}
//	cout << endl;
}

void PostOder(BiTree T)
{
	if (T)
	{
		PostOder(T->lchild);
		PostOder(T->rchild);
		cout << T->data;
	}

}

int main()
{
	BiTree T;
	//cout << "请输入数据:";
	CreatBitree(T);
		PreOder(T);
		cout << endl;
		inOder(T);
		cout << endl;
		PostOder(T);

}


C题代码

#include <bits/stdc++.h>

using namespace std;

void behind(string pre, string in)
{
     if(in.size() > 0)
     {
        char ch = pre[0];
         int k = in.find(ch);
         behind(pre.substr(1,k), in.substr(0,k));
         behind(pre.substr(k+1),in.substr(k+1));
         cout<<ch;
     }
}

int main(void)
{
    string preorder, inorder;
    cin >> preorder >> inorder;
    behind(preorder, inorder);
    cout << endl;
  return 0;
}

知识点

1.创建二叉树
2.二叉树的遍历
这些以后再写吧。先水到这。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值