一集斗罗大陆的时间——教你如何应对 二叉树经典oj题

目录

二叉树遍历

另一颗树的子树

对称二叉树

单值二叉树

二叉树的最大深度

翻转二叉树

 相同的树


二叉树遍历

二叉树遍历_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力icon-default.png?t=M3K6https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId=60&&tqId=29483&rp=1&ru=/activity/oj&qru=/ta/tsing-kaoyan/question-ranking

 首先我们这道oj题是一个IO型,所以需要我们定义好我们需要的函数和需要引的头文件。

具体操作:

1、用结构体来定义二叉树

2、将数组中的数据(用中序遍历的方法)赋给二叉树各节点的val

3、用中序遍历的方式打印二叉树

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

struct TreeNode//用结构体定义二叉树
{
    struct TreeNode* left;
    struct TreeNode* right;
    char val;
};
//将数组中的数据(用中序遍历的方法)赋给二叉树各节点的val
struct TreeNode* CreatTree(char *str,int* pi)
{
    if(str[*pi]=='#')
    {
        (*pi)++;
        return NULL;
    }
    //为根节点开辟空间
    struct TreeNode* root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
    root->val=str[*pi];
    (*pi)++;
    root->left=CreatTree(str, pi);
    root->right=CreatTree(str, pi);
    return root;
    
}
//用中序遍历的方式打印二叉树
void InOrder(struct TreeNode* root)
{
    if(root==NULL)
        return;
    InOrder(root->left);
    printf("%c ",root->val);
    InOrder(root->right);
}
int main()
{   
    char str[100];
    scanf("%s",str);
    int i=0;
    //先往二叉树中插入数据
    struct TreeNode* root=CreatTree(str,&i);
    //再用中序遍历二叉树并且打印
    InOrder(root);
    
    return 0;
}

另一颗树的子树

572. 另一棵树的子树 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=M3K6https://leetcode-cn.com/problems/subtree-of-another-tree/利用递归的方法,root的左子树与subroot比较 和 root的右子树与subroot比较 只要有一个相同就是满足条件的。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


 
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;
    }
    //root的左子树与subroot比较 和 root的右子树与subroot比较 只要有一个相同就是满足条件的
    return isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot);
}

对称二叉树

101. 对称二叉树 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=M3K6https://leetcode-cn.com/problems/symmetric-tree/ 

同样是利用递归,只不过是判断root1的左孩子是否等于root2的右孩子。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


bool _isSymmetric(struct TreeNode* root1,struct TreeNode* root2)
{
    //左右子树为空
    if(root1 == NULL && root2 == NULL)
    {
        return true;
    }
    //左右子树有一个为空
    if(root1==NULL ||root2==NULL)
    {
        return false;
    }
    //左右值不相等
    if(root1->val != root2->val)
    {
        return false;
    }
    //递归左子树的左和右子树的右      和        递归左子树的右和右子树的左
    return _isSymmetric(root1->left,root2->right) && _isSymmetric(root1->right,root2->left);
}
 
bool isSymmetric(struct TreeNode* root){
    if(root==NULL)
    {
        return true;
    }
    return _isSymmetric(root->left,root->right);
}

单值二叉树

965. 单值二叉树 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=M3K6https://leetcode-cn.com/problems/univalued-binary-tree/ 

直接判断根节点的左右孩子结点的值是否等于根节点的值,若不等于直接返回false,若等于继续遍历,直到遇到空就返回。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


bool isSameTree(struct TreeNode* root,int val)
{
    if(root==NULL)
    return true;
    if(root->val!=val)
    return false;
    return isSameTree(root->left,val)&&isSameTree(root->right,val);

}
bool isUnivalTree(struct TreeNode* root)
{
    if(root==NULL)
    return true;
    int val=root->val;
    return isSameTree(root,val);
}

二叉树的最大深度

104. 二叉树的最大深度 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=M3K6https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

挨个进行递归遍历,每递归一层就加一,最后取左孩子遍历的深度与右孩子遍历的深度中最大的作为二叉树的最大深度。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int maxDepth(struct TreeNode* root)
{
    if(root==NULL)
    return 0;
    int left=maxDepth(root->left)+1;
    int right=maxDepth(root->right)+1;
    if(left>right)
    return left;
    return right;
}

翻转二叉树

226. 翻转二叉树 - 力扣(LeetCode) (leetcode-cn.com)icon-default.png?t=M3K6https://leetcode-cn.com/problems/invert-binary-tree/从根节点进行遍历,遇到左孩子就跟右孩子进行交换,直到为空。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

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

 相同的树

100. 相同的树 - 力扣(LeetCode) (leetcode-cn.com)

直接遍历,相等就返回true,不相等就返回false。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


bool isSameTree(struct TreeNode* p, struct TreeNode* q)
{
    if(p!=NULL&&q==NULL)
    return false;

    if(p==NULL&&q!=NULL)
    return false;

    if(p==NULL&&q==NULL)
    return true;

    if(p->val==q->val)
    {
    return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
    }
    else
    return false;
}

猜你喜欢

转载自blog.csdn.net/m0_57249790/article/details/124514114