LeetCode100

题目

给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入: 1 1
/ \ /
2 3 2 3

    [1,2,3],   [1,2,3]

输出: true
示例 2:

输入: 1 1
/
2 2

    [1,2],     [1,null,2]

输出: false
示例 3:

输入:

1 1
/ \ /
2 1 1 2

    [1,2,1],   [1,1,2]

输出: false

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/same-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码

#include <stdio.h>
#include <malloc.h>
#include <vss.h>

#define bool int
#define true 1
#define false 0

typedef struct BiTree{ // 二叉树的结构体
    int data;// 二叉树存储的内容
    struct BiTree *lcjild, *rchild; // 分别为左子树和右子树
}BiNode, * BiTree;

bool isSameTree(BiTree t1, BiTree t2){ // 递归
    if(t1==NULL&&t2==NULL) return true;
    if(t1==NULL) return false;
    if(t2==NULL) return false;
    if(t1->data==t2->data) {
        return isSameTree(t1->lcjild,t2->lcjild)&&isSameTree(t1->rchild,t2->rchild);
    } else{
        return false;
    }
};

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值