1 Star 0 Fork 0

珂珂/Algorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
T1ContainT2.java 924 Bytes
一键复制 编辑 原始数据 按行查看 历史
珂珂 提交于 2021-07-17 17:12 . 2021/7/17 update
package zuo.chapter3;
/**
* @author keboom
* @date 2021/5/11
*/
public class T1ContainT2 {
public boolean contains(TreeNode t1, TreeNode t2) {
if (t2 == null) {
return true;
}
if (t1 == null) {
return false;
}
// 因为t1不是一开头就跟t2匹配,所以也需要contains递归去找
// 只要找到t1的子节点与t2相等的,则进行check递归,由于或运算只要有一个true,则结果为true
return check(t1, t2) || contains(t1.left, t2) || contains(t1.right, t2);
}
private boolean check(TreeNode h, TreeNode t2) {
if (t2 == null) {
return true;
}
// 此还可判断t1子节点是否与t2头结点相等呢!
if (h == null || h.value != t2.value) {
return false;
}
return check(h.left, t2.left) && check(h.right, t2.right);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/keke518/Algorithm.git
git@gitee.com:keke518/Algorithm.git
keke518
Algorithm
Algorithm
master

搜索帮助

A270a887 8829481 3d7a4017 8829481