有关二叉树的常见题目

本文介绍了二叉树的相关操作,包括将二叉树转换为镜像树的算法,判断一个二叉树是否是另一个的子树,将有序二叉树转化为双向链表的方法,以及如何进行二叉树的之字形遍历。此外,还涉及到了二叉树的其他操作如查找、删除和平衡性判断。
摘要由CSDN通过智能技术生成

将二叉树转镜像树

void mirror_tree(TreeNode* root)
{
	if(NULL == root) return;
	TreeNode* temp=root->left;
	root->left=root->right;
	root->right=temp;
	mirror_tree(root->left);
	mirror_tree(root->right);
}

判断B二叉树是否是A二叉树的子树

//比较两棵树是否包含
bool _contain_tree(TreeNode* r1,TreeNode* r2)//依赖函数
{
	if(NULL == r1 && NULL == r2) return true;
	if(NULL == r1 && NULL != r2) return false;
	if(NULL != r1 && NULL == r2) return true;
	if(r1->data != r2->data) return false;
	return _contain_tree(r1->right,r2->right) && _contain_tree(r1->left,r2->left);
}
bool is_subtree(TreeNode* Aroot,TreeNode* Broot)
{
	if(NULL == Aroot) return false;
	if(Aroot->data == Broot->data)
	{
		if(_contain_tree(Aroot,Broot)) return true;
	}

	return is_subtree(Aroot->left,Broot) || is_subtree(Aroot->right,Broot);
}

将由于二叉树转有序双向链表

//	链表尾添加
void __add_tail_list(TreeNode** head,TreeNode* node)
{
	printf("%d\n",node->data);
	if(NULL == *head)
	{
		*head = node;
	}
	else
	{
		(*head)->left->right = node;
		node->left = (*head)->left;
	}
	(*head)->left = node;
}

//	按照中序转换二叉树
void _tree_to_list(TreeNode* root,TreeNode** head)
{
	if(NULL == root) return;
	_tree_to_list(root->left,head);
	__add_tail_list(head,root);
	_tree_to_list(root->right,head);
}

//	3、有序二叉树转双向链表
TreeNode* tree_to_list(TreeNode* root)
{
	TreeNode* head = NULL;	//不带头节点 所以要二级指针
	_tree_to_list(root,&head);
	head->left->right = head;
	return head;
}

计算二叉树倒数第k个数

bool _access_rdl_tree(TreeNode* root,size_t index,int* val,int* k)
{
	if(NULL==root) return false;
	if(_access_rdl_tree(root->right,index,val,k)) return true;
	if((*k)++ == index)
	{
		*val=root->data;
		return true;
	}
	return _access_rdl_tree(root->left,index,val,k);
}

判断二叉树是否是对称

//判断两棵树是否对称
bool _is_mirror(TreeNode* r1,TreeNode* r2)
{
	if(NULL == r1 && NULL == r2) return true;	
	if(NULL != r1 && NULL == r2) return false;
	if(NULL == r1 && NULL != r2) return false;
	if(r1->data != r2->data) return false;
	return _is_mirror(r1->right,r2->left) && _is_mirror(r1->left,r2->right);
}
bool is_mirror_tree(TreeNode* root)
{
	if(NULL == root)
		return true;
	return _is_mirror(root->left,root->right);
}

之字形遍历二叉树

