数据结构与算法系列之二叉树习题练习

数据结构与算法系列之二叉树习题练习_第1张图片

博客:小怡同学
个人简介:编程小萌新
如果博客对大家有用的话,请点赞关注再收藏

创建二叉树

//手动创建

typedef struct TreeNode
{
	int a ;
	struct TreeNode* left;
	struct TreeNode* right;
}TreeNode;


TreeNode* BuyNode(int a)
{
	TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
	assert(root);
	root->left = NULL;
	root->right = NULL;
	root->a = a;
	return root;
}

//创建树
TreeNode* CreatTree()
{
	TreeNode* node1 = BuyNode(1);
	TreeNode* node2 = BuyNode(2);
	TreeNode* node3 = BuyNode(3);
	TreeNode* node4 = BuyNode(4);
	TreeNode* node5 = BuyNode(5);
	TreeNode* node6 = BuyNode(6);

	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node5;
	return node1;
}

//先序遍历
void preorder(TreeNode* tmp)
{
	if (tmp == NULL)
	{
		return;
	}
	printf("%d ", tmp->a);
	preorder(tmp->left);
	preorder(tmp->right);
}

int main()
{

	TreeNode* tmp = CreatTree();
	preorder(tmp);

	return 0;
}

//方法二手动输入字符串递归创建

#include 
#include 
typedef struct Snode
{
    char val;
    struct Snode* left;
 struct Snode* right;
}Snode;


Snode*  CreatTree(char* s,int* k)
{
    if(s[*k] == '#')
    {
        (*k)++;
        return NULL;
    }

    Snode * root =  ( Snode*)malloc(sizeof(Snode));
    if(root == NULL)
    {
        exit(-1);
    }
    root->val = s[(*k)++];
    root->left =  CreatTree(s,k);
    root->right = CreatTree(s,k);
    return root;
}


void preorder(Snode* root)
{
    if(root == NULL)
    {
        return;
    }
    
    preorder(root->left);
    printf("%c ",root->val);
    preorder(root->right);
}


int main() {

char s[100] = {0} ;
scanf("%s",s);
int i = 0;
Snode* root = CreatTree(s,&i);
preorder(root);
    return 0;
}

二叉树节点个数


// 二叉树节点个数
int BinaryTreeSize(TreeNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int leftsize = BinaryTreeSize(root->left);
	int rightsize = BinaryTreeSize(root->right);
	return leftsize + rightsize + 1;
}

二叉树叶子节点个数

int TreeHeight(TreeNode* root)
{
	if (root == NULL)
	{
		return 0;
	}
	int lHeight = TreeHeight(root->left);
	int rHeight = TreeHeight(root->right);
	return lHeight > rHeight ? lHeight + 1 : rHeight + 1;

}

二叉树第k层节点个数

int BinaryTreeLevelKSize(TreeNode* root, int k)
{
	if (root == NULL)//没到k层但是没有节点也返回
		return 0;
	if (k == 1)//当到k层时就返回
	{
		return 1;
	}
	int left = BinaryTreeLevelKSize(root->left, k - 1);
	int right = BinaryTreeLevelKSize(root->right, k - 1);
	return left + right;
}

单值二叉树


bool isUnivalTree(struct TreeNode* root){

    if(root == NULL)
    return true;

    if(root->left && root->val != root->left->val)
    {
        return false;
    }
    if(root->right && root->val != root->right->val)
    {
        return false;
    }

    return isUnivalTree(root->left) && isUnivalTree(root->right);
}

二叉树查找值为x的节点

TreeNode* BinaryTreeFind(TreeNode* root,int x)
{
	if (root == NULL)
		return NULL;
	if (root->val == x)
	{
		return root;
	}

	TreeNode* left = BinaryTreeFind(root->left, x);
	if (left)
	{
		return left;
	}
	TreeNode* right = BinaryTreeFind(root->right, x);
	if (right)
	{
		return right;
	}
	return NULL;

}

检查两颗树是否相同。

bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p == NULL && q== NULL)
{
    return true;
}
if(p == NULL || q == NULL)
{
    return false;
}

if(p->val != q ->val )
{
    return false;
}

return isSameTree(p->left,q->left) && isSameTree(p->right ,q ->right);

检查两颗树是否相同

bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
    if(p == NULL && q == NULL)
 return true;
if((p == NULL && q != NULL)  || (p != NULL && q == NULL))
{
    return false;
}

if(p->val != q->val)
{
    return false;
}

return isSameTree(p->left, q->right) && isSameTree(p->right, q->left) ;

}


bool isSymmetric(struct TreeNode* root){
if(root ==NULL)
{
    return true;
}
return isSameTree(root->left, root->right);

}

对称二叉树


