二叉树OJ题

1.单值⼆叉树

链接

. - 力扣(LeetCode)

题目

思路

将当前所在的节点的值,与左右子树相比较,都相同则返回true,否则返回false。

代码

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.相同的树

链接

. - 力扣(LeetCode)

题目

思路

将两棵树的结点依次比较,需要注意的是两颗树都为空的情况

代码

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.对称⼆叉树

链接

. - 力扣(LeetCode)

题目

思路

利用相同二叉树的的思路,但是要让树1的左子树与树2的右子树,树2的左子树和树1的右子树相比较,然后就是调用相同二叉树的代码,两棵树分别就是需要判断二叉树的左子树和右子树。

代码

 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->right) && isSameTree(p->right,q->left);
}
bool isSymmetric(struct TreeNode* root) 
{
    if(root == NULL)
    {
        return true;
    }
    return isSameTree(root->left,root->right);
}

4.另⼀棵树的⼦树

链接

. - 力扣(LeetCode)

题目

思路

也要借助相同二叉树的代码,递归需要判断的树,递归每个结点是都要判断以该结点为根的子树与判断的数是否相同。

代码

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

5.前序遍历

链接

. - 力扣(LeetCode)

题目

思路

先求出二叉树的长度,然后申请长度大小的数组,利用前序遍历将根节点的数据存储到数组中最后返回数组。

代码

int TreeSize(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }
    return 1 + TreeSize(root->left) + TreeSize(root->right);
}
void preorder(struct TreeNode* root, int* arr,int* i)
{
    if(root == NULL)
    {
        return ;
    }
    arr[(*i)++] = root->val;
    preorder(root->left,arr,i);
    preorder(root->right,arr,i);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) 
{
    
    int* arr;
    //求二叉树的结点个数
    int size = TreeSize(root);
    //动态申请内存
    arr = (int*)malloc(sizeof(int)*size);
    int i = 0;
    preorder(root,arr,&i);
    *returnSize = size;
    return arr;
}

6.中序遍历

链接

. - 力扣(LeetCode)

题目

思路

与前序遍历的思路类似

代码

int TreeSize(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }
    return 1 + TreeSize(root->left) + TreeSize(root->right);
}
void preorder(struct TreeNode* root, int* arr,int* pi)
{
    if(root == NULL)
    {
        return;
    }
    
    preorder(root->left,arr,pi);
    arr[(*pi)++] = root->val;
    preorder(root->right,arr,pi);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) 
{
    
    int* arr;
    //求二叉树的结点个数
    int size = TreeSize(root);
    //动态申请内存
    arr = (int*)malloc(sizeof(int)*size);
    int pi = 0;
    preorder(root,arr,&pi);
    *returnSize = size;
    return arr;
}

7.后序遍历

链接

. - 力扣(LeetCode)

题目

思路

与前序遍历的思路类似

代码

int TreeSize(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }
    return TreeSize(root->left) + TreeSize(root->right) + 1;
}
void postorder(struct TreeNode* root, int* arr,int* i)
{
   if(root == NULL)
   {
     return ;
   } 
   postorder(root->left,arr,i);
   postorder(root->right,arr,i);
   arr[(*i)++] = root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize) 
{
    *returnSize = TreeSize(root);
    int* arr = (int*)malloc(sizeof(int) * (*returnSize));
    if(arr == NULL)
    {
        exit(-1);
    }
    int i = 0;
    postorder(root,arr,&i);
    return arr;
    
}

8.⼆叉树的构建及遍历

链接

二叉树遍历_牛客题霸_牛客网

题目

思路

用数组先接收树的结构,然后通过树的遍历类似的方法构建树。

代码

typedef struct TreeNode
​
{
​
  char val;
​
  struct TreeNode* left;
​
  struct TreeNode* right;
​
}BTNode;
​
BTNode* TreeInit(char* arr,int* i)
​
{
​
  if(arr[*i] == '#')
​
  {
​
•    (*i)++;
​
•    return NULL;
​
  }
​
  BTNode* root = (BTNode*)malloc(sizeof(BTNode));
​
  root->left = NULL;
​
  root->right = NULL;
​
  root->val = arr[(*i)++];
​
  root->left = TreeInit(arr,i);
​
  root->right = TreeInit(arr,i);
​
  return root;
​
}
​
void InOrder(BTNode* root)
​
{
​
  if(root == NULL)
​
  {
​
•    return;
​
  }
​
  InOrder(root->left);
​
  printf("%c ",root->val);
​
  InOrder(root->right);
​
}
​
int main() 
​
{
​
  
​
  char arr[100];
​
  scanf("%s",arr);
​
  int i = 0;
​
  BTNode* root = TreeInit(arr,&i);
​
  InOrder(root);
​
  return 0;
​
}
​
​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值