头文件
#include
#include
#include
二叉树的结构
typedef int STDatatype;
typedef struct Stree
{
STDatatype *data;//数据
struct STree *left;//左子树
struct STree *right;//右子树
}BTNode;
二叉树的节点(浅试一下6个节点)
BTNode* CreateTree()
{
BTNode * n1 = (BTNode*)malloc(sizeof(BTNode));
assert(n1);
BTNode * n2 = (BTNode*)malloc(sizeof(BTNode));
assert(n2);
BTNode * n3 = (BTNode*)malloc(sizeof(BTNode));
assert(n3);
BTNode * n4 = (BTNode*)malloc(sizeof(BTNode));
assert(n4);
BTNode * n5 = (BTNode*)malloc(sizeof(BTNode));
assert(n5);
BTNode * n6 = (BTNode*)malloc(sizeof(BTNode));
assert(n6);
n1->data = 1;
n2->data = 2;
n3->data = 3;
n4->data = 4;
n5->data = 5;
n6->data = 6;
n1->left = n2;
n1->right = n4;
n2->left = n3;
n2->right = NULL;
n3->left = NULL;
n3->right = NULL;
n4->left = n5;
n4->right = n6;
n5->left = NULL;
n5->right = NULL;
n6->left = NULL;
n6->right = NULL;
return n1;
}
二叉树的销毁
TreeDestroy(BTNode* root)//销毁
{
if (root == NULL)
{
return;
}
TreeDestroy(root->left);
TreeDestroy(root->right);
free(root);
}
由于遍历二叉树的顺序不同,则打印出来的内容也不同。
preorder(BTNode* root)//前序遍历
{
if (root == NULL)
{
printf("NULL");
return ;
}
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
aforder(BTNode*root)//中序遍历
{
if (root == NULL)
{
printf("NULL");//遇到空就返回
return ;
}
aforder(root->left);//左子树
printf("%d ", root->data);//根
aforder(root->right);//右子树
}
laorder(BTNode* root)//后序遍历
{
if (root == NULL)
{
printf("NULL");
return ;
}
laorder(root->left);
laorder(root->right);
printf("%d ", root->data);
}
求节点(度)的个数
int TreeSize(BTNode* root)
{
return root == NULL ? 0 :
TreeSize(root->left) +TreeSize(root->right)+1;
}
求叶子节点的个数
int Leafsize(BTNode* root)
{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
{
return 1;
}
return Leafsize(root->left) + Leafsize(root->right);
}
int Treehigh(BTNode* root)
{
if (root == NULL)
return 0;
int lt = Treehigh(root->left);
int rt = Treehigh(root->right);
return lt > rt ? lt + 1 : rt + 1;
}
int Ktreesize(BTNode* root, int k)//求第k层节点个数
{
assert(k > 0);
if (root == NULL)//为空返回0
{
return 0;
}
if (k == 1)
{
return 1;
}
return Ktreesize(root->left,k-1) +Ktreesize(root->right,k-1);
//设第一层节点(第一个节点)为第0个,那么就是左子树第一个节点就是第1个节点,则传k-1
}
BTNode* TreeFind(BTNode* root, STDatatype X)//在二叉树找相应节点-返回该
{
if (root == NULL)
return NULL;
if (root->data == X)//返回该节点地址,主函数用%p来接收
{
return root;
}
//先去左树找
BTNode*lret= TreeFind(root->left, X);
if (lret)
return lret;
//找不到再去右树找
BTNode*rret=TreeFind(root->right, X);
if (rret)
return rret;
return NULL;//最后没找到要返回空
}
层序遍历二叉树(广度优先遍历)
void TreelevelOrder(BTNode* root)//用排列构建二叉树并打印-层序遍历
{
Queue q;
QueueInit(&q);
if (root)
{
QueuePush(&q, root);
}
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
printf("%d ", front->data);
if(front->left)//一定要判断是不是空
QueuePush(&q, front->left);
if(front->right)//一定要判断是不是空
QueuePush(&q, front->right);
}
printf("\n");
QueueDestory(&q);
}
判断二叉树是不是完全二叉树
isTreecomplete(BTNode* root)
{
Queue q;
QueueInit(&q);
if (root)//push根结点进队列
{
QueuePush(&q, root);
}
while (!QueueEmpty(&q))//结点不为空
{
BTNode* front = QueueFront(&q);.//记录结点
QueuePop(&q);//pop
if (front == NULL)
{
break;// 当遇到空可以判断是不是完全二叉树
}
QueuePush(&q, front->left);//push左子树的结点
QueuePush(&q, front->right);//push右子树的结点
}
//遇到空,若后面全是空则为完全二叉树
//遇到空,若后面有非空则不为完全二叉树
while (!QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front != NULL)
{
QueueDestory(&q);
return false;
}
}
return true;
}
二叉树的前序遍历
力扣https://leetcode.cn/problems/binary-tree-preorder-traversal/submissions/
void preorder(struct TreeNode*root,int* a,int* pi)
{
if(root==NULL)
return;
a[*pi]=root->val;
(*pi)++;
preorder(root->left,a,pi);
preorder(root->right,a,pi);
}
int Treesize(struct TreeNode*root)
{
if(root==NULL)
return 0;
return Treesize(root->left)+Treesize(root->right)+1;
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int n=Treesize(root);//由于该题要返回一个节点个数的值,所以先遍历一遍记录节点个数
int* a=(int*)malloc(sizeof(int)*n);//开辟一块空间(数组形式)作为接收root的值
int i=0;//由于要遍历a数组所以需要下标
preorder(root,a,&i);//前序遍历
*returnSize=n;//最后返回节点个数的值
return a;//返回数值-以前序遍历的结构返回root
}
力扣https://leetcode.cn/problems/same-tree/submissions/
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p==NULL && q==NULL)//假设两颗树都为空-true
return true;
if(p==NULL|| q==NULL ||p->val!=q->val )//两颗树一棵为空一棵不为空,或者两棵树的值不相同-false
return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);//然后到子树判断
return true;//最后剩下的情况都为true
}
力扣https://leetcode.cn/problems/subtree-of-another-tree/submissions/
bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p==NULL && q==NULL)//假设两颗树都为空-true
return true;
if(p==NULL|| q==NULL ||p->val!=q->val )//两颗树一棵为空一棵不为空,或者两棵树的值不相同-false
return false;
return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);//然后到子树判断
return true;//最后剩下的情况都为true
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
if(root==NULL)//如果root为空-false
return false;
if(isSameTree(root,subRoot)==true)//如果现在的树的结构相同-true
return true;
return isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot);//然后让子树去比较
}
单值二叉树
力扣https://leetcode.cn/problems/univalued-binary-tree/submissions/
bool isUnivalTree(struct TreeNode* root){
if(root==NULL)//要么一开始就为空,要么遍历到最后的节点都不被return false则为true
return true;
if(root->left!=NULL && root->val!=root->left->val)//左子树节点不为空且值与根结点不相等
return false;
if(root->right!=NULL && root->val!=root->right->val)//右子树节点不为空且值与根结点不相等
return false;
return isUnivalTree(root->left) && isUnivalTree(root->right);//到左子树,右子树去遍历
}
二叉树对于我来说是个不小的挑战,学完基本结构之后要去刷刷leetcode题巩固一下,如果你刚好对二叉树感到疑惑,不妨看看我上面写的内容吧!!!