二叉树OJ题

1.二叉树的构建及遍历(二叉树遍历_牛客题霸_牛客网

#include<stdio.h>

#include<assert.h>

#define BTelemtype char

typedef struct BinNode

{

  BTelemtype val;

  struct BinNode* left;

  struct BinNode* right;  

}BinNode;

typedef BinNode* BinTree;

void Creat_BinTree(BinTree* bt);

void inorder(BinTree bt);

int main(void)

{

  BinTree bt;

  Creat_BinTree(&bt);

  inorder(bt);

  return 0;

}

//创建二叉树

void Creat_BinTree(BinTree* bt)

{

  BTelemtype val;

  scanf("%c",&val);

  if(val=='#')

    *bt=NULL;

  else

  {

    *bt=(BinNode*)malloc(sizeof(BinNode));

    assert(*bt!=NULL);

    (*bt)->val=val;

    Creat_BinTree(&((*bt)->left));

    Creat_BinTree(&((*bt)->right));

     

  }

   

}

//中序遍历输出二叉树

void inorder(BinTree bt)

{

  if(bt!=NULL)

  {

    inorder(bt->left);

    printf("%c ",bt->val);

    inorder(bt->right);

  }

}

2.判断一颗二叉树是否是平衡二叉树(力扣

int height(struct TreeNode* root)

{

    int h1,h2;

    if(root==NULL)

       return false;

    h1=height(root->left);

    h2=height(root->right);

    return h1>h2?(h1+1):(h2+1);

}

bool isBalanced(struct TreeNode* root){

    int h1,h2;

    if(root == NULL)

    {

        return true;

    }

    h1=height(root->left);

    h2=height(root->right);

    int i=abs(h1-h2);

    if(i>1)

        return false;

    return isBalanced(root->left)&&isBalanced(root->right);

}

3.另一棵树的子树(力扣

bool _isequle(struct TreeNode*root,struct TreeNode* subRoot)

{

    if(root==NULL&&subRoot==NULL)

        return true;

    if(root==NULL||subRoot==NULL)

         return false;

    return root->val==subRoot->val&&_isequle(root->left,subRoot->left)&&_isequle(root->right,subRoot

    ->right);

}

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

    if(root==0)

       return false;

    if(subRoot==0)

       return true;

    if(_isequle(root,subRoot))

        return true;

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

}

4.二叉树的后续遍历(力扣)

//求树的结点个数

 int Size(struct TreeNode* root)

 {

     if(root==NULL)

        return 0;

    return Size(root->left)+Size(root->right)+1;

 }

 //后续遍历二叉树

 void _postorder(struct TreeNode* root,int *post_array,int *index)

 {

     if(root!=NULL)

     {

        _postorder(root->left,post_array,index);

        _postorder(root->right,post_array,index);

        post_array[*index]=root->val;

        (*index)++;

     }

 }

int* postorderTraversal(struct TreeNode* root, int* returnSize){

    int n=Size(root);

    *returnSize=n;

    int *post_array=(int*)malloc(sizeof(int)*n);

    int index=0;

    _postorder(root,post_array,&index);

    return post_array;

}

5.二叉树的中序遍历(力扣

//求二叉树结点个数

int Size(struct TreeNode* root)

{

    if(root==NULL)

        return 0;

    return Size(root->left)+Size(root->right)+1;

}

//中序遍历二叉树

void _inorder(struct TreeNode* root,int* in_array,int* index)

{

    if(root!=NULL)

    {

        _inorder(root->left,in_array,index);

        in_array[*index]=root->val;

        (*index)++;

        _inorder(root->right,in_array,index);

    }

}

int* inorderTraversal(struct TreeNode* root, int* returnSize){

    int n=Size(root);

    *returnSize=n;

    int* in_array=(int*)malloc(sizeof(int)*n);

    int index=0;

    _inorder(root,in_array,&index);

    return in_array;

}

6.二叉树的前序遍历(力扣

//求二叉树的结点个数

 int Size(struct TreeNode* root)

 {

     if(root==NULL)

        return 0;

    return Size(root->left)+Size(root->right)+1;

 }

 //前序遍历函数

 void _preorder(struct TreeNode* root,int* index,int* array)

 {

     if(root!=NULL)

     {

         array[*index]=root->val;

         (*index)++;

         _preorder(root->left,index,array);

         _preorder(root->right,index,array);

     }

 }

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

    int n=Size(root);

    *returnSize=n;

    int *preorder_array=(int*)malloc(sizeof(int)*n);

    int index=0;

    _preorder(root,&index,preorder_array);

    return preorder_array;

}

7.对称二叉树(力扣

bool _isSame(struct TreeNode* l_root,struct TreeNode* r_root)

{

    if(l_root==NULL&&r_root==NULL)

        return true;

    if(l_root==NULL||r_root==NULL)

        return false;

    return r_root->val==l_root->val&&_isSame(l_root->left,r_root->right)&&_isSame(l_root->right,r_root->left);

}

bool isSymmetric(struct TreeNode* root)

{

    if(root==NULL||(root->left==NULL&&root->right==NULL))

        return true;

    if(_isSame(root->left,root->right))

        return true;

    return false;

}

8.检查两颗树是否相同(力扣

bool isSameTree(struct TreeNode* p, struct TreeNode* q){

    if(p==NULL&&q==NULL)

        return true;

    if(p==NULL||q==NULL)

        return false;

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

}

9.翻转二叉树(力扣

struct TreeNode* invertTree(struct TreeNode* root){

    if (root == NULL) 

    {

        return NULL;

    }

    struct TreeNode* left = invertTree(root->left);

    struct TreeNode* right = invertTree(root->right);

    root->left = right;

    root->right = left;

    return root;

}

10.二叉树最大深度(力扣

int maxDepth(struct TreeNode* root){

    int Lh, Rh;

    if (root == NULL)

        return 0;

    Lh = maxDepth(root->left);

    Rh = maxDepth(root->right);

    return (Lh > Rh ? Lh : Rh) + 1;

}

11.单值二叉树(力扣

bool isUnivalTree(struct TreeNode* root){

   if(root == NULL)

        return true;

    bool left_res = (root->left==NULL || (root->left->val==root->val && isUnivalTree(root->left)));

    bool right_res = (root->right==NULL || (root->right->val==root->val && isUnivalTree(root->right)));

    return left_res && right_res;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值