[数据结构]:6.二叉树链式结构的实现2

二叉树链式结构的实现2

前言

嗨,我是firdawn,本章将使用二叉树的OJ题来帮大家巩固二叉树的遍历,查找操作,以及教大家学会任何通过递归方式实现二叉树的查找值,判断两棵树是否相同,一颗树是否是另一棵树的子树以及二叉树的创建操作。下面是本章的思维导图,那么让我们开始吧!

一,单值二叉树

题目:单值二叉树

答案:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool _isUnivalTree(struct TreeNode* root,int val)
{
    if(root==NULL)
    {
        return true;
    }
    if(root->val!=val)
    {
        return false;
    }
    return _isUnivalTree(root->left,val) && _isUnivalTree(root->right,val);
}

bool isUnivalTree(struct TreeNode* root) {

    //遍历判断
    //return  _isUnivalTree(root,root->val);

    //用分治思想解决
    //为空  返回ture
    //不为空    根和左子树相等,根和右子树相等并且左右子树都为单值二叉树时,该树为单值二叉树
    //
    if(root==NULL)
    {
        return true;
    }

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

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

二,检查两颗树是否相同

题目:检查两颗树是否相同

答案:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    //返回条件:一一比较两颗树的结点,1.如果p为空且q为空return ture     2.如果p为空q不为空,或者p为空q不为空,return false
    //子问题:先比较当前结点,当前结点不相等,就返回false
    //        如果当前结点相等,然后再来比较左子树是否相等和右子树是否相等,如果都相等return ture
    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);
}

三,对称二叉树

题目:对称二叉树

答案:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool _isSymmetric(struct TreeNode* left, struct TreeNode* right)
{
    //返回条件:1.left为空,right为空,return true  2.left为空right不为空或者left不为空right为空return false
    //子问题:1.left和right不相等return false   如果相等再比较left的左边和right的右边是否相等,以及left的右边和right的左边是否相等,都相等才返回true
    if(left == NULL && right == NULL)
    {
        return true;
    }
    if(left == NULL || right == NULL)
    {
        return false;
    }
    if(left->val != right->val)
    {
        return false;
    }
    return _isSymmetric(left->left,right->right) && _isSymmetric(left->right, right->left);

}


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

四,二叉树的前序遍历

题目:二叉树的前序遍历

答案:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
 int BinaryTreeSize(struct TreeNode* root)
 {
    return root==NULL ? 0 : BinaryTreeSize(root->left)+BinaryTreeSize(root->right)+1;
 }

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

}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize=BinaryTreeSize(root);
    int* arr=(int*)malloc(sizeof(int)*(*returnSize));
    if(arr==NULL)
    {
        perror("malloc fail");
        return NULL;
    }

    int i=0;
    _preorderTraversal(root,arr,&i);
    return arr;
}

五,另一棵树的子树

题目:另一颗树的子树

答案:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 bool isSameTree(struct TreeNode* root, struct TreeNode* subRoot)
 {
    if(root==NULL && subRoot == NULL)
    {
        return true;
    }
    if(root == NULL || subRoot == NULL)
    {
        return false;
    }
    if(root->val!=subRoot->val)
    {
        return false;
    }
    return isSameTree(root->left,subRoot->left) && isSameTree(root->right,subRoot->right);
 }
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot) {
    //解题思路:遍历root的所有子树,因为root的所有结点都可以看作子树的根结点,所以我们只需要遍历root的所有结点,再与subRoot比较,如果两颗树相等就返回true,不相等就再遍历root的左子树进行比较和右子树进行比较,只要子树中找到了就返回ture
    //返回条件:1.root为空return false  
    //子问题:root不为空,比较两颗树是否相等,相等就返回true,
    //      不相等就比较root的左子树是否和subRoot相等,比较root的右子树是否和subRoot相等,只要相等就返回true

    if(root == NULL)
    {
        return false;
    }
    if(root->val == subRoot->val && isSameTree(root,subRoot))
    {
        return true;
    }
    return isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot);
}

六,二叉树的创建

题目:二叉树的创建

答案:

c 复制代码
#include <stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>

typedef char BTDataType;
typedef struct BinaryTreeNode
{
    BTDataType val;
    struct BinaryTreeNode* left;
    struct BinaryTreeNode* right;
}BTNode;

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

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



int main() {
    BTDataType arr[100]={0};
    scanf("%s",arr);
    int i=0;
    BTNode* root = BinaryCreat(arr, &i);
    InOrder(root);
    return 0;
}
相关推荐
水饺编程2 小时前
第4章,[标签 Win32] :文本尺寸的度量
c语言·c++·windows·visual studio
浅念-2 小时前
C语言——自定义类型:结构体、联合体、枚举
c语言·开发语言·数据结构·c++·笔记·学习·html
mirror_zAI2 小时前
C语言中的sscanf用法详解
c语言·开发语言
仰泳的熊猫2 小时前
题目1433:蓝桥杯2013年第四届真题-危险系数
数据结构·c++·算法·蓝桥杯·深度优先·图论
cyforkk2 小时前
14、Java 基础硬核复习:数据结构与集合源码的核心逻辑与面试考点
java·数据结构·面试
试试勇气2 小时前
算法工具箱之哈希表
数据结构·算法·散列表
bin91533 小时前
C盘瘦身大作战:程序员的存储空间优化全攻略
c语言·开发语言·c盘清理·c盘清理技巧分享
共享家95273 小时前
双指针算法(一)
数据结构·算法·leetcode
shehuiyuelaiyuehao3 小时前
数据结构19栈和队列
数据结构