题目
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
示例 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;
}
};