二叉树基础OJ练习

该博客介绍了多种二叉树操作的算法实现,包括单值二叉树、判断两棵树是否相同、对称二叉树的检查、前序遍历、中序遍历、后序遍历、子树判断、二叉树的创建、翻转以及平衡二叉树的检测。这些算法均通过递归方式实现,并提供了具体的C语言代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 单值二叉树

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);
}

2. 检查两颗树是否相同

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);
}

3.对称二叉树

bool Symmetric(struct TreeNode* L,struct TreeNode* R)
{
    if(R==NULL&&L==NULL)
        return true;
    if(R==NULL||L==NULL||R->val!=L->val)
        return false;

    return Symmetric(R->left,L->right)&&Symmetric(L->left,R->right);

}

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

4.二叉树的前序遍历

//总节点数
int TreeSize(struct TreeNode* root)
{
    if(root==NULL)
        return 0;
    
    return TreeSize(root->left)+TreeSize(root->right)+1;
}

void preorder(struct TreeNode* root,int* a,int* pi)
{
    if(root==NULL)
        return;

    a[*pi]=root->val;
    printf("a[%d]=%d\n",*pi,a[*pi]);
    (*pi)++;

    preorder(root->left,a,pi);
    preorder(root->right,a,pi);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int n=TreeSize(root);
    printf("size:%d\n",n);
    int* a=(int*)malloc(sizeof(int)*n);//扩展内存

    int i=0;
    preorder(root,a,&i);

    *returnSize=n;
    return a;
}

5二叉树的中序遍历

6二叉树的后序遍历

7.另一棵树的子树

//判断是否相同
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 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);
    
}

8.二叉树遍历

#include <stdio.h>
#include <stdlib.h>

typedef char BTDataType;
typedef struct BinaeyTreeNode
{
	BTDataType data;
	struct BinaeyTreeNode* left;
	struct BinaeyTreeNode* right;
}BTNode;

BTNode* BinaeyTreeCreate(BTDataType* a,int* pi)
{
    if(a[*pi]=='#')
    {
        (*pi)++;
        return NULL;
    }
    
    BTNode* root=(BTNode*)malloc(sizeof(BTNode));
    if(root==NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    
    root->data=a[*pi];
    (*pi)++;
    root->left=BinaeyTreeCreate(a,pi);
    root->right=BinaeyTreeCreate(a,pi);
    return root;
    
}

void Inorder(BTNode* root)
{
    if(root==NULL)
        return;
    
    Inorder(root->left);
    printf("%c ",root->data);
    Inorder(root->right);
}
int main()
{
    char str[100];
    scanf("%s",str);
    int i=0;
    //创建树
    BTNode* root=BinaeyTreeCreate(str,&i);
    Inorder(root);
    
    return 0;
}

9.翻转二叉树

struct TreeNode* invertTree(struct TreeNode* root){
    if(root==NULL)
        return root;
    struct TreeNode* tmp=root->left;
    root->left=root->right;
    root->right=tmp;

    invertTree(root->left);
    invertTree(root->right);
    return root;
}

10.判断一棵二叉树是否是平衡二叉树

//二叉树的高度
int TreeHeight(struct TreeNode* root)
{
    if(root==NULL)
        return 0 ;
    int lh=TreeHeight(root->left);
    int rh=TreeHeight(root->right);

    return lh>rh?lh+1:rh+1;
}

bool isBalanced(struct TreeNode* root){
    if(root==NULL||(root->left==NULL)&&(root->right==NULL))
        return true;  //当达到叶子结点时
    if(fabs(TreeHeight(root->left)-TreeHeight(root->right))>1)
        return false;   //当左右高度差的绝对值超过1
    
    return isBalanced(root->left)&&isBalanced(root->right);//递归检查左右子树
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值