void z_show(TreeNode* root)
{
	Liststack* s1=creat_list_stack();
	Liststack* s2=creat_list_stack();
	
	push_list_stack(s1,root);
	while(!empty_list_stack(s1) || !empty_list_stack(s2))
	{
		// s1出栈 从左往右入s2
		while(!empty_list_stack(s1))
		{
			TreeNode* top=top_list_stack(s1);
			if(top->left) push_list_stack(s2,top->left);
			if(top->right) push_list_stack(s2,top->right);
			printf("%d ",top->data);
			pop_list_stack(s1);
		}
		//s2出栈 从右往左入s1
		while(!empty_list_stack(s2))
		{
			TreeNode* top=top_list_stack(s2);
			if(top->right) push_list_stack(s1,top->right);
			if(top->left) push_list_stack(s1,top->left);
			printf("%d ",top->data);
			pop_list_stack(sw);
		}


	}
	destroy_list_stack(s1);
	destroy_list_stack(s2);
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list_stack.h"

//	有序二叉树


TreeNode* create_tree_node(int data)
{
	TreeNode* node = malloc(sizeof(TreeNode));
	node->data = data;
	node->left;
	node->right;
	return node;
}

void _insert_tree(TreeNode** root,TreeNode* node)
{
	if(NULL == *root)
	{
		*root = node;
		return;
	}
	if(node->data < (*root)->data)
		_insert_tree(&(*root)->left,node);
	else
		_insert_tree(&(*root)->right,node);
}

//	添加
void insert_tree(TreeNode** root,int data)
{
	_insert_tree(root,create_tree_node(data));	
}

void dlr_show(TreeNode* root)
{
	if(NULL == root) return;
	printf("%d ",root->data);
	dlr_show(root->left);
	dlr_show(root->right);
}
void ldr_show(TreeNode* root)
{
	if(NULL == root) return;
	ldr_show(root->left);
	printf("%d ",root->data);
	ldr_show(root->right);
}

void lrd_show(TreeNode* root)
{
	if(NULL == root) return;
	lrd_show(root->left);
	lrd_show(root->right);
	printf("%d ",root->data);
}

//	查找
bool query_tree(TreeNode* root,int data)
{
	if(NULL == root) return false;
	if(data == root->data) return true;
	if(data < root->data)
		return query_tree(root->left,data);
	return query_tree(root->right,data);
}

//	高度
int high_tree(TreeNode* root)
{
	if(NULL == root) return 0;
	int lh = high_tree(root->left);
	int rh = high_tree(root->right);
	return lh>rh ? lh+1:rh+1;
}
//	密度
//	销毁

bool _access_ldr_tree(TreeNode* root,size_t index,int* val,int* k)
{
	if(NULL == root) return false;

	if(_access_ldr_tree(root->left,index,val,k)) return true;
	if((*k)++ == index)
	{
		*val = root->data;
		return true;
	}
	return _access_ldr_tree(root->right,index,val,k);
}

//	按中序访问
bool access_ldr_tree(TreeNode* root,size_t index,int* val)
{
	int k = 0;
	// 递归使用同一个k来计数,所以需要共享同一个变量k
	return _access_ldr_tree(root,index,val,&k);
}

//	按值删除
bool del_value_tree(TreeNode** root,int data)
{
	if(NULL == *root) return false;
	//	找到data
	if(data == (*root)->data)
	{
		//  左子树替换data节点
		TreeNode* temp = *root;
		*root = temp->left;
		//	data右子树重新往data的左子树插入
		_insert_tree(root,temp->right);
		//	删除原节点
		free(temp);
		//	返回
		return true;
	}
	if(data < (*root)->data)
		return del_value_tree(&(*root)->left,data);
	return del_value_tree(&(*root)->right,data);
}

//	判断是否平衡
//	左右子树高度差不超过1 子树都满足
bool is_AVL_tree(TreeNode* root)
{
	if(NULL == root) return true;
	int lh = high_tree(root->left);
	int rh = high_tree(root->right);
	return abs(lh - rh)<=1 && 
		is_AVL_tree(root->left) && is_AVL_tree(root->right);
}


//	1、转换镜像树
void mirror_tree(TreeNode* root)
{
	if(NULL == root) return;
	TreeNode* temp = root->left;
	root->left = root->right;
	root->right = temp;

	mirror_tree(root->left);
	mirror_tree(root->right);
}

//	比较两颗树是否包含
bool _contain_tree(TreeNode* r1,TreeNode* r2)
{
	if(NULL == r1 && NULL == r2) return true;
	if(NULL == r1 && NULL != r2) return false;
	if(NULL != r1 && NULL == r2) return true;

	if(r1->data != r2->data) 	return false;
	return _contain_tree(r1->left,r2->left) && _contain_tree(r1->right,r2->right);
}

//	2、判断子结构
bool is_subtree(TreeNode* Aroot,TreeNode* Broot)
{
	if(NULL == Aroot)	return false;
	
	if(Aroot->data == Broot->data)
	{
		if(_contain_tree(Aroot,Broot)) return true;	
	}

	return is_subtree(Aroot->left,Broot) || is_subtree(Aroot->right,Broot);
}

//	判断两棵树是否对称
bool _is_mirror(TreeNode* r1,TreeNode* r2)
{
	if(NULL == r1 && NULL == r2) return true;
	if(NULL == r1 && NULL != r2) return false;
	if(NULL != r1 && NULL == r2) return false;
	if(r1->data != r2->data)     return false;

	return _is_mirror(r1->right,r2->left) && _is_mirror(r1->left,r2->right);
}

//	5、判断是否对称
bool is_mirror_tree(TreeNode* root)
{
	return _is_mirror(root->left,root->right);
}

//	链表尾添加
void __add_tail_list(TreeNode** head,TreeNode* node)
{
	printf("%d\n",node->data);
	if(NULL == *head)
	{
		*head = node;
	}
	else
	{
		(*head)->left->right = node;
		node->left = (*head)->left;
	}
	(*head)->left = node;
}

//	按照中序转换二叉树
void _tree_to_list(TreeNode* root,TreeNode** head)
{
	if(NULL == root) return;
	_tree_to_list(root->left,head);
	__add_tail_list(head,root);
	_tree_to_list(root->right,head);
}

//	3、有序二叉树转双向链表
TreeNode* tree_to_list(TreeNode* root)
{
	TreeNode* head = NULL;	//不带头节点 所以要二级指针
	_tree_to_list(root,&head);
	head->left->right = head;
	return head;
}

//	6、之字形打印
void z_show(TreeNode* root)
{
	ListStack* s1 = create_list_stack();
	ListStack* s2 = create_list_stack();

	push_list_stack(s1,root);

	while(!empty_list_stack(s1) || !empty_list_stack(s2))
	{
		//	s1出栈 从左往右入s2
		while(!empty_list_stack(s1))
		{
			TreeNode* top = top_list_stack(s1);
			if(top->left) push_list_stack(s2,top->left);
			if(top->right) push_list_stack(s2,top->right);
			printf("%d ",top->data);
			pop_list_stack(s1);
		}
		printf("\n===============\n");
		//	s2出栈 从右往左入s1
		while(!empty_list_stack(s2))
		{
			TreeNode* top = top_list_stack(s2);
			if(top->right) push_list_stack(s1,top->right);
			if(top->left) push_list_stack(s1,top->left);
			printf("%d ",top->data);
			pop_list_stack(s2);
		}
		printf("\n===============\n");
	}
	destroy_list_stack(s1);
	destroy_list_stack(s2);
}

int main(int argc,const char* argv[])
{
	TreeNode* root = NULL;
	for(int i=0; i<10; i++)
	{
		int data = rand()%100;
		insert_tree(&root,data);
	}
	z_show(root);

/*
	TreeNode* head = tree_to_list(root);
	TreeNode* n = head;
	do{
		printf("%d ",n->data);
		n = n->right;
	}while(head != n);


	TreeNode* Aroot = NULL,*Broot = NULL;
	for(int i=0; i<5; i++)
	{
		int data = rand()%100;
		printf("%d ",data);
		insert_tree(&Aroot,data);
		insert_tree(&Broot,data);
	}
	for(int i=0; i<5; i++)
	{
		int data = rand()%100;
		printf("%d ",data);
		insert_tree(&Aroot,data);
	}
	insert_tree(&Broot,100);
	printf("\nsub:%d\n",is_subtree(Aroot,Broot));
	*/
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值