二叉树遍历(flist)(4月3日)

给定中序遍历和层次遍历的序列,通过深度优先搜索(DFS)策略,重构二叉树并打印其先序遍历序列。代码使用C++实现。

说明

输出格式

输出文件 flist.out 就一行,表示二叉树的先序序列。

样例

输入数据 1

DBEAC
ABCDE

输出数据 1

ABDEC

解题思路

给出中序遍历和层次遍历的序列,需要求出该树的先根序列。我们知道层次遍历的序列是从每层左到右的顺序给出,所以在层次遍历的序列中同一层的左子树肯定比右子树的结点先出现。而中序遍历的序列中的任一个结点的左子树和右子树(如果有)肯定是在该结点两边的,所以在遍历层次序列的结点时,查找该结点在先序序列中的位置,并且该结点的左子树在左边,右子树在右边,用dfs深搜,先搜左子树,再搜右子树。

完整代码:

#include<iostream>
#include<string>
#define MAXN 256
using namespace std;
string s1,s2;
int a[MAXN];
void dfs(int l=0,int r=s1.length()-1){
	if(l>=s1.length() || r<l)//超出范围了 
		return;
	int tr=-1;
	for(int i=0;i<s2.length();i++)
	{ 
		int op=0;//op作开关,先关 
		if(a[s2[i]-'0']!=0)//这个结点已遍历 
			continue;
		for(int j=l;j<=r;j++)
		{
			if(s2[i] != s1[j])
				continue;
			tr=j;//tr是该子树的根的下标 
			a[s2[i]-'0']=1;//结点已遍历 
			cout << s2[i];
			op=1;//找到了该子树的根,开 
			break;
		}
		if(op)//如果开关开了,退出 
			break;
	}
	if(tr==-1)//如果找不到 
		return;
	dfs(0,tr-1);//递归左子树 
	dfs(tr+1,r);//递归右子树 
}
int main()
{
	cin >> s1 >> s2;
	dfs();
	return 0;
} 

 注:新手的每日一题,如有错误请指出(谢谢*——*)

这里提供一个使用链表实现的二叉树遍历代码: ```c #include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; typedef struct Node { TreeNode* data; struct Node* next; } Node; typedef struct { Node* head; Node* tail; } Flist; // 初始化链表 void init_list(Flist* flist) { flist->head = NULL; flist->tail = NULL; } // 在链表尾部插入节点 void append(Flist* flist, TreeNode* data) { Node* new_node = (Node*)malloc(sizeof(Node)); new_node->data = data; new_node->next = NULL; if (flist->head == NULL) { flist->head = new_node; } else { flist->tail->next = new_node; } flist->tail = new_node; } // 删除链表头部节点 TreeNode* pop(Flist* flist) { if (flist->head == NULL) { return NULL; } Node* temp_node = flist->head; TreeNode* temp_data = flist->head->data; if (flist->head == flist->tail) { flist->tail = NULL; } flist->head = flist->head->next; free(temp_node); return temp_data; } // 前序遍历 void preorder(TreeNode* root) { if (root == NULL) { return; } Flist flist; init_list(&flist); append(&flist, root); while (flist.head != NULL) { TreeNode* current_node = pop(&flist); printf("%d ", current_node->data); if (current_node->right != NULL) { append(&flist, current_node->right); } if (current_node->left != NULL) { append(&flist, current_node->left); } } } // 中序遍历 void inorder(TreeNode* root) { if (root == NULL) { return; } Flist flist; init_list(&flist); TreeNode* current_node = root; while (current_node != NULL || flist.head != NULL) { if (current_node != NULL) { append(&flist, current_node); current_node = current_node->left; } else { current_node = pop(&flist); printf("%d ", current_node->data); current_node = current_node->right; } } } // 后序遍历 void postorder(TreeNode* root) { if (root == NULL) { return; } Flist flist; init_list(&flist); TreeNode* current_node = root; TreeNode* last_visited_node = NULL; while (current_node != NULL || flist.head != NULL) { if (current_node != NULL) { append(&flist, current_node); current_node = current_node->left; } else { TreeNode* temp_node = flist.head->data; if (temp_node->right != NULL && temp_node->right != last_visited_node) { current_node = temp_node->right; } else { printf("%d ", temp_node->data); last_visited_node = pop(&flist); } } } } // 创建新节点 TreeNode* new_node(int data) { TreeNode* new_node = (TreeNode*)malloc(sizeof(TreeNode)); new_node->data = data; new_node->left = NULL; new_node->right = NULL; return new_node; } int main() { // 创建二叉树 TreeNode* root = new_node(1); root->left = new_node(2); root->right = new_node(3); root->left->left = new_node(4); root->left->right = new_node(5); root->right->left = new_node(6); root->right->right = new_node(7); // 遍历二叉树 printf("前序遍历:"); preorder(root); printf("\n"); printf("中序遍历:"); inorder(root); printf("\n"); printf("后序遍历:"); postorder(root); printf("\n"); return 0; } ``` 该代码使用了一个自定义的链表结构体`Flist`,其中的`init_list`、`append`和`pop`函数分别用于初始化链表、在链表尾部插入节点和删除链表头部节点。在遍历过程中,需要用到一个链表来存储待处理的节点。对于前序遍历,从根节点开始,先输出当前节点的值,然后将其右子节点和左子节点分别加入链表中,这样在下一次循环时就会先处理左子节点。对于中序遍历,从根节点开始,先将其和其所有左子节点加入链表中,然后依次弹出链表头部节点,输出其值,并将其右子节点加入链表中。对于后序遍历,从根节点开始,先将其和其所有左子节点加入链表中,然后依次弹出链表头部节点,如果该节点的右子节点存在且未被处理过,则将其右子节点加入链表中,否则输出该节点的值并将其标记为已处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值