根据中序和后序遍历推导层次遍历

输入:总的结点个数n
中序序列
后序序列
输出:层次遍历序列
注意输出的最后没有空格

测试样例:
输入:
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>
#include <algorithm>
using namespace std;

const int MAX=50;
struct Node 
{
	int val; 
	Node* lchild;
	Node* rchild;
	Node(int a)
	{
		val=a;
		lchild=NULL;
		rchild=NULL;
	}
	Node()
	{
		val=0;
		lchild=NULL;
		rchild=NULL;
	}
};
int in[MAX],post[MAX];
int n;


//传入后序序列及其起始坐标,中序序列及其起始坐标,总的结点数 
Node* createTree(int post[],int i,int in[],int j,int n)
{
	Node* t;
	int ch;
	int p,k;
	if(n<=0) return NULL;
	ch=post[i+n-1];//根结点 
	t=new Node(ch);
	p=j;//临时记录头结点位置 
	while(p<j+n)//寻找根结点 
	{
		if(in[p]==ch)
		break;
		p++;
	}
	k=p-j;//左子树的长度 
	t->lchild=createTree(post,i,in,j,k);
	t->rchild=createTree(post,i+k,in,p+1,n-k-1);
	return t;
	
} 

void BFS(Node* root)
{
	int num=0;//用来记录当前的结点的个数,作用是确保输出的最后没有空格 
	queue<Node*> q;
	q.push(root);
	while(!q.empty())
	{
		Node* now=q.front();
		q.pop();
		cout<<now->val;
		num++;
		if(num<n) cout<<" ";//确保最后一个输出的后面没有空格 
		if(now->lchild) q.push(now->lchild);
		if(now->rchild) q.push(now->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* root=createTree(post,0,in,0,n);
	BFS(root);
	return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好,这是一个比较全面的二叉树操作实验,我们可以一步一步来完成。 1. 按先序序列构造一棵二叉链表表示的二叉树T 先序序列构造二叉树的方法是:从先序序列中取出一个元素作为根节点,然后将剩余序列分成左子树和右子树两个部分,递归构造左右子树。代码如下: ```c typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* buildTree(int* preorder, int preorderSize) { if (preorderSize == 0) { return NULL; } TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->data = preorder[0]; int i = 1; while (i < preorderSize && preorder[i] < root->data) { i++; } root->left = buildTree(preorder + 1, i - 1); root->right = buildTree(preorder + i, preorderSize - i); return root; } ``` 2. 对这棵二叉树进行递归遍历 二叉树的递归遍历有四种:先序遍历、中序遍历后序遍历层次遍历。代码如下: ```c void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->data); preorderTraversal(root->left); preorderTraversal(root->right); } void inorderTraversal(TreeNode* root) { if (root == NULL) { return; } inorderTraversal(root->left); printf("%d ", root->data); inorderTraversal(root->right); } void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->data); } void levelorderTraversal(TreeNode* root) { if (root == NULL) { return; } Queue* q = createQueue(); enqueue(q, root); while (!isEmpty(q)) { TreeNode* node = dequeue(q); printf("%d ", node->data); if (node->left != NULL) { enqueue(q, node->left); } if (node->right != NULL) { enqueue(q, node->right); } } } ``` 3. 对这棵树用非递归方式进行遍历 非递归遍历需要借助栈的数据结构,对于先序、中序后序遍历,分别有不同的实现方法。代码如下: ```c void preorderTraversal2(TreeNode* root) { if (root == NULL) { return; } Stack* s = createStack(); push(s, root); while (!isEmpty(s)) { TreeNode* node = pop(s); printf("%d ", node->data); if (node->right != NULL) { push(s, node->right); } if (node->left != NULL) { push(s, node->left); } } } void inorderTraversal2(TreeNode* root) { if (root == NULL) { return; } Stack* s = createStack(); TreeNode* node = root; while (node != NULL || !isEmpty(s)) { while (node != NULL) { push(s, node); node = node->left; } node = pop(s); printf("%d ", node->data); node = node->right; } } void postorderTraversal2(TreeNode* root) { if (root == NULL) { return; } Stack* s1 = createStack(); Stack* s2 = createStack(); push(s1, root); while (!isEmpty(s1)) { TreeNode* node = pop(s1); push(s2, node); if (node->left != NULL) { push(s1, node->left); } if (node->right != NULL) { push(s1, node->right); } } while (!isEmpty(s2)) { TreeNode* node = pop(s2); printf("%d ", node->data); } } ``` 4. 求二叉树的深度、结点数目、叶结点数目 二叉树的深度可以递归地求解,结点数目和叶结点数目也可以通过递归遍历的方式求解。代码如下: ```c int maxDepth(TreeNode* root) { if (root == NULL) { return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return 1 + (leftDepth > rightDepth ? leftDepth : rightDepth); } int countNodes(TreeNode* root) { if (root == NULL) { return 0; } return 1 + countNodes(root->left) + countNodes(root->right); } int countLeaves(TreeNode* root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } return countLeaves(root->left) + countLeaves(root->right); } ``` 5. 将二叉树每个结点的左右子树交换位置 交换左右子树的方法也可以递归实现。代码如下: ```c void swap(TreeNode* root) { if (root == NULL) { return; } TreeNode* temp = root->left; root->left = root->right; root->right = temp; swap(root->left); swap(root->right); } ``` 6. 设计二叉树的双序遍历算法 双序遍历是一种比较特殊的遍历方式,它可以由先序和中序遍历序列推导出来。具体实现方法可以参考以下代码: ```c void doubleOrderTraversal(TreeNode* root) { if (root == NULL) { return; } printf("%d ", root->data); doubleOrderTraversal(root->left); printf("%d ", root->data); doubleOrderTraversal(root->right); } ``` 7. 计二叉树最大宽度 二叉树的最大宽度可以通过层次遍历的方式求解。代码如下: ```c int maxWidth(TreeNode* root) { if (root == NULL) { return 0; } Queue* q = createQueue(); enqueue(q, root); int maxWidth = 0; while (!isEmpty(q)) { int size = getSize(q); maxWidth = size > maxWidth ? size : maxWidth; for (int i = 0; i < size; i++) { TreeNode* node = dequeue(q); if (node->left != NULL) { enqueue(q, node->left); } if (node->right != NULL) { enqueue(q, node->right); } } } return maxWidth; } ``` 8. 求二叉树中第一条最长的路径长度,并输出此路径上各结点 求解二叉树中第一条最长路径的方法是:递归遍历每个结点,计左子树和右子树的深度之和,并记录下经过当前结点的路径长度。代码如下: ```c void findLongestPath(TreeNode* root, int* maxLen, Stack* pathStack) { if (root == NULL) { return; } Stack* leftStack = createStack(); Stack* rightStack = createStack(); findLongestPath(root->left, maxLen, leftStack); findLongestPath(root->right, maxLen, rightStack); int leftDepth = getDepth(leftStack); int rightDepth = getDepth(rightStack); int len = leftDepth + rightDepth; if (len > *maxLen) { *maxLen = len; while (!isEmpty(pathStack)) { pop(pathStack); } while (!isEmpty(leftStack)) { push(pathStack, pop(leftStack)); } push(pathStack, root->data); while (!isEmpty(rightStack)) { push(pathStack, pop(rightStack)); } } push(leftStack, root->data); push(rightStack, root->data); } void printStack(Stack* s) { while (!isEmpty(s)) { printf("%d ", pop(s)); } printf("\n"); } void longestPath(TreeNode* root) { int maxLen = 0; Stack* pathStack = createStack(); findLongestPath(root, &maxLen, pathStack); printStack(pathStack); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值