```c
bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
    if(p == NULL && q == NULL)
 return true;
if((p == NULL && q != NULL)  || (p != NULL && q == NULL))
{
    return false;
}

if(p->val != q->val)
{
    return false;
}

return isSameTree(p->left, q->right) && isSameTree(p->right, q->left) ;

}


bool isSymmetric(struct TreeNode* root){
if(root ==NULL)
{
    return true;
}
return isSameTree(root->left, root->right);

}

二叉树的前序遍历。

void preorder(int* arr, struct TreeNode* root, int* k)
{
    if(root == NULL)
    {
        return;
    }

    arr[(*k)++] = root->val;

    preorder(arr,root->left,k);
    preorder(arr,root->right,k);

}
int* preorderTraversal(struct TreeNode* root, int* returnSize){

*returnSize = 0;
int* arr = (int*)malloc(sizeof(int)* (20000));

preorder(arr,root,returnSize);
return arr;
}

二叉树中序遍历

void inorder(struct TreeNode* root, int* arr, int* returnSize){
if(root == NULL)
{
    return NULL;
}
inorder(root->left,arr,returnSize);
arr[(*returnSize)++] = root->val;
inorder(root->right,arr,returnSize);
}

int*  inorderTraversal(struct TreeNode* root, int* returnSize)
{
    int* arr = (int*)malloc(sizeof(int)*2000);
    *returnSize = 0;
    inorder(root,arr,returnSize);
    return arr;
}

二叉树的后序遍历

    
if(root == NULL)
{
    return NULL;
}
postorder(root->left,arr,returnSize);
postorder(root->right,arr,returnSize);
arr[(*returnSize)++] = root->val;
}

int*  postorderTraversal(struct TreeNode* root, int* returnSize)
{
    int* arr = (int*)malloc(sizeof(int)*2000);
    *returnSize = 0;
    postorder(root,arr,returnSize);
    return arr;
}

另一颗树的子树

bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p == NULL && q == NULL)
return true;
if((p == NULL && q != NULL)  || (p != NULL && q == NULL))
{
    return false;
}
if(p->val != q->val)
{
    return false;
}
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right) ;
}



bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){

if(root == NULL)
{
    return false;
}
if(isSameTree(root,subRoot))
{
    return true;
}

return isSubtree(root->left,subRoot) ||  isSubtree(root->right,subRoot);
}

二叉树的层序遍历

此层序遍历由栈来实现

Queue.h


#define _CRT_SECURE_NO_WARNINGS
#include 
#include  
#include 
#include 
typedef char SD;
typedef struct SNode
{
	struct SNode* left;
	struct SNode* right;
	SD val;
}SNode;



typedef  SNode*  QDatetype;
typedef struct Qnode
{
	 struct Qnode* next;
	QDatetype x;
}Qnode;
typedef struct Queue
{
	Qnode* head;
	Qnode* tail;
}Queue;

void QueueInit(Queue* pq); 
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq, QDatetype x);
void QueuePop(Queue* pq);
bool QueueEmpty(Queue* pq);
QDatetype QueueTop(Queue* pq);
int QueueSize(Queue* pq);
QDatetype QueueTail(Queue* pq);

Queue.c

#include "Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq);

	pq->tail = pq->head = NULL;

}
bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return  pq->head == NULL;
}
void QueueDestory(Queue* pq)
{
	assert(pq);
	while (pq->head)
	{
		Qnode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}
void QueuePush(Queue* pq, QDatetype q)
{
	assert(pq);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	assert(newnode);
	newnode->next = NULL;
	newnode->x = q;

	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;

	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		Qnode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	
}

QDatetype QueueTop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(&pq));
	return pq->head->x;
}



int QueueSize(Queue* pq)
{
	assert(pq);
	Qnode* head = pq->head;
	int size = 0;
	while (head)
	{
		size++;
		head = head->next;
	}
	return size;
}

QDatetype QueueTail(Queue* pq)
{
	assert(pq);
	return pq->tail->x;
}

test.c


#include "Queue.h"

SNode* BinaryTreeCreat(char* a, int* i)
{
	if (a[*i] == '#')
	{
		(*i)++;
		return NULL;
	}

	SNode* root = (SNode*)malloc(sizeof(SNode));
	root->val = a[(*i)++];
	root->left = BinaryTreeCreat(a, i);
	root->right = BinaryTreeCreat(a, i);

	return root;
}

void preorder(SNode* root)
{
	if (root == NULL)
	{
		return NULL;
	}
	printf("%c ", root->val);
	preorder(root->left);
	preorder(root->right);
}
int main()
{
	char a[100];
	scanf("%s", a);
	int i = 0;
	SNode* root = BinaryTreeCreat(a,&i);
	preorder(root);

	printf("\n");
	Queue pq;
	QueueInit(&pq);
	QueuePush(&pq, root);
	while (!QueueEmpty(&pq))
	{
		if (QueueTop(&pq)->left)
		{
			QueuePush(&pq, QueueTop(&pq)->left);
		}
		if (QueueTop(&pq)->right)
		{
			QueuePush(&pq, QueueTop(&pq)->right);
		}

		printf("%c ", QueueTop(&pq)->val);
		QueuePop(&pq);
		

	}
	QueueDestory(&pq);
	
	return 0;
}

数据结构与算法系列之二叉树习题练习_第2张图片

你可能感兴趣的:(算法,数据结构)