当前位置:网站首页>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);
}
}
边栏推荐
- 1873. 计算特殊奖金
- Quickly learn chess from zero to one
- 【OpenCV 图像处理2】:OpenCV 基础知识
- Amazon Cloud Technology joins hands with Thundersoft to build an AIoT platform for industry customers
- Programmer's Tanabata Romantic Moment
- 优炫数据库的单节点如何转集群
- 特殊矩阵的压缩存储
- Data to enhance Mixup principle and code reading
- matlab绘制用颜色表示模值大小的箭头图
- C学生管理系统 据学号查找学生节点
猜你喜欢
What should I do if the self-incrementing id of online MySQL is exhausted?
View handler 踩坑记录
在这个超连接的世界里,你的数据安全吗
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
DAY23:命令执行&代码执行漏洞
Opening - Open a new .NET modern application development experience
倒计时 2 天|云原生 Meetup 广州站,等你来!
Quickly learn chess from zero to one
lua learning
随机推荐
北斗三号短报文终端露天矿山高边坡监测方案
Snapback - same tree
ARM Mailbox
1527. 患某种疾病的患者
Error: Not a signal or slot declaration
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
SuperMap iDesktop.Net之布尔运算求交——修复含拓扑错误复杂模型
lua learning
Flink 1.15.1 集群搭建(StandaloneSession)
线性表的查找
Ant Sword Advanced Module Development
采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
Quickly learn chess from zero to one
后期学习计划
undo问题
[ROS](10)ROS通信 —— 服务(Service)通信
The 22-07-31 weeks summary
Error: Not a signal or slot declaration
[深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
力扣-相同的树