当前位置:网站首页>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);
}
}
边栏推荐
- Ant Sword Advanced Module Development
- Common hardware delays
- LeetCode使用最小花费爬楼梯----dp问题
- 【C语言】详解栈和队列(定义、销毁、数据的操作)
- Matlab map with color representation module value size arrow
- VSCode Change Default Terminal how to modify the Default Terminal VSCode
- [ROS] (10) ROS Communication - Service Communication
- The 20th day of the special assault version of the sword offer
- leetcode-另一棵树的子树
- ARM Mailbox
猜你喜欢
[Decryption] Can the NFTs created by OpenSea for free appear in my wallet without being chained?
学习笔记-----左偏树
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)
Pisanix v0.2.0 released | Added support for dynamic read-write separation
Access Characteristics of Constructor under Inheritance Relationship
DAY23:命令执行&代码执行漏洞
特殊矩阵的压缩存储
select tag custom style
Data to enhance Mixup principle and code reading
OpenGL 工作原理
随机推荐
【genius_platform软件平台开发】第七十六讲:vs预处理器定义的牛逼写法!!!!(其他组牛逼conding人员告知这么配置来取消宏定义)
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
从零到一快速学会三子棋
Compressed storage of special matrices
[In-depth study of 4G/5G/6G topic-51]: URLLC-16-"3GPP URLLC related protocols, specifications, and technical principles in-depth interpretation"-11-High reliability technology-2-Link adaptive enhancem
金仓数据库如何验证安装文件平台正确性
【LeetCode刷题】-数之和专题(待补充更多题目)
[ROS](10)ROS通信 —— 服务(Service)通信
继承关系下构造方法的访问特点
Flink 1.15.1 集群搭建(StandaloneSession)
The design idea of DMicro, the Go microservice development framework
C language implements a simple number guessing game
Review 51 MCU
1873. 计算特殊奖金
VSCode Change Default Terminal how to modify the Default Terminal VSCode
The 2022 EdgeX China Challenge will be grandly opened on August 3
Apache DolphinScheduler, a new generation of distributed workflow task scheduling platform in practice - Medium
1527. Patients suffering from a disease
DAY22: sqli-labs shooting range clearance wp (Less01~~Less20)
Intel XDC 2022 Wonderful Review: Build an Open Ecosystem and Unleash the Potential of "Infrastructure"