当前位置:网站首页>leetcode-另一棵树的子树
leetcode-另一棵树的子树
2022-08-05 02:00:00 【qq_52025208】
题目描述:
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。
class Solution {
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if(root == null || subRoot == null) return false;
if(isSameTree(root,subRoot)) return true;
if(isSubtree(root.left,subRoot)) return true;
if(isSubtree(root.right,subRoot)) return true;
return false;
}
public static boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q != null || p != null && q == null) {
return false;
}
if(p == null && q == null) {
return true;
}
if(p.val != q.val) {
return false;
}
return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
边栏推荐
- sql语句多字段多个值如何进行排序
- 【Endnote】Word插入自定义形式的Endnote文献格式
- [How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?
- How to create an rpm package
- 【七夕如何根据情侣倾听的音乐进行薅羊毛】背景音乐是否会影响情侣对酒的选择
- ExcelPatternTool: Excel表格-数据库互导工具
- std::string::find 返回值的坑
- Fragment visibility judgment
- 详细全面的postman接口测试实战教程
- EBS uses virtual columns and hint hints to optimize sql case
猜你喜欢

【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗

Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion

KingbaseES V8 GIS数据迁移方案(2. Kingbase GIS能力介绍)

释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展

.Net C# 控制台 使用 Win32 API 创建一个窗口

迁移学习——Distant Domain Transfer Learning

树形查找(二叉查找树)

测试工作这么难找吗?今年32,失业2个月,大龄测试工程师接下来该拿什么养家?

缺陷检测(图像处理部分)

新来个技术总监,把DDD落地的那叫一个高级,服气
随机推荐
手把手基于YOLOv5定制实现FacePose之《YOLO结构解读、YOLO数据格式转换、YOLO过程修改》
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
原生js实现多选框全部选中和取消效果
使用OpenVINO实现飞桨版PGNet推理程序
Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
A new technical director, who calls DDD a senior, is convinced
1349. 参加考试的最大学生数 状态压缩
Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
SuperMap支持的国产环境汇总
Programmer's list of sheep counting when insomnia | Daily anecdote
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
DDOS攻击真的是无解吗?不!
IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
优化Feed流遭遇拦路虎,是谁帮百度打破了“内存墙”?
为什么他们选择和AI恋爱?
Transfer Learning - Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
[Machine Learning] 21-day Challenge Study Notes (2)
软件测试技术之最有效的七大性能测试技术
【Redis】Linux下Redis安装
AI+PROTAC|dx/tx完成500万美元种子轮融资