当前位置:网站首页>leetcode - a subtree of another tree
leetcode - a subtree of another tree
2022-08-05 02:38: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);
}
}
边栏推荐
- 【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
- 正则表达式,匹配中间的某一段字符串
- 627. 变更性别
- Ant Sword Advanced Module Development
- leetcode-对称二叉树
- LeetCode使用最小花费爬楼梯----dp问题
- UOS系统下ksql应用缺少动态库”libtinfo.so.5“问题
- Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
- 1873. The special bonus calculation
- C student management system head to add a student node
猜你喜欢
[ROS] (10) ROS Communication - Service Communication
Data storage practice based on left-order traversal
02 【开发服务器 资源模块】
Regular expression to match a certain string in the middle
The design idea of DMicro, the Go microservice development framework
J9数字货币论:web3的创作者经济是什么?
【LeetCode刷题】-数之和专题(待补充更多题目)
Pisanix v0.2.0 发布|新增动态读写分离支持
shell语句修改txt文件或者sh文件
select tag custom style
随机推荐
Pisanix v0.2.0 发布|新增动态读写分离支持
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
Common hardware delays
甘特图来啦,项目管理神器,模板直接用
程序员的七夕浪漫时刻
OpenGL 工作原理
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
软链接引发的物理备份问题
J9数字货币论:web3的创作者经济是什么?
网络安全与元宇宙:找出薄弱环节
Snapback - same tree
采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
dmp(dump)转储文件
继承关系下构造方法的访问特点
Advanced Numbers_Review_Chapter 1: Functions, Limits, Continuity
LeetCode使用最小花费爬楼梯----dp问题
1667. 修复表中的名字
线性表的查找